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 6946 2008-08-09 02:30:28Z thjc $
00044  */
00045 
00093 #ifndef _PLAYERTCP_H_
00094 #define _PLAYERTCP_H_
00095 
00096 #include <sys/types.h>
00097 #include <sys/socket.h>
00098 #include <netinet/in.h>
00099 #include <pthread.h>
00100 
00101 #include <libplayercore/playercore.h>
00102 
00104 #define PLAYERTCP_DEFAULT_PORT 6665
00105 
00108 #define PLAYERTCP_READBUFFER_SIZE 65536
00109 
00112 #define PLAYERTCP_WRITEBUFFER_SIZE 65536
00113 
00114 // Forward declarations
00115 struct pollfd;
00116 
00117 struct playertcp_listener;
00118 struct playertcp_conn;
00119 
00120 class PlayerTCP
00121 {
00122   private:
00123     uint32_t host;
00124     int num_listeners;
00125     playertcp_listener* listeners;
00126     struct pollfd* listen_ufds;
00127 
00128     pthread_mutex_t clients_mutex;
00129     int size_clients;
00130     int num_clients;
00131     playertcp_conn* clients;
00132     struct pollfd* client_ufds;
00133 
00135     char* decode_readbuffer;
00137     int decode_readbuffersize;
00138 
00139     void Lock();
00140     void Unlock();
00141 
00142   public:
00143     PlayerTCP();
00144     ~PlayerTCP();
00145 
00146 
00147     static void InitGlobals(void);
00148 
00149     pthread_t thread;
00150 
00151     int Listen(int* ports, int num_ports, int* new_ports=NULL);
00152     int Listen(int port);
00153     QueuePointer AddClient(struct sockaddr_in* cliaddr,
00154                             unsigned int local_host,
00155                             unsigned int local_port,
00156                             int newsock,
00157                             bool send_banner,
00158                             int* kill_flag,
00159                             bool have_lock);
00160     int Update(int timeout);
00161     int Accept(int timeout);
00162     void Close(int cli);
00163     int ReadClient(int cli);
00164     int ReadClient(QueuePointer q);
00165     int Read(int timeout, bool have_lock);
00166     int Write(bool have_lock);
00167     int WriteClient(int cli);
00168     void DeleteClients();
00169     void ParseBuffer(int cli);
00170     int HandlePlayerMessage(int cli, Message* msg);
00171     void DeleteClient(QueuePointer &q, bool have_lock);
00172     bool Listening(int port);
00173     uint32_t GetHost() {return host;};
00174 };
00175 
00178 #endif

Last updated 12 September 2005 21:38:45