EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TCPServer.h
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur, Bloody.Rabbit
24 */
25 
26 #ifndef __NETWORK__TCP_SERVER_H__INCL__
27 #define __NETWORK__TCP_SERVER_H__INCL__
28 
29 #include "network/Socket.h"
30 #include "threading/Mutex.h"
31 
33 extern const uint32 TCPSRV_ERRBUF_SIZE;
35 extern const uint32 TCPSRV_LOOP_GRANULARITY;
36 
43 {
44 public:
48  BaseTCPServer();
52  virtual ~BaseTCPServer();
53 
55  uint16 GetPort() const { return mPort; }
57  bool IsOpen() const;
58 
67  bool Open( uint16 port, char* errbuf = 0 );
71  void Close();
72 
73 protected:
80  void StartLoop();
84  void WaitLoop();
85 
91  virtual bool Process();
95  void ListenNewConnections();
96 
103  virtual void CreateNewConnection( Socket* sock, uint32 rIP, uint16 rPort ) = 0;
104 
113  static void* TCPServerLoop( void* arg );
117  void TCPServerLoop();
118 
120  mutable Mutex mMSock;
125 
128 };
129 
135 template<typename X>
136 class TCPServer : public BaseTCPServer
137 {
138 public:
143  {
144  MutexLock lock( mMQueue );
145 
146  X* conn(nullptr);
147  while( ( conn = PopConnection() ) )
148  SafeDelete( conn );
149  }
150 
157  {
158  MutexLock lock( mMQueue );
159 
160  X* ret(nullptr);
161  if( !mQueue.empty() )
162  {
163  ret = mQueue.front();
164  mQueue.pop();
165  }
166 
167  return ret;
168  }
169 
170 protected:
176  void AddConnection( X* con )
177  {
178  MutexLock lock( mMQueue );
179 
180  mQueue.push( con );
181  }
182 
186  std::queue<X*> mQueue;
187 };
188 
189 #endif /* !__NETWORK__TCP_SERVER_H__INCL__ */
Simple wrapper for sockets.
Definition: Socket.h:34
void WaitLoop()
Waits for worker thread to terminate.
Definition: TCPServer.cpp:139
void TCPServerLoop()
Loop for worker threads.
Definition: TCPServer.cpp:183
BaseTCPServer()
Creates empty TCP server.
Definition: TCPServer.cpp:37
void AddConnection(X *con)
Adds connection to the queue.
Definition: TCPServer.h:176
A lock for a Lockable object.
Definition: Lock.h:70
Socket * mSock
Definition: TCPServer.h:122
Mutex mMSock
Definition: TCPServer.h:120
Common wrapper for platform-specific mutexes.
Definition: Mutex.h:36
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
void ListenNewConnections()
Accepts all new connections.
Definition: TCPServer.cpp:156
uint16 GetPort() const
Definition: TCPServer.h:55
void Close()
Stops started listening.
Definition: TCPServer.cpp:118
bool Open(uint16 port, char *errbuf=0)
Start listening on specified port.
Definition: TCPServer.cpp:61
virtual ~BaseTCPServer()
Cleans server up.
Definition: TCPServer.cpp:43
X * PopConnection()
Pops connection from queue.
Definition: TCPServer.h:156
Mutex mMLoopRunning
Definition: TCPServer.h:127
uint16 mPort
Definition: TCPServer.h:124
unsigned __int32 uint32
Definition: eve-compat.h:50
Mutex mMQueue
Definition: TCPServer.h:184
Connection-type-dependent version of TCP server.
Definition: TCPServer.h:136
const uint32 TCPSRV_ERRBUF_SIZE
Definition: TCPServer.cpp:34
virtual void CreateNewConnection(Socket *sock, uint32 rIP, uint16 rPort)=0
Processes new connection.
std::queue< X * > mQueue
Definition: TCPServer.h:186
void StartLoop()
Starts worker thread.
Definition: TCPServer.cpp:125
bool IsOpen() const
Definition: TCPServer.cpp:53
Generic class for TCP server.
Definition: TCPServer.h:42
unsigned __int16 uint16
Definition: eve-compat.h:48
~TCPServer()
Deletes all stored connections.
Definition: TCPServer.h:142
virtual bool Process()
Does periodical stuff to keep the server alive.
Definition: TCPServer.cpp:146
const uint32 TCPSRV_LOOP_GRANULARITY
Definition: TCPServer.cpp:35