Commit 5421b4d6 authored by alistert's avatar alistert

Made networking optional at compile time.

parent 943294c8
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Part of the OpenJazz project * Part of the OpenJazz project
* *
* *
* Copyright (c) 2005-2009 Alister Thomson * Copyright (c) 2005-2010 Alister Thomson
* *
* OpenJazz is distributed under the terms of * OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0 * the GNU General Public License, version 2.0
...@@ -19,70 +19,75 @@ ...@@ -19,70 +19,75 @@
* *
*/ */
/*
* Deals with a platform-specific networking API.
*
* On most platforms, USE_SOCKETS should be defined.
*
*/
#include "controls.h" #include "controls.h"
#include "gfx/font.h" #include "gfx/font.h"
#include "gfx/video.h" #include "gfx/video.h"
#include "network.h" #include "network.h"
#ifdef USE_SDL_NET
#include <arpa/inet.h> #ifdef USE_SOCKETS
#else #ifdef WIN32
#ifdef WIN32
#include <winsock.h> #include <winsock.h>
#define ioctl ioctlsocket #define ioctl ioctlsocket
#define socklen_t int #define socklen_t int
#define EWOULDBLOCK WSAEWOULDBLOCK #define EWOULDBLOCK WSAEWOULDBLOCK
#define MSG_NOSIGNAL 0 #define MSG_NOSIGNAL 0
#else #else
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <string.h>
#endif
#elif defined USE_SDL_NET
#include <arpa/inet.h>
#endif #endif
#endif
Network::Network () { Network::Network () {
#ifdef USE_SDL_NET
SDLNet_Init(); #ifdef USE_SOCKETS
#else #ifdef WIN32
#ifdef WIN32
WSADATA WSAData; WSADATA WSAData;
// Start Windows Sockets // Start Windows Sockets
WSAStartup(MAKEWORD(1, 0), &WSAData); WSAStartup(MAKEWORD(1, 0), &WSAData);
#endif
#elif defined USE_SDL_NET
SDLNet_Init();
#endif #endif
#endif
return; return;
} }
Network::~Network () { Network::~Network () {
#ifdef USE_SDL_NET
SDLNet_Quit();
#else
#ifdef WIN32 #ifdef USE_SOCKETS
#ifdef WIN32
// Shut down Windows Sockets // Shut down Windows Sockets
WSACleanup(); WSACleanup();
#endif
#elif defined USE_SDL_NET
SDLNet_Quit();
#endif #endif
#endif
return; return;
} }
int Network::host () { int Network::host () {
#ifdef USE_SDL_NET
ipAddress.port = NET_PORT;
ipAddress.host = 0;
socket = SDLNet_TCP_Open(&ipAddress);
if(socket == NULL) #ifdef USE_SOCKETS
return E_N_SOCKET;
return (int) socket;
#else
sockaddr_in sockAddr; sockaddr_in sockAddr;
int sock, nonblock; int sock, nonblock;
...@@ -119,24 +124,23 @@ int Network::host () { ...@@ -119,24 +124,23 @@ int Network::host () {
} }
return sock; return sock;
#elif defined 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
return E_N_OTHER;
#endif #endif
} }
int Network::join (char *address) { 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; #ifdef USE_SOCKETS
#else
sockaddr_in sockAddr; sockaddr_in sockAddr;
fd_set writefds; fd_set writefds;
timeval timeouttv; timeval timeouttv;
...@@ -160,11 +164,11 @@ int Network::join (char *address) { ...@@ -160,11 +164,11 @@ int Network::join (char *address) {
sockAddr.sin_family = AF_INET; sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(NET_PORT); sockAddr.sin_port = htons(NET_PORT);
#ifdef WIN32 #ifdef WIN32
sockAddr.sin_addr.s_addr = inet_addr(address); sockAddr.sin_addr.s_addr = inet_addr(address);
#else #else
if (inet_aton(address, &(sockAddr.sin_addr)) == 0) return E_N_ADDRESS; if (inet_aton(address, &(sockAddr.sin_addr)) == 0) return E_N_ADDRESS;
#endif #endif
// Initiate connection // Initiate connection
con = connect(sock, (sockaddr *)&sockAddr, sizeof(sockAddr)); con = connect(sock, (sockaddr *)&sockAddr, sizeof(sockAddr));
...@@ -227,17 +231,27 @@ int Network::join (char *address) { ...@@ -227,17 +231,27 @@ int Network::join (char *address) {
} }
return sock; return sock;
#elif defined 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
return E_N_OTHER;
#endif #endif
} }
int Network::accept (int sock) { int Network::accept (int sock) {
#ifdef USE_SDL_NET
clientSocket = SDLNet_TCP_Accept((TCPsocket)sock); #ifdef USE_SOCKETS
if(clientSocket == NULL)
return -1;
return (int) &clientSocket;
#else
sockaddr_in sockAddr; sockaddr_in sockAddr;
int clientSocket, length; int clientSocket, length;
...@@ -254,45 +268,61 @@ int Network::accept (int sock) { ...@@ -254,45 +268,61 @@ int Network::accept (int sock) {
} }
return clientSocket; return clientSocket;
#elif defined USE_SDL_NET
clientSocket = SDLNet_TCP_Accept((TCPsocket)sock);
if (clientSocket == NULL) return -1;
return (int)&clientSocket;
#else
return -1;
#endif #endif
} }
void Network::close (int sock) { void Network::close (int sock) {
#ifdef USE_SDL_NET
SDLNet_TCP_Close((TCPsocket)sock);
#else
#ifdef WIN32 #ifdef USE_SOCKETS
#ifdef WIN32
closesocket(sock); closesocket(sock);
#else #else
::close(sock); ::close(sock);
#endif
#elif defined USE_SDL_NET
SDLNet_TCP_Close((TCPsocket)sock);
#endif #endif
#endif
return; return;
} }
int Network::send (int sock, unsigned char *buffer) { int Network::send (int sock, unsigned char *buffer) {
#ifdef USE_SDL_NET
#ifdef USE_SOCKETS
return ::send(sock, (char *)buffer, buffer[0], MSG_NOSIGNAL);
#elif defined USE_SDL_NET
return SDLNet_TCP_Send((TCPsocket)sock, (char *)buffer, buffer[0]); return SDLNet_TCP_Send((TCPsocket)sock, (char *)buffer, buffer[0]);
#else #else
return ::send(sock, (char *)buffer, buffer[0], MSG_NOSIGNAL); return 0;
#endif #endif
} }
int Network::recv (int sock, unsigned char *buffer, int length) { int Network::recv (int sock, unsigned char *buffer, int length) {
#ifdef USE_SDL_NET
#ifdef USE_SOCKETS
return ::recv(sock, (char *)buffer, length, MSG_NOSIGNAL);
#elif defined USE_SDL_NET
return SDLNet_TCP_Recv((TCPsocket)sock, buffer, length); return SDLNet_TCP_Recv((TCPsocket)sock, buffer, length);
#else #else
return ::recv(sock, (char *)buffer, length, MSG_NOSIGNAL); return 0;
#endif #endif
} }
bool Network::isConnected (int sock) { bool Network::isConnected (int sock) {
#ifdef USE_SDL_NET
return SDLNet_SocketReady((TCPsocket) sock); #ifdef USE_SOCKETS
#else
int length; int length;
char buffer; char buffer;
...@@ -301,21 +331,29 @@ bool Network::isConnected (int sock) { ...@@ -301,21 +331,29 @@ bool Network::isConnected (int sock) {
// Still connected if data was received or if there was no data to receive // Still connected if data was received or if there was no data to receive
return (length != -1) || (getError() == EWOULDBLOCK); return (length != -1) || (getError() == EWOULDBLOCK);
#elif defined USE_SDL_NET
return SDLNet_SocketReady((TCPsocket)sock);
#else
return false;
#endif #endif
} }
int Network::getError () { int Network::getError () {
#ifdef USE_SDL_NET
return (int) SDLNet_GetError();
#else
#ifdef WIN32 #ifdef USE_SOCKETS
#ifdef WIN32
return WSAGetLastError(); return WSAGetLastError();
#else #else
return errno; return errno;
#endif
#elif defined USE_SDL_NET
return (int)SDLNet_GetError();
#else
return 0;
#endif #endif
#endif
} }
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Part of the OpenJazz project * Part of the OpenJazz project
* *
* *
* Copyright (c) 2005-2009 Alister Thomson * Copyright (c) 2005-2010 Alister Thomson
* *
* OpenJazz is distributed under the terms of * OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0 * the GNU General Public License, version 2.0
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "OpenJazz.h" #include "OpenJazz.h"
#ifdef USE_SDL_NET #ifdef USE_SDL_NET
#include <SDL_net.h> #include <SDL_net.h>
#endif #endif
...@@ -46,6 +47,13 @@ ...@@ -46,6 +47,13 @@
class Network { class Network {
public: public:
#ifdef USE_SDL_NET
TCPsocket socket;
TCPsocket clientSocket;
IPaddress ipAddress;
SDLNet_SocketSet socketset;
#endif
Network (); Network ();
~Network (); ~Network ();
...@@ -57,12 +65,6 @@ class Network { ...@@ -57,12 +65,6 @@ class Network {
int recv (int sock, unsigned char *buffer, int length); int recv (int sock, unsigned char *buffer, int length);
bool isConnected (int sock); bool isConnected (int sock);
int getError (); int getError ();
#ifdef USE_SDL_NET
TCPsocket socket;
TCPsocket clientSocket;
IPaddress ipAddress;
SDLNet_SocketSet socketset;
#endif
}; };
......
...@@ -63,18 +63,20 @@ int loadMain (int argc, char *argv[]) { ...@@ -63,18 +63,20 @@ int loadMain (int argc, char *argv[]) {
// Determine paths // Determine paths
// Use the hard-coded path, if available // Use hard-coded paths, if available
#ifdef DATAPATH #ifdef DATAPATH
firstPath = new Path(NULL, createString(DATAPATH)); firstPath = new Path(NULL, createString(DATAPATH));
#elseifdef __SYMBIAN32__ #else
firstPath = NULL;
#endif
#ifdef __SYMBIAN32__
#ifdef UIQ3 #ifdef UIQ3
firstPath = new Path(NULL, createString("c:\\shared\\openjazz\\")); firstPath = new Path(firstPath, createString("c:\\shared\\openjazz\\"));
#else #else
firstPath = new Path(NULL, createString("c:\\data\\openjazz\\")); firstPath = new Path(firstPath, createString("c:\\data\\openjazz\\"));
#endif #endif
#else
firstPath = NULL;
#endif #endif
......
...@@ -40,13 +40,15 @@ ...@@ -40,13 +40,15 @@
int Menu::main () { int Menu::main () {
#if (defined USE_SOCKETS) || (defined USE_SDL_NET)
const char *newGameOptions[6] = {"new single player game", "new co-op game", const char *newGameOptions[6] = {"new single player game", "new co-op game",
"new battle", "new team battle", "new race", "join game"}; "new battle", "new team battle", "new race", "join game"};
int ret;
#endif
Scene *scene; Scene *scene;
SDL_Rect src, dst; SDL_Rect src, dst;
int option, suboption; int option, suboption;
unsigned int idleTime; unsigned int idleTime;
int ret;
option = suboption = 0; option = suboption = 0;
...@@ -73,6 +75,7 @@ int Menu::main () { ...@@ -73,6 +75,7 @@ int Menu::main () {
case 0: // New game case 0: // New game
#if (defined USE_SOCKETS) || (defined USE_SDL_NET)
while (true) { while (true) {
ret = generic(newGameOptions, 6, &suboption); ret = generic(newGameOptions, 6, &suboption);
...@@ -92,6 +95,9 @@ int Menu::main () { ...@@ -92,6 +95,9 @@ int Menu::main () {
} }
} }
#else
if (newGameEpisode(suboption) == E_QUIT) return E_QUIT;
#endif
break; break;
......
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