net code imported

This commit is contained in:
James Turk 2003-03-14 22:18:14 +00:00
parent b7c9226183
commit bd4c6731a8
4 changed files with 266 additions and 2 deletions

131
include/ZE_ZClient.h Executable file
View File

@ -0,0 +1,131 @@
/*******************************************************************************
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
*******************************************************************************/
/*!
\par File Header:
File: ZE_ZClient.h <br>
Description: Header file for core ZEngine TCP Client Object. <br>
Author(s): James Turk <br>
$Id: ZE_ZClient.h,v 1.1 2003/03/14 22:18:14 cozman Exp $<br>
\file ZE_ZClient.h
\brief Definition file for ZClient.
Definition file for ZClient, the TCP Client class for ZEngine.
**/
#ifndef __ze_zclient_h__
#define __ze_zclient_h__
#include "ZE_ZEngine.h"
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.
**/
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 //__ze_zclient_h__

133
include/ZE_ZServer.h Executable file
View File

@ -0,0 +1,133 @@
/*******************************************************************************
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
*******************************************************************************/
/*!
\par File Header:
File: ZE_ZServer.h <br>
Description: Header file for core ZEngine TCP Server Object. <br>
Author(s): James Turk <br>
$Id: ZE_ZServer.h,v 1.1 2003/03/14 22:18:14 cozman Exp $<br>
\file ZE_ZServer.h
\brief Definition file for ZServer.
Definition file for ZServer, the TCP Server class for ZEngine.
**/
#ifndef __ze_zserver_h__
#define __ze_zserver_h__
#include "ZE_ZEngine.h"
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.
**/
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 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 //__ze_zserver_h__

View File

@ -13,7 +13,7 @@
File: ZE_ZClient.cpp <br> File: ZE_ZClient.cpp <br>
Description: Implementation source file for core ZEngine TCP Client Object. <br> Description: Implementation source file for core ZEngine TCP Client Object. <br>
Author(s): James Turk <br> Author(s): James Turk <br>
$Id:<br> $Id: ZE_ZClient.cpp,v 1.2 2003/03/14 22:20:13 cozman Exp $<br>
\file ZE_ZClient.cpp \file ZE_ZClient.cpp
\brief Source file for ZClient. \brief Source file for ZClient.

View File

@ -13,7 +13,7 @@
File: ZE_ZServer.cpp <br> File: ZE_ZServer.cpp <br>
Description: Implementation source file for core ZEngine TCP Server Object. <br> Description: Implementation source file for core ZEngine TCP Server Object. <br>
Author(s): James Turk <br> Author(s): James Turk <br>
$Id:<br> $Id: ZE_ZServer.cpp,v 1.2 2003/03/14 22:20:12 cozman Exp $<br>
\file ZE_ZServer.cpp \file ZE_ZServer.cpp
\brief Source file for ZServer. \brief Source file for ZServer.