Commit 630b578a authored by anotherguest's avatar anotherguest

Initial SDL_Net support. Not tested yet though!

parent 5a60ec98
......@@ -24,7 +24,9 @@
#include "gfx/font.h"
#include "gfx/video.h"
#include "network.h"
#ifdef USE_SDL_NET
#include <arpa/inet.h>
#else
#ifdef WIN32
#include <winsock.h>
#define ioctl ioctlsocket
......@@ -39,33 +41,48 @@
#include <unistd.h>
#include <errno.h>
#endif
#endif
Network::Network () {
#ifdef USE_SDL_NET
SDLNet_Init();
#else
#ifdef WIN32
WSADATA WSAData;
// Start Windows Sockets
WSAStartup(MAKEWORD(1, 0), &WSAData);
#endif
#endif
return;
}
Network::~Network () {
#ifdef USE_SDL_NET
SDLNet_Quit();
#else
#ifdef WIN32
// Shut down Windows Sockets
WSACleanup();
#endif
#endif
return;
}
int Network::host () {
#ifdef USE_SDL_NET
ipAddress.port = NET_PORT;
ipAddress.host = 0;
socket = SDLNet_TCP_Open(&ipAddress);
if(socket == NULL)
return E_N_SOCKET;
return (int) socket;
#else
sockaddr_in sockAddr;
int sock, nonblock;
......@@ -102,11 +119,24 @@ int Network::host () {
}
return sock;
#endif
}
int Network::join (char *address) {
#ifdef USE_SDL_NET
clearScreen(0);
fontmn2->showString("CONNECTING TO SERVER", screenW >> 2,
(screenH >> 1) - 16);
loop(NORMAL_LOOP);
ipAddress.port = NET_PORT;
ipAddress.host = inet_addr(address);
socket = SDLNet_TCP_Open(&ipAddress);
if(socket == NULL)
return -1;
return (int) socket;
#else
sockaddr_in sockAddr;
fd_set writefds;
timeval timeouttv;
......@@ -197,11 +227,17 @@ int Network::join (char *address) {
}
return sock;
#endif
}
int Network::accept (int sock) {
#ifdef USE_SDL_NET
clientSocket = SDLNet_TCP_Accept((TCPsocket)sock);
if(clientSocket == NULL)
return -1;
return (int) &clientSocket;
#else
sockaddr_in sockAddr;
int clientSocket, length;
......@@ -218,35 +254,45 @@ int Network::accept (int sock) {
}
return clientSocket;
#endif
}
void Network::close (int sock) {
#ifdef USE_SDL_NET
SDLNet_TCP_Close((TCPsocket)sock);
#else
#ifdef WIN32
closesocket(sock);
#else
::close(sock);
#endif
#endif
return;
}
int Network::send (int sock, unsigned char *buffer) {
#ifdef USE_SDL_NET
return SDLNet_TCP_Send((TCPsocket)sock, (char *)buffer, buffer[0]);
#else
return ::send(sock, (char *)buffer, buffer[0], MSG_NOSIGNAL);
#endif
}
int Network::recv (int sock, unsigned char *buffer, int length) {
#ifdef USE_SDL_NET
return SDLNet_TCP_Recv((TCPsocket)sock, buffer, length);
#else
return ::recv(sock, (char *)buffer, length, MSG_NOSIGNAL);
#endif
}
bool Network::isConnected (int sock) {
#ifdef USE_SDL_NET
return SDLNet_SocketReady((TCPsocket) sock);
#else
int length;
char buffer;
......@@ -255,18 +301,21 @@ bool Network::isConnected (int sock) {
// Still connected if data was received or if there was no data to receive
return (length != -1) || (getError() == EWOULDBLOCK);
#endif
}
int Network::getError () {
#ifdef USE_SDL_NET
return (int) SDLNet_GetError();
#else
#ifdef WIN32
return WSAGetLastError();
#else
return errno;
#endif
#endif
}
......@@ -24,7 +24,9 @@
#include "OpenJazz.h"
#ifdef USE_SDL_NET
#include <SDL_net.h>
#endif
// Constants
......@@ -55,6 +57,12 @@ class Network {
int recv (int sock, unsigned char *buffer, int length);
bool isConnected (int sock);
int getError ();
#ifdef USE_SDL_NET
TCPsocket socket;
TCPsocket clientSocket;
IPaddress ipAddress;
SDLNet_SocketSet socketset;
#endif
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment