From bd4c6731a8431731837758691de61edd657ee827 Mon Sep 17 00:00:00 2001 From: James Turk Date: Fri, 14 Mar 2003 22:18:14 +0000 Subject: [PATCH] net code imported --- include/ZE_ZClient.h | 131 ++++++++++++++++++++++++++++++++++++++++++ include/ZE_ZServer.h | 133 +++++++++++++++++++++++++++++++++++++++++++ src/ZE_ZClient.cpp | 2 +- src/ZE_ZServer.cpp | 2 +- 4 files changed, 266 insertions(+), 2 deletions(-) create mode 100755 include/ZE_ZClient.h create mode 100755 include/ZE_ZServer.h diff --git a/include/ZE_ZClient.h b/include/ZE_ZClient.h new file mode 100755 index 0000000..38dc708 --- /dev/null +++ b/include/ZE_ZClient.h @@ -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
+Description: Header file for core ZEngine TCP Client Object.
+Author(s): James Turk
+$Id: ZE_ZClient.h,v 1.1 2003/03/14 22:18:14 cozman Exp $
+ + \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. ( +Description: Header file for core ZEngine TCP Server Object.
+Author(s): James Turk
+$Id: ZE_ZServer.h,v 1.1 2003/03/14 22:18:14 cozman Exp $
+ + \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__ diff --git a/src/ZE_ZClient.cpp b/src/ZE_ZClient.cpp index 3e58cbf..fbacc01 100755 --- a/src/ZE_ZClient.cpp +++ b/src/ZE_ZClient.cpp @@ -13,7 +13,7 @@ File: ZE_ZClient.cpp
Description: Implementation source file for core ZEngine TCP Client Object.
Author(s): James Turk
-$Id:
+$Id: ZE_ZClient.cpp,v 1.2 2003/03/14 22:20:13 cozman Exp $
\file ZE_ZClient.cpp \brief Source file for ZClient. diff --git a/src/ZE_ZServer.cpp b/src/ZE_ZServer.cpp index 03d9942..949ddf4 100755 --- a/src/ZE_ZServer.cpp +++ b/src/ZE_ZServer.cpp @@ -13,7 +13,7 @@ File: ZE_ZServer.cpp
Description: Implementation source file for core ZEngine TCP Server Object.
Author(s): James Turk
-$Id:
+$Id: ZE_ZServer.cpp,v 1.2 2003/03/14 22:20:12 cozman Exp $
\file ZE_ZServer.cpp \brief Source file for ZServer.