playertcp.h
1 /*
2  * Player - One Hell of a Robot Server
3  * Copyright (C) 2005 -
4  * Brian Gerkey
5  *
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  */
22 /********************************************************************
23  *
24  * This library is free software; you can redistribute it and/or
25  * modify it under the terms of the GNU Lesser General Public
26  * License as published by the Free Software Foundation; either
27  * version 2.1 of the License, or (at your option) any later version.
28  *
29  * This library is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32  * Lesser General Public License for more details.
33  *
34  * You should have received a copy of the GNU Lesser General Public
35  * License along with this library; if not, write to the Free Software
36  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37  *
38  ********************************************************************/
39 
40 /*
41  * Interface to libplayertcp
42  *
43  * $Id$
44  */
45 
92 #ifndef _PLAYERTCP_H_
93 #define _PLAYERTCP_H_
94 
95 #if defined (WIN32)
96  #if defined (PLAYER_STATIC)
97  #define PLAYERTCP_EXPORT
98  #elif defined (playertcp_EXPORTS)
99  #define PLAYERTCP_EXPORT __declspec (dllexport)
100  #else
101  #define PLAYERTCP_EXPORT __declspec (dllimport)
102  #endif
103 #else
104  #define PLAYERTCP_EXPORT
105 #endif
106 
107 #if defined (WIN32)
108  #include <winsock2.h>
109  #include <ws2tcpip.h>
110 #else
111  #include <sys/socket.h>
112  #include <sys/ioctl.h>
113  #include <netdb.h>
114  #include <netinet/in.h>
115 #endif
116 #include <sys/types.h>
117 #include <pthread.h>
118 
119 #include <libplayercore/playercore.h>
120 
122 #define PLAYERTCP_DEFAULT_PORT 6665
123 
126 #define PLAYERTCP_READBUFFER_SIZE 65536
127 
130 #define PLAYERTCP_WRITEBUFFER_SIZE 65536
131 
132 // Forward declarations
133 struct pollfd;
134 
135 struct playertcp_listener;
136 struct playertcp_conn;
137 
139 {
140  private:
141  uint32_t host;
142  int num_listeners;
143  playertcp_listener* listeners;
144  struct pollfd* listen_ufds;
145 
146  pthread_mutex_t clients_mutex;
147  int size_clients;
148  int num_clients;
149  playertcp_conn* clients;
150  struct pollfd* client_ufds;
151 
153  char* decode_readbuffer;
155  int decode_readbuffersize;
156 
157  public:
158  PlayerTCP();
159  ~PlayerTCP();
160 
161  void Lock();
162  void Unlock();
163 
164  static void InitGlobals(void);
165 
166  pthread_t thread;
167 
168  int Listen(int* ports, int num_ports, int* new_ports=NULL);
169  int Listen(int port);
170  QueuePointer AddClient(struct sockaddr_in* cliaddr,
171  unsigned int local_host,
172  unsigned int local_port,
173  int newsock,
174  bool send_banner,
175  int* kill_flag,
176  bool have_lock);
177  QueuePointer AddClient(struct sockaddr_in* cliaddr,
178  unsigned int local_host,
179  unsigned int local_port,
180  int newsock,
181  bool send_banner,
182  int* kill_flag,
183  bool have_lock,
184  QueuePointer queue);
185  int Update(int timeout);
186  int Accept(int timeout);
187  void Close(int cli);
188  int ReadClient(int cli);
189  int ReadClient(QueuePointer q);
190  int Read(int timeout, bool have_lock);
191  int Write(bool have_lock);
192  int WriteClient(int cli);
193  void DeleteClients();
194  void ParseBuffer(int cli);
195  int HandlePlayerMessage(int cli, Message* msg);
196  void DeleteClient(QueuePointer &q, bool have_lock);
197  bool Listening(int port);
198  uint32_t GetHost() {return host;};
199 };
200 
203 #endif
Definition: playertcp.h:137
An autopointer for the message queue.
Definition: message.h:72
Reference-counted message objects.
Definition: message.h:131
#define PLAYERTCP_EXPORT
Default TCP port.
Definition: playertcp.h:103