mirror of
https://github.com/open-goal/jak-project
synced 2026-06-28 03:03:29 -04:00
Windows fixes
More Windows fixes
This commit is contained in:
+21
-18
@@ -2,12 +2,11 @@
|
||||
* @file Deci2Server.cpp
|
||||
* Basic implementation of a DECI2 server.
|
||||
* Works with deci2.cpp (sceDeci2) to implement the networking on target
|
||||
*/
|
||||
|
||||
|
||||
#include <cstdio>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <unistd.h>
|
||||
#include <Winsock2.h>
|
||||
#include <io.h>
|
||||
#include <cassert>
|
||||
#include <utility>
|
||||
|
||||
@@ -15,6 +14,8 @@
|
||||
#include "common/versions.h"
|
||||
#include "Deci2Server.h"
|
||||
|
||||
typedef int socklen_t;
|
||||
|
||||
Deci2Server::Deci2Server(std::function<bool()> shutdown_callback) {
|
||||
buffer = new char[BUFFER_SIZE];
|
||||
want_exit = std::move(shutdown_callback);
|
||||
@@ -41,7 +42,7 @@ Deci2Server::~Deci2Server() {
|
||||
|
||||
/*!
|
||||
* Start waiting for the Listener to connect
|
||||
*/
|
||||
|
||||
bool Deci2Server::init() {
|
||||
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (server_fd < 0) {
|
||||
@@ -49,16 +50,16 @@ bool Deci2Server::init() {
|
||||
return false;
|
||||
}
|
||||
|
||||
int opt = 1;
|
||||
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
|
||||
const char opt = 1;
|
||||
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_BROADCAST, &opt, sizeof(opt))) {
|
||||
printf("[Deci2Server] Failed to setsockopt 1\n");
|
||||
close(server_fd);
|
||||
server_fd = -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
int one = 1;
|
||||
if (setsockopt(server_fd, SOL_TCP, TCP_NODELAY, &one, sizeof(one))) {
|
||||
const char one = 1;
|
||||
if (setsockopt(server_fd, SOL_SOCKET, TCP_NODELAY, &one, sizeof(one))) {
|
||||
printf("[Deci2Server] Failed to setsockopt 2\n");
|
||||
close(server_fd);
|
||||
server_fd = -1;
|
||||
@@ -103,7 +104,7 @@ bool Deci2Server::init() {
|
||||
|
||||
/*!
|
||||
* Return true if the listener is connected.
|
||||
*/
|
||||
|
||||
bool Deci2Server::check_for_listener() {
|
||||
if (server_connected) {
|
||||
if (accept_thread_running) {
|
||||
@@ -118,7 +119,7 @@ bool Deci2Server::check_for_listener() {
|
||||
|
||||
/*!
|
||||
* Send data from buffer. User must provide appropriate headers.
|
||||
*/
|
||||
|
||||
void Deci2Server::send_data(void* buf, u16 len) {
|
||||
lock();
|
||||
if (!server_connected) {
|
||||
@@ -139,14 +140,14 @@ void Deci2Server::send_data(void* buf, u16 len) {
|
||||
|
||||
/*!
|
||||
* 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();
|
||||
}
|
||||
@@ -154,7 +155,7 @@ void Deci2Server::unlock() {
|
||||
/*!
|
||||
* Wait for protocols to become ready.
|
||||
* This avoids the case where we receive messages before protocol handlers are set up.
|
||||
*/
|
||||
|
||||
void Deci2Server::wait_for_protos_ready() {
|
||||
if (protocols_ready)
|
||||
return;
|
||||
@@ -167,7 +168,7 @@ void Deci2Server::wait_for_protos_ready() {
|
||||
* Will unblock wait_for_protos_ready and incoming messages will be dispatched to these
|
||||
* protocols. You can change the protocol handlers, but you should lock the mutex before
|
||||
* doing so.
|
||||
*/
|
||||
|
||||
void Deci2Server::send_proto_ready(Deci2Driver* drivers, int* driver_count) {
|
||||
lock();
|
||||
d2_drivers = drivers;
|
||||
@@ -214,7 +215,7 @@ void Deci2Server::run() {
|
||||
printf("[DECI2] Warning: no handler for this message, ignoring...\n");
|
||||
unlock();
|
||||
return;
|
||||
// throw std::runtime_error("no handler!");
|
||||
// throw std::exception("no handler!");
|
||||
}
|
||||
|
||||
auto& driver = d2_drivers[handler];
|
||||
@@ -249,16 +250,18 @@ void Deci2Server::run() {
|
||||
|
||||
/*!
|
||||
* Background thread for waiting for the listener.
|
||||
*/
|
||||
|
||||
void Deci2Server::accept_thread_func() {
|
||||
socklen_t l = sizeof(addr);
|
||||
while (!kill_accept_thread) {
|
||||
new_sock = accept(server_fd, (sockaddr*)&addr, &l);
|
||||
if (new_sock >= 0) {
|
||||
u32 versions[2] = {versions::GOAL_VERSION_MAJOR, versions::GOAL_VERSION_MINOR};
|
||||
const char versions = {versions::GOAL_VERSION_MAJOR};
|
||||
send(new_sock, &versions, 8, 0); // todo, check result?
|
||||
server_connected = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user