2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 /** \addtogroup u2w User to World Communication
22 * \author Derex <derex101@gmail.com>
25 #ifndef _WORLDSOCKET_H
26 #define _WORLDSOCKET_H
28 #include <ace/Basic_Types.h>
29 #include <ace/Synch_Traits.h>
30 #include <ace/Svc_Handler.h>
31 #include <ace/SOCK_Stream.h>
32 #include <ace/SOCK_Acceptor.h>
33 #include <ace/Acceptor.h>
34 #include <ace/Thread_Mutex.h>
35 #include <ace/Guard_T.h>
36 #include <ace/Unbounded_Queue.h>
37 #include <ace/Message_Block.h>
39 #if !defined (ACE_LACKS_PRAGMA_ONCE)
41 #endif /* ACE_LACKS_PRAGMA_ONCE */
44 #include "Auth/AuthCrypt.h"
46 class ACE_Message_Block
;
50 /// Handler that can communicate over stream sockets.
51 typedef ACE_Svc_Handler
<ACE_SOCK_STREAM
, ACE_NULL_SYNCH
> WorldHandler
;
56 * This class is responsible for the communication with
58 * Most methods return -1 on failure.
59 * The class uses reference counting.
61 * For output the class uses one buffer (64K usually) and
62 * a queue where it stores packet if there is no place on
63 * the queue. The reason this is done, is because the server
64 * does really a lot of small-size writes to it, and it doesn't
65 * scale well to allocate memory for every. When something is
66 * written to the output buffer the socket is not immediately
67 * activated for output (again for the same reason), there
68 * is 10ms celling (thats why there is Update() method).
69 * This concept is similar to TCP_CORK, but TCP_CORK
70 * uses 200ms celling. As result overhead generated by
71 * sending packets from "producer" threads is minimal,
72 * and doing a lot of writes with small size is tolerated.
74 * The calls to Update () method are managed by WorldSocketMgr
75 * and ReactorRunnable.
77 * For input ,the class uses one 1024 bytes buffer on stack
78 * to which it does recv() calls. And then received data is
79 * distributed where its needed. 1024 matches pretty well the
80 * traffic generated by client for now.
82 * The input/output do speculative reads/writes (AKA it tryes
83 * to read all data available in the kernel buffer or tryes to
84 * write everything available in userspace buffer),
85 * which is ok for using with Level and Edge Triggered IO
89 class WorldSocket
: protected WorldHandler
92 /// Declare some friends
93 friend class ACE_Acceptor
< WorldSocket
, ACE_SOCK_ACCEPTOR
>;
94 friend class WorldSocketMgr
;
95 friend class ReactorRunnable
;
97 /// Declare the acceptor for this class
98 typedef ACE_Acceptor
< WorldSocket
, ACE_SOCK_ACCEPTOR
> Acceptor
;
100 /// Mutex type used for various synchronizations.
101 typedef ACE_Thread_Mutex LockType
;
102 typedef ACE_Guard
<LockType
> GuardType
;
104 /// Queue for storing packets for which there is no space.
105 typedef ACE_Unbounded_Queue
< WorldPacket
* > PacketQueueT
;
107 /// Check if socket is closed.
108 bool IsClosed (void) const;
110 /// Close the socket.
111 void CloseSocket (void);
113 /// Get address of connected peer.
114 const std::string
& GetRemoteAddress (void) const;
116 /// Send A packet on the socket, this function is reentrant.
117 /// @param pct packet to send
118 /// @return -1 of failure
119 int SendPacket (const WorldPacket
& pct
);
121 /// Add reference to this object.
122 long AddReference (void);
124 /// Remove reference to this object.
125 long RemoveReference (void);
128 /// things called by ACE framework.
130 virtual ~WorldSocket (void);
132 /// Called on open ,the void* is the acceptor.
133 virtual int open (void *);
135 /// Called on failures inside of the acceptor, don't call from your code.
136 virtual int close (int);
138 /// Called when we can read from the socket.
139 virtual int handle_input (ACE_HANDLE
= ACE_INVALID_HANDLE
);
141 /// Called when the socket can write.
142 virtual int handle_output (ACE_HANDLE
= ACE_INVALID_HANDLE
);
144 /// Called when connection is closed or error happens.
145 virtual int handle_close (ACE_HANDLE
= ACE_INVALID_HANDLE
,
146 ACE_Reactor_Mask
= ACE_Event_Handler::ALL_EVENTS_MASK
);
148 /// Called by WorldSocketMgr/ReactorRunnable.
152 /// Helper functions for processing incoming data.
153 int handle_input_header (void);
154 int handle_input_payload (void);
155 int handle_input_missing_data (void);
157 /// Help functions to mark/unmark the socket for output.
158 /// @param g the guard is for m_OutBufferLock, the function will release it
159 int cancel_wakeup_output (GuardType
& g
);
160 int schedule_wakeup_output (GuardType
& g
);
162 /// process one incoming packet.
163 /// @param new_pct received packet ,note that you need to delete it.
164 int ProcessIncoming (WorldPacket
* new_pct
);
166 /// Called by ProcessIncoming() on CMSG_AUTH_SESSION.
167 int HandleAuthSession (WorldPacket
& recvPacket
);
169 /// Called by ProcessIncoming() on CMSG_PING.
170 int HandlePing (WorldPacket
& recvPacket
);
172 /// Try to write WorldPacket to m_OutBuffer ,return -1 if no space
173 /// Need to be called with m_OutBufferLock lock held
174 int iSendPacket (const WorldPacket
& pct
);
176 /// Flush m_PacketQueue if there are packets in it
177 /// Need to be called with m_OutBufferLock lock held
178 /// @return true if it wrote to the buffer ( AKA you need
179 /// to mark the socket for output ).
180 bool iFlushPacketQueue ();
183 /// Time in which the last ping was received
184 ACE_Time_Value m_LastPingTime
;
186 /// Keep track of over-speed pings ,to prevent ping flood.
187 uint32 m_OverSpeedPings
;
189 /// Address of the remote peer
190 std::string m_Address
;
192 /// Class used for managing encryption of the headers
195 /// Mutex lock to protect m_Session
196 LockType m_SessionLock
;
198 /// Session to which received packets are routed
199 WorldSession
* m_Session
;
201 /// here are stored the fragments of the received data
202 WorldPacket
* m_RecvWPct
;
204 /// This block actually refers to m_RecvWPct contents,
205 /// which allows easy and safe writing to it.
206 /// It wont free memory when its deleted. m_RecvWPct takes care of freeing.
207 ACE_Message_Block m_RecvPct
;
209 /// Fragment of the received header.
210 ACE_Message_Block m_Header
;
212 /// Mutex for protecting output related data.
213 LockType m_OutBufferLock
;
215 /// Buffer used for writing output.
216 ACE_Message_Block
*m_OutBuffer
;
218 /// Size of the m_OutBuffer.
219 size_t m_OutBufferSize
;
221 /// Here are stored packets for which there was no space on m_OutBuffer,
222 /// this allows not-to kick player if its buffer is overflowed.
223 PacketQueueT m_PacketQueue
;
225 /// True if the socket is registered with the reactor for output
231 #endif /* _WORLDSOCKET_H */