removing SDL_net

This commit is contained in:
James Turk 2003-08-08 04:22:20 +00:00
parent 6d744d1f45
commit ecd089b489
5 changed files with 1 additions and 641 deletions

View File

@ -13,7 +13,7 @@
\brief Define file for ZEngine where all #define statements to control compilation options are placed.
Definition file, holds #define statements describing optional features of ZEngine.
<br>$Id: ZE_Defines.h,v 1.21 2003/08/08 04:05:22 cozman Exp $<br>
<br>$Id: ZE_Defines.h,v 1.22 2003/08/08 04:24:42 cozman Exp $<br>
\author James Turk
**/
@ -36,13 +36,9 @@
#define USE_SDL_IMAGE
//! Define to include sound support.
#define USE_SDL_MIXER
//! Define to include net support.
//#define USE_SDL_NET
//! Define to use the Physfs file system.
//#define USE_PHYSFS
//! Define to use depreciated code that has not been entirely removed.
//#define DEPRECIATED
#endif
#endif //__ze_defines_h__

View File

@ -1,132 +0,0 @@
/*******************************************************************************
This file is Part of the ZEngine Library for 2D game development.
Copyright (C) 2002, 2003 James Turk
Licensed under a BSD-style license.
The maintainer of this library is James Turk (james@conceptofzero.net)
and the home of this Library is http://www.zengine.sourceforge.net
*******************************************************************************/
/*!
\file ZE_ZClient.h
\brief Definition file for ZClient.
Definition file for ZClient, the TCP Client class for ZEngine.
<br>$Id: ZE_ZClient.h,v 1.6 2003/05/13 01:30:51 cozman Exp $<br>
\author James Turk
**/
#ifndef __ze_zclient_h__
#define __ze_zclient_h__
#include "ZE_ZEngine.h"
#ifdef USE_SDL_NET
namespace ZE
{
/*!
\brief ZEngine class for a simplified TCP client.
ZClient net client class, used to create a simple TCP game client for use with ZServer.
\since 0.8.3
**/
class ZClient
{
protected:
//! Pointer to ZEngine Object.
ZEngine *rEngine;
//! SDL_net socket for connection.
TCPsocket rSocket;
//! Socket set for connection.
SDLNet_SocketSet rSocketSet;
//! Verbose setting for connection logs.
bool rVerbose;
//! Wait timeout setting for recieves on connection. [Defaults to 0.]
int rWaitTime;
public:
/*!
\brief Default Constructor.
Default Constructor, does basic initialization. If verbose is true connection data will be added to log file.
\param verbose Sets verbose setting of client. [Defaults to false.]
**/
ZClient(bool verbose=false);
/*!
\brief Destructor, disconnects and frees memory.
Destructor calls ZClient::Disconnect().
**/
virtual ~ZClient();
/*!
\brief Connects to a server on a given port.
Connects to a server (given in dotted ip form) on a given port.
\param server Dotted IP address to connect to.
\param port Port number to connect to.
\return true if connected, false otherwise.
**/
bool Connect(char *server, Uint16 port);
/*!
\brief Disconnect client.
Disconnect from server if connected.
**/
void Disconnect();
/*!
\brief Sets wait time for recieves.
Sets wait time in milliseconds , time which ZClient::Recieve will wait before returning if there is no data.
Before this is called for the first time WaitTime is 0.
\param wait Wait time.
*/
void SetWaitTime(int wait);
/*!
\brief Send data too connected server.
Sends data to server if available.
\param data Pointer to data buffer to send.
\param size Size of data buffer. (Can't be larger than MAX_MSG_LEN as defined in ZE_Defines.h)
\return True if data could be sent, false upon error.
**/
bool Send(ZByte *data, int size);
/*!
\brief Recieve data if available.
Recieves data from server if available, waiting for timeout period if no data is pending.
\param data Pointer to buffer to store data in. (Must be large enough, needs not be more than MAX_MSG_LEN as defined in ZE_Defines.h)
\return Size of recieved data stored to buffer. (<MAX_MSG_LEN)
**/
int Receive(ZByte *data);
/*!
\brief Return connected state.
Returns state of connection.
\return true if connected, false otherwise
**/
bool Connected();
/*!
\brief Gets current wait time.
Gets amount of time in milliseconds that is specified to wait for data on Recieve.
\return Wait time.
**/
int WaitTime();
};
} //namespace ZE
#endif //USE_SDL_NET
#endif //__ze_zclient_h__

View File

@ -1,134 +0,0 @@
/*******************************************************************************
This file is Part of the ZEngine Library for 2D game development.
Copyright (C) 2002, 2003 James Turk
Licensed under a BSD-style license.
The maintainer of this library is James Turk (james@conceptofzero.net)
and the home of this Library is http://www.zengine.sourceforge.net
*******************************************************************************/
/*!
\file ZE_ZServer.h
\brief Definition file for ZServer.
Definition file for ZServer, the TCP Server class for ZEngine.
<br>$Id: ZE_ZServer.h,v 1.7 2003/05/13 01:30:51 cozman Exp $<br>
\author James Turk
**/
#ifndef __ze_zserver_h__
#define __ze_zserver_h__
#include "ZE_ZEngine.h"
#ifdef USE_SDL_NET
namespace ZE
{
/*!
\brief ZEngine class for simplified TCP server.
ZServer server class, used to create a simple TCP game server for use with ZClient. A very simple turnaround server, all the server does
is accept connections and relay all recieved data to all other clients.
\since 0.8.3
**/
class ZServer
{
private:
//! Pointer to ZEngine Object.
ZEngine *rEngine;
//! SDL_net socket for connection.
TCPsocket rSocket;
//! Socket set for client connections.
SDLNet_SocketSet rSocketSet;
//! SDL_net TCPSockets for client connections.
TCPsocket *rClientSockets;
//! Maximum number of clients.
int rMaxClients;
//! Verbose setting for connection logs.
bool rVerbose;
//! Wait timeout setting for recieves on connection. [Defaults to 0.]
int rWaitTime;
/*!
\brief Closes a socket.
Close a socket given by it's number.
\param num Socket number to close.
**/
void CloseSocket(int num);
public:
/*!
\brief Default Constructor.
Default Constructor, does basic initialization. If verbose is true connection data will be added to log file.
\param verbose Sets verbose setting of client. [Defaults to false.]
**/
ZServer(bool verbose=false);
/*!
\brief Destructor, disconnects server and frees memory.
Destructor calls ZServer::Stop().
**/
virtual ~ZServer();
/*!
\brief Starts server.
Starts server with a number of maximum clients and on a specified port.
\param maxClients Maximum number of clients possible to connect to server.
\param port Port to listen on for connections.
\return True if server started, false otherwise.
**/
bool Start(int maxClients, Uint16 port);
/*!
\brief Stops server.
Closes port and disconnects all clients.
**/
void Stop();
/*!
\brief Sets wait time for recieves.
Sets wait time in milliseconds , time which ZServer::CheckSockets will wait before returning if there is no activity.
Before this is called for the first time the wait time is 0.
\param wait Wait time.
*/
void SetWaitTime(int wait);
/*!
\brief Check sockets for connections or data.
Check sockets for connections or data, accepting new connections or relaying data from existing connections. Waits the specified wait
period. This should be called very often to keep events flowing.
**/
void CheckSockets();
/*!
\brief Gets number of clients.
Finds number of connected clients.
\return Number of clients.
**/
int Clients();
/*!
\brief Gets current wait time.
Gets amount of time in milliseconds that is specified to wait for data on CheckSockets.
\return Wait time.
**/
int WaitTime();
};
} //namespace ZE
#endif //USE_SDL_NET
#endif //__ze_zserver_h__

View File

@ -1,177 +0,0 @@
/*******************************************************************************
This file is Part of the ZEngine Library for 2D game development.
Copyright (C) 2002, 2003 James Turk
Licensed under a BSD-style license.
The maintainer of this library is James Turk (james@conceptofzero.net)
and the home of this Library is http://www.zengine.sourceforge.net
*******************************************************************************/
/**
\file ZE_ZClient.cpp
\brief Source file for ZClient.
Implementation file for ZClient, the TCP Client class for ZEngine.
<br>$Id: ZE_ZClient.cpp,v 1.11 2003/06/16 21:17:22 cozman Exp $<br>
\author James Turk
**/
#include "ZE_ZClient.h"
#ifdef USE_SDL_NET
namespace ZE
{
std::string num2dotted4(unsigned int num)
{
int ip[4];
int index=0;
int d=16777216; //2^24
int m;
//the dotted IP a.b.c.d = a256^3+b256^2+c256+d, this is the numeric ip algorithm in reverse
while(d > 0)
{
m = num/d;
num %= d;
ip[index] = m;
++index;
d /= 256;
}
return FormatStr("%d.%d.%d.%d",ip[3],ip[2],ip[1],ip[0]);
}
ZClient::ZClient(bool verbose) :
rEngine(ZEngine::GetInstance()),
rSocket(NULL),
rSocketSet(NULL),
rVerbose(verbose),
rWaitTime(0)
{
}
ZClient::~ZClient()
{
Disconnect();
}
bool ZClient::Connect(char *server, Uint16 port)
{
IPaddress ip;
//see if the IP is reachable
if(SDLNet_ResolveHost(&ip,server,port) < 0)
rEngine->ReportError(ZERR_NET_CLIENT,FormatStr("Failed to resolve host: %s:%d",server,port));
else if(rVerbose)
rEngine->WriteLog(FormatStr("ZClient: resolved host: %s:%d",server,port));
rEngine->WriteLog(FormatStr("IP: %s",num2dotted4(ip.host).c_str()));
rSocket = SDLNet_TCP_Open(&ip); //try to open rSocket
if(!rSocket)
{
rEngine->ReportError(ZERR_NET_CLIENT,"Failed to open TCP socket.");
Disconnect();
return false;
}
rSocketSet = SDLNet_AllocSocketSet(1); //get a set for the single socket
if(!rSocketSet || SDLNet_TCP_AddSocket(rSocketSet,rSocket) == -1)
{
rEngine->ReportError(ZERR_NET_CLIENT,"Failed to create socket set.");
Disconnect();
return false;
}
return true;
}
void ZClient::Disconnect()
{
if(rSocket)
{
SDLNet_TCP_Close(rSocket);
rSocket = NULL;
}
if(rSocketSet)
{
SDLNet_FreeSocketSet(rSocketSet);
rSocketSet = NULL;
}
}
void ZClient::SetWaitTime(int wait)
{
rWaitTime = wait;
}
bool ZClient::Send(ZByte *data, int size)
{
int sent = 0;
if(rSocket)
{
sent = SDLNet_TCP_Send(rSocket,data,size);
//send < size means that entire packet didn't send.. which is a problem
if(sent < size)
{
rEngine->ReportError(ZERR_NET_CLIENT,FormatStr("Failed to send data, closing socket: %s", SDLNet_GetError()));
Disconnect();
return false;
}
else if(rVerbose)
rEngine->WriteLog(FormatStr("ZClient: Sent %d bytes to server.",sent));
return true;
}
else
{
rEngine->ReportError(ZERR_NOSOCKET,"Send");
return false;
}
}
int ZClient::Receive(ZByte *data)
{
int received = 0;
if(rSocket)
{
//check if data is waiting on the socket
if(SDLNet_CheckSockets(rSocketSet, rWaitTime) > 0 && SDLNet_SocketReady(rSocket))
{
//if recieved 0 (data was waiting) something happened
received = SDLNet_TCP_Recv(rSocket,data,MAX_MSG_LEN);
if(received <= 0)
{
Disconnect();
rEngine->ReportError(ZERR_NET_CLIENT,FormatStr("TCP_Recv failed [%s]",SDLNet_GetError()));
}
else if(rVerbose)
rEngine->WriteLog(FormatStr("ZClient: received %d bytes.",received));
}
}
else
rEngine->ReportError(ZERR_NOSOCKET,"Receive");
return received;
}
bool ZClient::Connected()
{
return rSocket?true:false;
}
int ZClient::WaitTime()
{
return rWaitTime;
}
} //namespace ZE
#endif //USE_SDL_NET

View File

@ -1,193 +0,0 @@
/*******************************************************************************
This file is Part of the ZEngine Library for 2D game development.
Copyright (C) 2002, 2003 James Turk
Licensed under a BSD-style license.
The maintainer of this library is James Turk (james@conceptofzero.net)
and the home of this Library is http://www.zengine.sourceforge.net
*******************************************************************************/
/**
\file ZE_ZServer.cpp
\brief Source file for ZServer.
Implementation file for ZServer, the TCP Server class for ZEngine.
<br>$Id: ZE_ZServer.cpp,v 1.8 2003/07/05 01:11:03 cozman Exp $<br>
\author James Turk
**/
#include "ZE_ZServer.h"
#ifdef USE_SDL_NET
namespace ZE
{
void ZServer::CloseSocket(int num)
{
SDLNet_TCP_Close(rClientSockets[num]);
rClientSockets[num] = NULL; //closed sockets should be NULL to mark them closed
if(rVerbose)
rEngine->WriteLog(FormatStr("Closing socket #%d",num));
}
ZServer::ZServer(bool verbose) :
rEngine(ZEngine::GetInstance()),
rSocket(NULL), rSocketSet(NULL), rClientSockets(NULL),
rMaxClients(0), rVerbose(verbose), rWaitTime(0)
{
}
ZServer::~ZServer()
{
Stop();
}
bool ZServer::Start(int maxClients, Uint16 port)
{
IPaddress ip;
rMaxClients = maxClients;
rClientSockets = new TCPsocket[rMaxClients];
rSocketSet = SDLNet_AllocSocketSet(rMaxClients+1); //need one more (for server socket)
if(!rSocketSet)
{
rEngine->ReportError(ZERR_NET_SERVER,FormatStr("Failed to create sockets (%s).",SDLNet_GetError()));
Stop();
return false;
}
else if(rVerbose)
rEngine->WriteLog(FormatStr("ZServer: Created server with %d available sockets.",rMaxClients));
for(int i=0; i < rMaxClients; ++i)
rClientSockets[i] = NULL;
if(SDLNet_ResolveHost(&ip,NULL,port) < 0) //try to resolve the host
rEngine->ReportError(ZERR_NET_SERVER,FormatStr("Failed to create host server on port %d (%s).",port,SDLNet_GetError()));
else if(rVerbose)
rEngine->WriteLog(FormatStr("ZServer: Created host server on port %d.",port));
rSocket = SDLNet_TCP_Open(&ip); //try to open socket
if(!rSocket || SDLNet_TCP_AddSocket(rSocketSet,rSocket) == -1)
{
rEngine->ReportError(ZERR_NET_SERVER,"Failed to open server TCP socket.");
Stop();
return false;
}
return true;
}
void ZServer::Stop()
{
if(rSocket)
{
SDLNet_TCP_Close(rSocket);
rSocket = NULL;
}
if(rSocketSet)
{
SDLNet_FreeSocketSet(rSocketSet);
rSocketSet = NULL;
}
//each client must be let go, old memory leak
if(rClientSockets)
{
for(int i=0; i < rMaxClients; ++i)
{
if(rClientSockets[i])
{
CloseSocket(i);
}
}
delete[] rClientSockets;
}
rMaxClients = 0;
}
void ZServer::SetWaitTime(int wait)
{
rWaitTime = wait;
}
void ZServer::CheckSockets()
{
ZByte buf[MAX_MSG_LEN];
int result,size,count;
SDLNet_CheckSockets(rSocketSet, rWaitTime);
if(SDLNet_SocketReady(rSocket)) //new client
{
for(count=0; count < rMaxClients; ++count) //find first open socket
{
if(!rClientSockets[count])
break;
}
if(rClientSockets[count]) //if it exists the set is full
{
rEngine->ReportError(ZERR_NET_SERVER,FormatStr("All %d ports full.",rMaxClients));
}
else
{
rClientSockets[count] = SDLNet_TCP_Accept(rSocket);
SDLNet_TCP_AddSocket(rSocketSet, rClientSockets[count]);
if(rVerbose)
rEngine->WriteLog(FormatStr("ZServer: Adding socket #%d.",count+1));
}
}
//check all sockets for activity//
for(int i=0; i < rMaxClients; ++i)
{
if(SDLNet_SocketReady(rClientSockets[i])) //incoming message
{
result = SDLNet_TCP_Recv(rClientSockets[i], buf, MAX_MSG_LEN);
if(result <= 0) //disconnect bad sockets
CloseSocket(i);
else if(rVerbose)
rEngine->WriteLog(FormatStr("ZServer: Received %d bytes on Port %d",result,i));
size = result;
if(rVerbose)
rEngine->WriteLog("ZServer: Mirroring data: ");
for(int j=0; j < rMaxClients; ++j)
{
if(rClientSockets[j] && i != j) //send to open sockets that aren't the same
{
result = SDLNet_TCP_Send(rClientSockets[j],buf,size);
if(rVerbose)
rEngine->WriteLog(FormatStr(" +%d bytes on socket %d.",size,j));
}
}
}
}
}
int ZServer::Clients()
{
int numClients=0;
for(int i=0; i < rMaxClients; ++i)
{
if(rClientSockets[i])
++numClients;
}
return numClients;
}
int ZServer::WaitTime()
{
return rWaitTime;
}
} //namespace ZE
#endif //USE_SDL_NET