EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TCPConnection.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_CONNECTION_H__INCL__
27 #define __NETWORK__TCP_CONNECTION_H__INCL__
28 
29 #include "network/Socket.h"
30 #include "threading/Mutex.h"
31 #include "utils/Buffer.h"
32 
34 static const uint32 TCPCONN_ERRBUF_SIZE = 1024;
36 extern const uint32 TCPCONN_RECVBUF_SIZE;
38 extern const uint32 TCPCONN_LOOP_GRANULARITY;
39 
46 {
47 public:
49  enum state_t
50  {
55  };
56 
60  TCPConnection();
64  virtual ~TCPConnection();
65 
67  uint32 GetrIP() const { return mrIP; }
69  uint16 GetrPort() const { return mrPort; }
75  std::string GetAddress();
77  state_t GetState() const { return mSockState; }
78 
92  bool Connect( uint32 rIP, uint16 rPort, char* errbuf = 0 );
103  void AsyncConnect( uint32 rIP, uint16 rPort );
111  void Disconnect();
112 
120  bool Send( Buffer** data );
121 
122 protected:
130  TCPConnection( Socket* sock, uint32 rIP, uint16 rPort );
131 
138  void StartLoop();
142  void WaitLoop();
143 
149  virtual bool Process();
161  virtual bool ProcessReceivedData( char* errbuf = 0 ) = 0;
162 
170  virtual bool SendData( char* errbuf = 0 );
178  virtual bool RecvData( char* errbuf = 0 );
182  void DoDisconnect();
183 
187  virtual void ClearBuffers();
188 
197  static void* TCPConnectionLoop( void* arg );
201  void TCPConnectionLoop();
202 
204  mutable Mutex mMSock;
213 
216 
220  std::deque<Buffer*> mSendQueue;
221 
224 };
225 
226 #endif /* !__NETWORK__TCP_CONNECTION_H__INCL__ */
uint16 GetrPort() const
Definition: TCPConnection.h:69
Simple wrapper for sockets.
Definition: Socket.h:34
virtual ~TCPConnection()
Cleans connection up.
const uint32 TCPCONN_RECVBUF_SIZE
state_t mSockState
void WaitLoop()
Blocks calling thread until working thread terminates.
virtual bool RecvData(char *errbuf=0)
Receives data and puts them into receive queue.
virtual bool SendData(char *errbuf=0)
Sends data in send queue.
Common wrapper for platform-specific mutexes.
Definition: Mutex.h:36
virtual void ClearBuffers()
Clears send and receive buffers.
bool Send(Buffer **data)
Enqueues data to be sent.
TCPConnection()
Creates new connection in STATE_DISCONNECTED.
static const uint32 TCPCONN_ERRBUF_SIZE
Definition: TCPConnection.h:34
Generic class for buffers.
Definition: Buffer.h:40
void StartLoop()
Starts working thread.
Socket * mSock
Buffer * mRecvBuf
std::string GetAddress()
virtual bool Process()
Does all stuff that needs to be periodically done to keep connection alive.
Generic class for TCP connections.
Definition: TCPConnection.h:45
unsigned __int32 uint32
Definition: eve-compat.h:50
std::deque< Buffer * > mSendQueue
void AsyncConnect(uint32 rIP, uint16 rPort)
Schedules asynchronous connect to specified address.
bool Connect(uint32 rIP, uint16 rPort, char *errbuf=0)
Connects to specified address.
const uint32 TCPCONN_LOOP_GRANULARITY
void Disconnect()
Schedules disconnect of current connection.
uint32 GetrIP() const
Definition: TCPConnection.h:67
void DoDisconnect()
Disconnects socket.
unsigned __int16 uint16
Definition: eve-compat.h:48
state_t GetState() const
Definition: TCPConnection.h:77
void TCPConnectionLoop()
Loop for worker threads.
virtual bool ProcessReceivedData(char *errbuf=0)=0
Processes received data.