144 lines
5.1 KiB
C++
144 lines
5.1 KiB
C++
/*
|
|
__ _____ _______ __ _____ ____ ___ ___
|
|
/ |/ / / / / ___// / / /__ \ / __ \__ \< /
|
|
/ /|_/ / / / /\__ \/ /_/ /__/ // / / /_/ // /
|
|
/ / / / /_/ /___/ / __ // __// /_/ / __// /
|
|
/_/ /_/\____//____/_/ /_//____/\____/____/_/
|
|
|
|
Copyright (C) 2021 MatCat
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <cstdio>
|
|
#include <string>
|
|
#include <cstring>
|
|
#include <vector>
|
|
#include <cstdint>
|
|
#include <uv.h>
|
|
#include <algorithm>
|
|
#include "socketserver.hpp"
|
|
|
|
using namespace std;
|
|
/*
|
|
__ ________ __ ___ __
|
|
/ / / _/ __ )/ / / / | / /
|
|
/ / / // __ / / / /| | / /
|
|
/ /____/ // /_/ / /_/ / | |/ /
|
|
/_____/___/_____/\____/ |___/
|
|
|
|
|
|
_________________________________________________________________________________________________
|
|
/ ↓ ↓ ↓ ↓ ↓ ↓ \
|
|
*/
|
|
|
|
|
|
uv_loop_t *loop;
|
|
uv_tcp_t server;
|
|
|
|
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
|
|
buf->base = (char*) malloc(suggested_size);
|
|
buf->len = suggested_size;
|
|
}
|
|
|
|
int setupSocketServer() {
|
|
loop = uv_default_loop();
|
|
uv_tcp_init(loop, &server);
|
|
struct sockaddr_in bind_addr;
|
|
uv_ip4_addr("0.0.0.0", 1701, &bind_addr);
|
|
uv_tcp_bind(&server, (const struct sockaddr*)&bind_addr, 0);
|
|
int r = uv_listen((uv_stream_t*) &server, 512, on_new_connection);
|
|
if (r) {
|
|
fprintf(stderr, "ERROR Could not bind to host and port!\n");
|
|
return 1;
|
|
}
|
|
return uv_run(loop, UV_RUN_DEFAULT);
|
|
}
|
|
|
|
void client_write(uv_write_t *req, int status) {
|
|
if (status == -1) {
|
|
fprintf(stderr, "Write error!\n");
|
|
}
|
|
char *base = (char*) req->data;
|
|
free(base);
|
|
free(req);
|
|
}
|
|
|
|
void send_Message(uv_stream_t* client, char* msg) {
|
|
uv_buf_t outbuf = uv_buf_init(msg, strlen(msg));
|
|
uv_write_t *outreq = (uv_write_t *) malloc(sizeof(uv_write_t));
|
|
uv_write(outreq, (uv_stream_t *) client, &outbuf, 1, client_write);
|
|
}
|
|
|
|
|
|
void client_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
|
|
if (nread == -1) {
|
|
fprintf(stderr, "Read error!\n");
|
|
uv_close((uv_handle_t*)client, (uv_close_cb) free);
|
|
return;
|
|
}
|
|
printf("Got %d bytes\n%s",nread,buf->base);
|
|
if (nread > 0) {
|
|
if (buf->base[0] == '@') {
|
|
string baseString = buf->base;
|
|
string Command = baseString.substr(0,baseString.find(" "));
|
|
string ogCommand = Command;
|
|
transform(Command.begin(), Command.end(), Command.begin(), ::tolower);
|
|
Command.erase( remove(Command.begin(), Command.end(), '\r'), Command.end() );
|
|
Command.erase( remove(Command.begin(), Command.end(), '\n'), Command.end() );
|
|
printf("It's a command: %s\n",Command.c_str());
|
|
if (Command == "@help") {
|
|
send_Message(client,(char *)"There is no help for you!\n");
|
|
printf("User wants help!\n");
|
|
}
|
|
if (Command == "@quit") {
|
|
send_Message(client,(char *)"Bye!\n");
|
|
uv_close((uv_handle_t*) client,(uv_close_cb) free);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (nread <= 0) {
|
|
printf("Read error %s\n",uv_err_name(nread));
|
|
uv_close((uv_handle_t*) client, (uv_close_cb) free);
|
|
return;
|
|
}
|
|
}
|
|
|
|
void on_new_connection(uv_stream_t *server, int status) {
|
|
if (status == -1) {
|
|
return;
|
|
}
|
|
|
|
uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
|
|
uv_tcp_init(loop, client);
|
|
if (uv_accept(server, (uv_stream_t*) client) == 0) {
|
|
printf("New Connection\n");
|
|
uv_read_start((uv_stream_t*) client, alloc_buffer, client_read);
|
|
send_Message((uv_stream_t*) client,(char *)"Welcome!\n");
|
|
|
|
}
|
|
else {
|
|
uv_close((uv_handle_t*) client, (uv_close_cb) free);
|
|
printf("Dropping connection\n");
|
|
}
|
|
}
|
|
|
|
/*
|
|
\________↑_______________↑_______________↑_______________↑_______________↑_______________↑________/
|
|
END OF SECTION: LIBUV /
|
|
________________________________________________________________________________________________/
|
|
*/
|