OregonCore  revision fb2a440-git
Your Favourite TBC server
WorldSocket.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef _WORLDSOCKET_H
19 #define _WORLDSOCKET_H
20 
21 #include <ace/Basic_Types.h>
22 #include <ace/Synch_Traits.h>
23 #include <ace/Svc_Handler.h>
24 #include <ace/SOCK_Stream.h>
25 #include <ace/SOCK_Acceptor.h>
26 #include <ace/Acceptor.h>
27 #include <ace/Thread_Mutex.h>
28 #include <ace/Guard_T.h>
29 #include <ace/Unbounded_Queue.h>
30 #include <ace/Message_Block.h>
31 
32 #if !defined (ACE_LACKS_PRAGMA_ONCE)
33 #pragma once
34 #endif /* ACE_LACKS_PRAGMA_ONCE */
35 
36 #include "Common.h"
37 #include "Auth/AuthCrypt.h"
38 
39 class ACE_Message_Block;
40 class WorldPacket;
42 
43 // Handler that can communicate over stream sockets.
44 typedef ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> WorldHandler;
45 
82 class WorldSocket : protected WorldHandler
83 {
84  public:
85  // Declare some friends
86  friend class ACE_Acceptor< WorldSocket, ACE_SOCK_ACCEPTOR >;
87  friend class WorldSocketMgr;
88  friend class ReactorRunnable;
89 
90  // Declare the acceptor for this class
92 
93  // Mutex type used for various synchronizations.
94  typedef ACE_Thread_Mutex LockType;
95  typedef ACE_Guard<LockType> GuardType;
96 
97  // Queue for storing packets for which there is no space.
98  typedef ACE_Unbounded_Queue< WorldPacket* > PacketQueueT;
99 
100  // Check if socket is closed.
101  bool IsClosed (void) const;
102 
103  // Close the socket.
104  void CloseSocket (void);
105 
106  // Get address of connected peer.
107  const std::string& GetRemoteAddress (void) const;
108 
109  // Send A packet on the socket, this function is reentrant.
110  // pct packet to send
111  // return -1 of failure
112  int SendPacket (const WorldPacket& pct);
113 
114  // Add reference to this object.
115  long AddReference (void);
116 
117  // Remove reference to this object.
118  long RemoveReference (void);
119 
120  protected:
121  // things called by ACE framework.
122  WorldSocket (void);
123  virtual ~WorldSocket (void);
124 
125  // Called on open ,the void* is the acceptor.
126  virtual int open (void*);
127 
128  // Called on failures inside of the acceptor, don't call from your code.
129  virtual int close (u_long);
130 
131  // Called when we can read from the socket.
132  virtual int handle_input (ACE_HANDLE = ACE_INVALID_HANDLE);
133 
134  // Called when the socket can write.
135  virtual int handle_output (ACE_HANDLE = ACE_INVALID_HANDLE);
136 
137  // Called when connection is closed or error happens.
138  virtual int handle_close (ACE_HANDLE = ACE_INVALID_HANDLE,
139  ACE_Reactor_Mask = ACE_Event_Handler::ALL_EVENTS_MASK);
140 
141  // Called by WorldSocketMgr/ReactorRunnable.
142  int Update (void);
143 
144  private:
145  // Helper functions for processing incoming data.
146  int handle_input_header (void);
147  int handle_input_payload (void);
148  int handle_input_missing_data (void);
149 
150  // Help functions to mark/unmark the socket for output.
151  // param g the guard is for m_OutBufferLock, the function will release it
152  int cancel_wakeup_output (GuardType& g);
153  int schedule_wakeup_output (GuardType& g);
154 
155  // process one incoming packet.
156  // param new_pct received packet ,note that you need to delete it.
157  int ProcessIncoming (WorldPacket* new_pct);
158 
159  // Called by ProcessIncoming() on CMSG_AUTH_SESSION.
160  int HandleAuthSession (WorldPacket& recvPacket);
161 
162  // Called by ProcessIncoming() on CMSG_PING.
163  int HandlePing (WorldPacket& recvPacket);
164 
165  // Try to write WorldPacket to m_OutBuffer ,return -1 if no space
166  // Need to be called with m_OutBufferLock lock held
167  int iSendPacket (const WorldPacket& pct);
168 
169  // Flush m_PacketQueue if there are packets in it
170  // Need to be called with m_OutBufferLock lock held
171  // return true if it wrote to the buffer (AKA you need
172  // to mark the socket for output).
173  bool iFlushPacketQueue ();
174 
175  private:
176  // Time in which the last ping was received
177  ACE_Time_Value m_LastPingTime;
178 
179  // Keep track of over-speed pings ,to prevent ping flood.
181 
182  // Address of the remote peer
183  std::string m_Address;
184 
185  // Class used for managing encryption of the headers
187 
188  // Mutex lock to protect m_Session
189  LockType m_SessionLock;
190 
191  // Session to which received packets are routed
193 
194  // here are stored the fragments of the received data
196 
197  // This block actually refers to m_RecvWPct contents,
198  // which allows easy and safe writing to it.
199  // It wont free memory when its deleted. m_RecvWPct takes care of freeing.
200  ACE_Message_Block m_RecvPct;
201 
202  // Fragment of the received header.
203  ACE_Message_Block m_Header;
204 
205  // Mutex for protecting output related data.
206  LockType m_OutBufferLock;
207 
208  // Buffer used for writing output.
209  ACE_Message_Block* m_OutBuffer;
210 
211  // Size of the m_OutBuffer.
213 
214  // Here are stored packets for which there was no space on m_OutBuffer,
215  // this allows not-to kick player if its buffer is overflowed.
216  PacketQueueT m_PacketQueue;
217 
218  // True if the socket is registered with the reactor for output
220 
222 };
223 
224 #endif /* _WORLDSOCKET_H */
225 
ACE_Message_Block m_Header
Definition: WorldSocket.h:203
long AddReference(void)
ACE_Time_Value m_LastPingTime
Definition: WorldSocket.h:177
uint32 m_Seed
Definition: WorldSocket.h:221
virtual int close(u_long)
PacketQueueT m_PacketQueue
Definition: WorldSocket.h:216
ACE_Guard< LockType > GuardType
Definition: WorldSocket.h:95
ACE_Thread_Mutex LockType
Definition: WorldSocket.h:94
int SendPacket(const WorldPacket &pct)
friend class ACE_Acceptor< WorldSocket, ACE_SOCK_ACCEPTOR >
Definition: WorldSocket.h:86
int iSendPacket(const WorldPacket &pct)
WorldSession * m_Session
Definition: WorldSocket.h:192
virtual int handle_input(ACE_HANDLE=ACE_INVALID_HANDLE)
size_t m_OutBufferSize
Definition: WorldSocket.h:212
bool m_OutActive
Definition: WorldSocket.h:219
LockType m_SessionLock
Definition: WorldSocket.h:189
WorldSocket(void)
Definition: WorldSocket.cpp:69
LockType m_OutBufferLock
Definition: WorldSocket.h:206
int cancel_wakeup_output(GuardType &g)
ACE_Message_Block m_RecvPct
Definition: WorldSocket.h:200
int handle_input_missing_data(void)
int handle_input_header(void)
std::string m_Address
Definition: WorldSocket.h:183
int Update(void)
WorldPacket * m_RecvWPct
Definition: WorldSocket.h:195
int HandleAuthSession(WorldPacket &recvPacket)
ACE_Message_Block * m_OutBuffer
Definition: WorldSocket.h:209
int HandlePing(WorldPacket &recvPacket)
int handle_input_payload(void)
virtual int handle_output(ACE_HANDLE=ACE_INVALID_HANDLE)
virtual ~WorldSocket(void)
Definition: WorldSocket.cpp:85
bool IsClosed(void) const
int ProcessIncoming(WorldPacket *new_pct)
virtual int open(void *)
ACE_Acceptor< WorldSocket, ACE_SOCK_ACCEPTOR > Acceptor
Definition: WorldSocket.h:91
int schedule_wakeup_output(GuardType &g)
void CloseSocket(void)
ACE_UINT32 uint32
Definition: Define.h:71
const std::string & GetRemoteAddress(void) const
AuthCrypt m_Crypt
Definition: WorldSocket.h:186
ACE_Unbounded_Queue< WorldPacket * > PacketQueueT
Definition: WorldSocket.h:98
uint32 m_OverSpeedPings
Definition: WorldSocket.h:180
ACE_Svc_Handler< ACE_SOCK_STREAM, ACE_NULL_SYNCH > WorldHandler
Definition: WorldSocket.h:41
long RemoveReference(void)
virtual int handle_close(ACE_HANDLE=ACE_INVALID_HANDLE, ACE_Reactor_Mask=ACE_Event_Handler::ALL_EVENTS_MASK)
bool iFlushPacketQueue()