Commit 5421b4d6 authored by alistert's avatar alistert

Made networking optional at compile time.

parent 943294c8
/* /*
* *
* network.cpp * network.cpp
* *
* 3rd June 2009: Created network.cpp from parts of game.cpp * 3rd June 2009: Created network.cpp from parts of game.cpp
* *
* 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
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* *
*/ */
/*
#include "controls.h" * Deals with a platform-specific networking API.
#include "gfx/font.h" *
#include "gfx/video.h" * On most platforms, USE_SOCKETS should be defined.
#include "network.h" *
#ifdef USE_SDL_NET */
#include <arpa/inet.h>
#else
#ifdef WIN32 #include "controls.h"
#include <winsock.h> #include "gfx/font.h"
#define ioctl ioctlsocket #include "gfx/video.h"
#define socklen_t int #include "network.h"
#define EWOULDBLOCK WSAEWOULDBLOCK
#define MSG_NOSIGNAL 0 #ifdef USE_SOCKETS
#else #ifdef WIN32
#include <sys/types.h> #include <winsock.h>
#include <sys/socket.h> #define ioctl ioctlsocket
#include <arpa/inet.h> #define socklen_t int
#include <sys/ioctl.h> #define EWOULDBLOCK WSAEWOULDBLOCK
#include <unistd.h> #define MSG_NOSIGNAL 0
#include <errno.h> #else
#endif #include <sys/types.h>
#endif #include <sys/socket.h>
#include <arpa/inet.h>
Network::Network () { #include <sys/ioctl.h>
#ifdef USE_SDL_NET #include <unistd.h>
SDLNet_Init(); #include <errno.h>
#else #include <string.h>
#ifdef WIN32 #endif
WSADATA WSAData; #elif defined USE_SDL_NET
#include <arpa/inet.h>
// Start Windows Sockets #endif
WSAStartup(MAKEWORD(1, 0), &WSAData);
#endif
#endif Network::Network () {
return;
#ifdef USE_SOCKETS
} #ifdef WIN32
WSADATA WSAData;
Network::~Network () {
#ifdef USE_SDL_NET // Start Windows Sockets
SDLNet_Quit(); WSAStartup(MAKEWORD(1, 0), &WSAData);
#else #endif
#elif defined USE_SDL_NET
#ifdef WIN32 SDLNet_Init();
// Shut down Windows Sockets #endif
WSACleanup();
#endif return;
#endif
return; }
} Network::~Network () {
int Network::host () { #ifdef USE_SOCKETS
#ifdef USE_SDL_NET #ifdef WIN32
ipAddress.port = NET_PORT; // Shut down Windows Sockets
ipAddress.host = 0; WSACleanup();
socket = SDLNet_TCP_Open(&ipAddress); #endif
#elif defined USE_SDL_NET
if(socket == NULL) SDLNet_Quit();
return E_N_SOCKET; #endif
return (int) socket; return;
#else
sockaddr_in sockAddr; }
int sock, nonblock;
int Network::host () {
sock = socket(AF_INET, SOCK_STREAM, 0); #ifdef USE_SOCKETS
sockaddr_in sockAddr;
if (sock == -1) return E_N_SOCKET; int sock, nonblock;
// Make the socket non-blocking sock = socket(AF_INET, SOCK_STREAM, 0);
nonblock = 1;
ioctl(sock, FIONBIO, (u_long *)&nonblock); if (sock == -1) return E_N_SOCKET;
memset(&sockAddr, 0, sizeof(sockaddr_in));
sockAddr.sin_family = AF_INET; // Make the socket non-blocking
sockAddr.sin_addr.s_addr = INADDR_ANY; nonblock = 1;
sockAddr.sin_port = htons(NET_PORT); ioctl(sock, FIONBIO, (u_long *)&nonblock);
memset(&sockAddr, 0, sizeof(sockaddr_in));
if (bind(sock, (sockaddr *)&sockAddr, sizeof(sockaddr_in))) { sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = INADDR_ANY;
close(sock); sockAddr.sin_port = htons(NET_PORT);
return E_N_BIND;
if (bind(sock, (sockaddr *)&sockAddr, sizeof(sockaddr_in))) {
}
close(sock);
if (listen(sock, MAX_CLIENTS) == -1) {
return E_N_BIND;
close(sock);
}
return E_N_LISTEN;
if (listen(sock, MAX_CLIENTS) == -1) {
}
close(sock);
return sock;
#endif return E_N_LISTEN;
} }
int Network::join (char *address) { return sock;
#ifdef USE_SDL_NET #elif defined USE_SDL_NET
clearScreen(0); ipAddress.port = NET_PORT;
fontmn2->showString("CONNECTING TO SERVER", screenW >> 2, ipAddress.host = 0;
(screenH >> 1) - 16); socket = SDLNet_TCP_Open(&ipAddress);
loop(NORMAL_LOOP);
ipAddress.port = NET_PORT; if (socket == NULL) return E_N_SOCKET;
ipAddress.host = inet_addr(address);
socket = SDLNet_TCP_Open(&ipAddress); return (int)socket;
if(socket == NULL) #else
return -1; return E_N_OTHER;
#endif
return (int) socket;
#else }
sockaddr_in sockAddr;
fd_set writefds; int Network::join (char *address) {
timeval timeouttv;
unsigned int timeout; #ifdef USE_SOCKETS
int sock, con; sockaddr_in sockAddr;
fd_set writefds;
// Create socket timeval timeouttv;
unsigned int timeout;
sock = socket(AF_INET, SOCK_STREAM, 0); int sock, con;
if (sock == -1) return E_N_SOCKET; // Create socket
// Make socket non-blocking sock = socket(AF_INET, SOCK_STREAM, 0);
con = 1;
ioctl(sock, FIONBIO, (u_long *)&con); if (sock == -1) return E_N_SOCKET;
// Make socket non-blocking
// Connect to server con = 1;
ioctl(sock, FIONBIO, (u_long *)&con);
memset(&sockAddr, 0, sizeof(sockaddr_in));
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(NET_PORT); // Connect to server
#ifdef WIN32 memset(&sockAddr, 0, sizeof(sockaddr_in));
sockAddr.sin_addr.s_addr = inet_addr(address); sockAddr.sin_family = AF_INET;
#else sockAddr.sin_port = htons(NET_PORT);
if (inet_aton(address, &(sockAddr.sin_addr)) == 0) return E_N_ADDRESS;
#endif #ifdef WIN32
sockAddr.sin_addr.s_addr = inet_addr(address);
// Initiate connection #else
con = connect(sock, (sockaddr *)&sockAddr, sizeof(sockAddr)); if (inet_aton(address, &(sockAddr.sin_addr)) == 0) return E_N_ADDRESS;
#endif
// If the connection completed, return
if (con == 0) return sock; // Initiate connection
con = connect(sock, (sockaddr *)&sockAddr, sizeof(sockAddr));
// Wait for connection to complete // If the connection completed, return
if (con == 0) return sock;
con = 0;
timeout = globalTicks + T_TIMEOUT;
// Wait for connection to complete
while (!con) {
con = 0;
if (loop(NORMAL_LOOP) == E_QUIT) { timeout = globalTicks + T_TIMEOUT;
close(sock); while (!con) {
return E_QUIT; if (loop(NORMAL_LOOP) == E_QUIT) {
} close(sock);
if (controls.release(C_ESCAPE)) { return E_QUIT;
close(sock); }
return E_UNUSED; if (controls.release(C_ESCAPE)) {
} close(sock);
clearScreen(0); return E_UNUSED;
fontmn2->showString("CONNECTING TO SERVER", screenW >> 2,
(screenH >> 1) - 16); }
FD_ZERO(&writefds); clearScreen(0);
FD_SET(sock, &writefds); fontmn2->showString("CONNECTING TO SERVER", screenW >> 2,
timeouttv.tv_sec = 0; (screenH >> 1) - 16);
timeouttv.tv_usec = T_FRAME;
con = select(sock + 1, NULL, &writefds, NULL, &timeouttv); FD_ZERO(&writefds);
FD_SET(sock, &writefds);
if (con == -1) { timeouttv.tv_sec = 0;
timeouttv.tv_usec = T_FRAME;
log("Could not connect to server - code", getError()); con = select(sock + 1, NULL, &writefds, NULL, &timeouttv);
close(sock); if (con == -1) {
return E_N_CONNECT; log("Could not connect to server - code", getError());
} close(sock);
if (globalTicks > timeout) { return E_N_CONNECT;
close(sock); }
return E_TIMEOUT; if (globalTicks > timeout) {
} close(sock);
} return E_TIMEOUT;
return sock; }
#endif
}
}
return sock;
int Network::accept (int sock) { #elif defined USE_SDL_NET
#ifdef USE_SDL_NET clearScreen(0);
clientSocket = SDLNet_TCP_Accept((TCPsocket)sock); fontmn2->showString("CONNECTING TO SERVER", screenW >> 2,
if(clientSocket == NULL) (screenH >> 1) - 16);
return -1; loop(NORMAL_LOOP);
return (int) &clientSocket; ipAddress.port = NET_PORT;
#else ipAddress.host = inet_addr(address);
sockaddr_in sockAddr; socket = SDLNet_TCP_Open(&ipAddress);
int clientSocket, length;
if (socket == NULL) return -1;
length = sizeof(sockaddr_in);
return (int)socket;
clientSocket = ::accept(sock, (sockaddr *)&sockAddr, (socklen_t *)&length); #else
return E_N_OTHER;
if (clientSocket != -1) { #endif
// Make the socket non-blocking }
length = 1;
ioctl(clientSocket, FIONBIO, (u_long *)&length); int Network::accept (int sock) {
} #ifdef USE_SOCKETS
sockaddr_in sockAddr;
return clientSocket; int clientSocket, length;
#endif
} length = sizeof(sockaddr_in);
void Network::close (int sock) { clientSocket = ::accept(sock, (sockaddr *)&sockAddr, (socklen_t *)&length);
#ifdef USE_SDL_NET
SDLNet_TCP_Close((TCPsocket)sock); if (clientSocket != -1) {
#else
// Make the socket non-blocking
#ifdef WIN32 length = 1;
closesocket(sock); ioctl(clientSocket, FIONBIO, (u_long *)&length);
#else
::close(sock); }
#endif
#endif return clientSocket;
return; #elif defined USE_SDL_NET
clientSocket = SDLNet_TCP_Accept((TCPsocket)sock);
}
if (clientSocket == NULL) return -1;
int Network::send (int sock, unsigned char *buffer) {
#ifdef USE_SDL_NET return (int)&clientSocket;
return SDLNet_TCP_Send((TCPsocket)sock, (char *)buffer, buffer[0]); #else
#else return -1;
return ::send(sock, (char *)buffer, buffer[0], MSG_NOSIGNAL); #endif
#endif
}
}
void Network::close (int sock) {
int Network::recv (int sock, unsigned char *buffer, int length) {
#ifdef USE_SDL_NET #ifdef USE_SOCKETS
return SDLNet_TCP_Recv((TCPsocket)sock, buffer, length); #ifdef WIN32
#else closesocket(sock);
return ::recv(sock, (char *)buffer, length, MSG_NOSIGNAL); #else
#endif ::close(sock);
} #endif
#elif defined USE_SDL_NET
bool Network::isConnected (int sock) { SDLNet_TCP_Close((TCPsocket)sock);
#ifdef USE_SDL_NET #endif
return SDLNet_SocketReady((TCPsocket) sock);
#else return;
int length;
char buffer; }
// Check for incoming data int Network::send (int sock, unsigned char *buffer) {
length = ::recv(sock, &buffer, 1, MSG_PEEK | MSG_NOSIGNAL);
#ifdef USE_SOCKETS
// Still connected if data was received or if there was no data to receive return ::send(sock, (char *)buffer, buffer[0], MSG_NOSIGNAL);
return (length != -1) || (getError() == EWOULDBLOCK); #elif defined USE_SDL_NET
#endif return SDLNet_TCP_Send((TCPsocket)sock, (char *)buffer, buffer[0]);
} #else
return 0;
#endif
int Network::getError () {
#ifdef USE_SDL_NET }
return (int) SDLNet_GetError();
#else int Network::recv (int sock, unsigned char *buffer, int length) {
#ifdef WIN32 #ifdef USE_SOCKETS
return WSAGetLastError(); return ::recv(sock, (char *)buffer, length, MSG_NOSIGNAL);
#else #elif defined USE_SDL_NET
return errno; return SDLNet_TCP_Recv((TCPsocket)sock, buffer, length);
#endif #else
#endif return 0;
} #endif
}
bool Network::isConnected (int sock) {
#ifdef USE_SOCKETS
int length;
char buffer;
// Check for incoming data
length = ::recv(sock, &buffer, 1, MSG_PEEK | MSG_NOSIGNAL);
// Still connected if data was received or if there was no data to receive
return (length != -1) || (getError() == EWOULDBLOCK);
#elif defined USE_SDL_NET
return SDLNet_SocketReady((TCPsocket)sock);
#else
return false;
#endif
}
int Network::getError () {
#ifdef USE_SOCKETS
#ifdef WIN32
return WSAGetLastError();
#else
return errno;
#endif
#elif defined USE_SDL_NET
return (int)SDLNet_GetError();
#else
return 0;
#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