playertcp.h
00001 /*
00002  *  Player - One Hell of a Robot Server
00003  *  Copyright (C) 2005 -
00004  *     Brian Gerkey
00005  *
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  */
00022 /********************************************************************
00023  *
00024  *  This library is free software; you can redistribute it and/or
00025  *  modify it under the terms of the GNU Lesser General Public
00026  *  License as published by the Free Software Foundation; either
00027  *  version 2.1 of the License, or (at your option) any later version.
00028  *
00029  *  This library is distributed in the hope that it will be useful,
00030  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00031  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00032  *  Lesser General Public License for more details.
00033  *
00034  *  You should have received a copy of the GNU Lesser General Public
00035  *  License along with this library; if not, write to the Free Software
00036  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00037  *
00038  ********************************************************************/
00039 
00040 /*
00041  * Interface to libplayertcp
00042  *
00043  * $Id: playertcp.h 7878 2009-06-23 11:14:00Z thjc $
00044  */
00045 
00093 #ifndef _PLAYERTCP_H_
00094 #define _PLAYERTCP_H_
00095 
00096 #if defined (WIN32)
00097   #if defined (PLAYER_STATIC)
00098     #define PLAYERTCP_EXPORT
00099   #elif defined (playertcp_EXPORTS)
00100     #define PLAYERTCP_EXPORT    __declspec (dllexport)
00101   #else
00102     #define PLAYERTCP_EXPORT    __declspec (dllimport)
00103   #endif
00104 #else
00105   #define PLAYERTCP_EXPORT
00106 #endif
00107 
00108 #if defined (WIN32)
00109   #include <winsock2.h>
00110   #include <ws2tcpip.h>
00111 #else
00112   #include <sys/socket.h>
00113   #include <sys/ioctl.h>
00114   #include <netdb.h>
00115   #include <netinet/in.h>
00116 #endif
00117 #include <sys/types.h>
00118 #include <pthread.h>
00119 
00120 #include <libplayercore/playercore.h>
00121 
00123 #define PLAYERTCP_DEFAULT_PORT 6665
00124 
00127 #define PLAYERTCP_READBUFFER_SIZE 65536
00128 
00131 #define PLAYERTCP_WRITEBUFFER_SIZE 65536
00132 
00133 // Forward declarations
00134 struct pollfd;
00135 
00136 struct playertcp_listener;
00137 struct playertcp_conn;
00138 
00139 class PLAYERTCP_EXPORT PlayerTCP
00140 {
00141   private:
00142     uint32_t host;
00143     int num_listeners;
00144     playertcp_listener* listeners;
00145     struct pollfd* listen_ufds;
00146 
00147     pthread_mutex_t clients_mutex;
00148     int size_clients;
00149     int num_clients;
00150     playertcp_conn* clients;
00151     struct pollfd* client_ufds;
00152 
00154     char* decode_readbuffer;
00156     int decode_readbuffersize;
00157 
00158   public:
00159     PlayerTCP();
00160     ~PlayerTCP();
00161 
00162     void Lock();
00163     void Unlock();
00164 
00165     static void InitGlobals(void);
00166 
00167     pthread_t thread;
00168 
00169     int Listen(int* ports, int num_ports, int* new_ports=NULL);
00170     int Listen(int port);
00171     QueuePointer AddClient(struct sockaddr_in* cliaddr,
00172                             unsigned int local_host,
00173                             unsigned int local_port,
00174                             int newsock,
00175                             bool send_banner,
00176                             int* kill_flag,
00177                             bool have_lock);
00178     QueuePointer AddClient(struct sockaddr_in* cliaddr,
00179                             unsigned int local_host,
00180                             unsigned int local_port,
00181                             int newsock,
00182                             bool send_banner,
00183                             int* kill_flag,
00184                             bool have_lock,
00185                             QueuePointer queue);
00186     int Update(int timeout);
00187     int Accept(int timeout);
00188     void Close(int cli);
00189     int ReadClient(int cli);
00190     int ReadClient(QueuePointer q);
00191     int Read(int timeout, bool have_lock);
00192     int Write(bool have_lock);
00193     int WriteClient(int cli);
00194     void DeleteClients();
00195     void ParseBuffer(int cli);
00196     int HandlePlayerMessage(int cli, Message* msg);
00197     void DeleteClient(QueuePointer &q, bool have_lock);
00198     bool Listening(int port);
00199     uint32_t GetHost() {return host;};
00200 };
00201 
00204 #endif

Last updated 12 September 2005 21:38:45