2 * Copyright (C) 2005-2010 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 /// Check if socket is closed.
105 bool IsClosed (void) const;
107 /// Close the socket.
108 void CloseSocket (void);
110 /// Get address of connected peer.
111 const std::string
& GetRemoteAddress (void) const;
113 /// Send A packet on the socket, this function is reentrant.
114 /// @param pct packet to send
115 /// @return -1 of failure
116 int SendPacket (const WorldPacket
& pct
);
118 /// Add reference to this object.
119 long AddReference (void);
121 /// Remove reference to this object.
122 long RemoveReference (void);
125 /// things called by ACE framework.
127 virtual ~WorldSocket (void);
129 /// Called on open ,the void* is the acceptor.
130 virtual int open (void *);
132 /// Called on failures inside of the acceptor, don't call from your code.
133 virtual int close (int);
135 /// Called when we can read from the socket.
136 virtual int handle_input (ACE_HANDLE
= ACE_INVALID_HANDLE
);
138 /// Called when the socket can write.
139 virtual int handle_output (ACE_HANDLE
= ACE_INVALID_HANDLE
);
141 /// Called when connection is closed or error happens.
142 virtual int handle_close (ACE_HANDLE
= ACE_INVALID_HANDLE
,
143 ACE_Reactor_Mask
= ACE_Event_Handler::ALL_EVENTS_MASK
);
145 /// Called by WorldSocketMgr/ReactorRunnable.
149 /// Helper functions for processing incoming data.
150 int handle_input_header (void);
151 int handle_input_payload (void);
152 int handle_input_missing_data (void);
154 /// Help functions to mark/unmark the socket for output.
155 /// @param g the guard is for m_OutBufferLock, the function will release it
156 int cancel_wakeup_output (GuardType
& g
);
157 int schedule_wakeup_output (GuardType
& g
);
159 /// Drain the queue if its not empty.
160 int handle_output_queue (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
);
173 /// Time in which the last ping was received
174 ACE_Time_Value m_LastPingTime
;
176 /// Keep track of over-speed pings ,to prevent ping flood.
177 uint32 m_OverSpeedPings
;
179 /// Address of the remote peer
180 std::string m_Address
;
182 /// Class used for managing encryption of the headers
185 /// Mutex lock to protect m_Session
186 LockType m_SessionLock
;
188 /// Session to which received packets are routed
189 WorldSession
* m_Session
;
191 /// here are stored the fragments of the received data
192 WorldPacket
* m_RecvWPct
;
194 /// This block actually refers to m_RecvWPct contents,
195 /// which allows easy and safe writing to it.
196 /// It wont free memory when its deleted. m_RecvWPct takes care of freeing.
197 ACE_Message_Block m_RecvPct
;
199 /// Fragment of the received header.
200 ACE_Message_Block m_Header
;
202 /// Mutex for protecting output related data.
203 LockType m_OutBufferLock
;
205 /// Buffer used for writing output.
206 ACE_Message_Block
*m_OutBuffer
;
208 /// Size of the m_OutBuffer.
209 size_t m_OutBufferSize
;
211 /// True if the socket is registered with the reactor for output
217 #endif /* _WORLDSOCKET_H */