common: make a common interface for creating a server socket

This commit is contained in:
Tyler Wilding
2022-04-26 21:32:46 -04:00
parent d41fe7e6ad
commit fe36687366
19 changed files with 416 additions and 325 deletions
+30 -166
View File
@@ -4,154 +4,19 @@
* Works with deci2.cpp (sceDeci2) to implement the networking on target
*/
#include <cstdio>
#include <utility>
// TODO - i think im not including the dependency right..?
#include "common/cross_sockets/xsocket.h"
#ifdef __linux
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <unistd.h>
#elif _WIN32
#include <Windows.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#endif
#include "common/listener_common.h"
#include "common/versions.h"
#include "Deci2Server.h"
#include "common/util/Assert.h"
Deci2Server::Deci2Server(std::function<bool()> shutdown_callback)
: want_exit(std::move(shutdown_callback)) {
buffer = new char[BUFFER_SIZE];
}
#include "common/cross_sockets/XSocket.h"
Deci2Server::~Deci2Server() {
close_server_socket();
close_socket(new_sock);
#include "common/versions.h"
#include <common/listener_common.h>
#include <common/util/Assert.h>
// if accept thread is running, kill it
if (accept_thread_running) {
kill_accept_thread = true;
accept_thread.join();
accept_thread_running = false;
}
#include "third-party/fmt/core.h"
delete[] buffer;
}
/*!
* Start waiting for the Listener to connect
*/
bool Deci2Server::init() {
server_socket = open_socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
server_socket = -1;
return false;
}
#ifdef __linux
int server_socket_opt = SO_REUSEADDR | SO_REUSEPORT;
#elif _WIN32
int server_socket_opt = SO_EXCLUSIVEADDRUSE;
#endif
int opt = 1;
if (set_socket_option(server_socket, SOL_SOCKET, server_socket_opt, &opt, sizeof(opt)) < 0) {
close_server_socket();
return false;
};
if (set_socket_option(server_socket, TCP_SOCKET_LEVEL, TCP_NODELAY, &opt, sizeof(opt)) < 0) {
close_server_socket();
return false;
}
if (set_socket_timeout(server_socket, 100000) < 0) {
close_server_socket();
return false;
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(DECI2_PORT);
if (bind(server_socket, (sockaddr*)&addr, sizeof(addr)) < 0) {
printf("[Deci2Server] Failed to bind\n");
close_server_socket();
return false;
}
if (listen(server_socket, 0) < 0) {
printf("[Deci2Server] Failed to listen\n");
close_server_socket();
return false;
}
server_initialized = true;
accept_thread_running = true;
kill_accept_thread = false;
accept_thread = std::thread(&Deci2Server::accept_thread_func, this);
return true;
}
void Deci2Server::close_server_socket() {
close_socket(server_socket);
server_socket = -1;
}
/*!
* Return true if the listener is connected.
*/
bool Deci2Server::check_for_listener() {
if (server_connected) {
if (accept_thread_running) {
accept_thread.join();
accept_thread_running = false;
}
return true;
} else {
return false;
}
}
/*!
* Send data from buffer. User must provide appropriate headers.
*/
void Deci2Server::send_data(void* buf, u16 len) {
lock();
if (!server_connected) {
printf("[DECI2] send while not connected, not sending!\n");
} else {
uint16_t prog = 0;
while (prog < len) {
int wrote = write_to_socket(new_sock, (char*)(buf) + prog, len - prog);
prog += wrote;
if (!server_connected || want_exit()) {
unlock();
return;
}
}
}
unlock();
}
/*!
* Lock the DECI mutex. Should be done before modifying protocols.
*/
void Deci2Server::lock() {
deci_mutex.lock();
}
/*!
* Unlock the DECI mutex. Should be done after modifying protocols.
*/
void Deci2Server::unlock() {
deci_mutex.unlock();
void Deci2Server::write_on_accept() {
u32 versions[2] = {versions::GOAL_VERSION_MAJOR, versions::GOAL_VERSION_MINOR};
write_to_socket(accepted_socket, (char*)&versions, 8);
}
/*!
@@ -161,7 +26,7 @@ void Deci2Server::unlock() {
void Deci2Server::wait_for_protos_ready() {
if (protocols_ready)
return;
std::unique_lock<std::mutex> lk(deci_mutex);
std::unique_lock<std::mutex> lk(server_mutex);
cv.wait(lk, [&] { return protocols_ready; });
}
@@ -180,14 +45,14 @@ void Deci2Server::send_proto_ready(Deci2Driver* drivers, int* driver_count) {
cv.notify_all();
}
void Deci2Server::run() {
void Deci2Server::read_data() {
int desired_size = (int)sizeof(Deci2Header);
int got = 0;
while (got < desired_size) {
ASSERT(got + desired_size < BUFFER_SIZE);
auto x = read_from_socket(new_sock, buffer + got, desired_size - got);
if (want_exit()) {
ASSERT(got + desired_size < buffer_size);
auto x = read_from_socket(accepted_socket, buffer + got, desired_size - got);
if (want_exit_callback()) {
return;
}
got += x > 0 ? x : 0;
@@ -222,7 +87,7 @@ void Deci2Server::run() {
auto& driver = d2_drivers[handler];
u32 sent_to_program = 0;
while (!want_exit() && (hdr->rsvd < hdr->len || sent_to_program < hdr->rsvd)) {
while (!want_exit_callback() && (hdr->rsvd < hdr->len || sent_to_program < hdr->rsvd)) {
// send what we have to the program
if (sent_to_program < hdr->rsvd) {
// driver.next_recv_size = 0;
@@ -236,8 +101,8 @@ void Deci2Server::run() {
// receive from network
if (hdr->rsvd < hdr->len) {
auto x = read_from_socket(new_sock, buffer + hdr->rsvd, hdr->len - hdr->rsvd);
if (want_exit()) {
auto x = read_from_socket(accepted_socket, buffer + hdr->rsvd, hdr->len - hdr->rsvd);
if (want_exit_callback()) {
return;
}
got += x > 0 ? x : 0;
@@ -249,21 +114,20 @@ void Deci2Server::run() {
unlock();
}
/*!
* Background thread for waiting for the listener.
*/
void Deci2Server::accept_thread_func() {
socklen_t l = sizeof(addr);
while (!kill_accept_thread) {
// TODO - might want to do a WSAStartUp call here as well, else it won't be balanced on the
// close
new_sock = accept(server_socket, (sockaddr*)&addr, &l);
if (new_sock >= 0) {
set_socket_timeout(new_sock, 100000);
u32 versions[2] = {versions::GOAL_VERSION_MAJOR, versions::GOAL_VERSION_MINOR};
write_to_socket(new_sock, (char*)&versions, 8); // todo, check result?
server_connected = true;
return;
void Deci2Server::send_data(void* buf, u16 len) {
lock();
if (!client_connected) {
printf("[DECI2] send while not connected, not sending!\n");
} else {
uint16_t prog = 0;
while (prog < len) {
int wrote = write_to_socket(accepted_socket, (char*)(buf) + prog, len - prog);
prog += wrote;
if (!client_connected || want_exit_callback()) {
unlock();
return;
}
}
}
unlock();
}