root/trunk/src/game/WorldSocket.h @ 42

Revision 6, 7.2 kB (checked in by yumileroy, 17 years ago)

[svn] * Added ACE for Linux and Windows (Thanks Derex for Linux part and partial Windows part)
* Updated to 6721 and 676
* Fixed TrinityScript? logo
* Version updated to 0.2.6721.676

Original author: Neo2003
Date: 2008-10-04 06:17:19-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19/** \addtogroup u2w User to World Communication
20 * @{
21 * \file WorldSocket.h
22 * \author Derex <derex101@gmail.com>
23 */
24
25#ifndef _WORLDSOCKET_H
26#define _WORLDSOCKET_H
27
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>
38
39#if !defined (ACE_LACKS_PRAGMA_ONCE)
40#pragma once
41#endif /* ACE_LACKS_PRAGMA_ONCE */
42
43#include "Common.h"
44#include "Auth/AuthCrypt.h"
45
46class ACE_Message_Block;
47class WorldPacket;
48class WorldSession;
49
50/// Handler that can communicate over stream sockets.
51typedef ACE_Svc_Handler<ACE_SOCK_STREAM, ACE_NULL_SYNCH> WorldHandler;
52
53/**
54 * WorldSocket.
55 *
56 * This class is responsible for the comunication with
57 * remote clients.
58 * Most methods return -1 on failure.
59 * The class uses refferece counting.
60 *
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 realy a lot of small-size writes to it, and it doesn't
65 * scale well to allocate memory for every. When something is
66 * writen to the output buffer the socket is not immideately
67 * activated for output (again for the same reason), there
68 * is 10ms celling (thats why there is Update() method).
69 * This concept is simmilar to TCP_CORK, but TCP_CORK
70 * usses 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 tollerated.
73 *
74 * The calls to Upate () method are managed by WorldSocketMgr
75 * and ReactorRunnable.
76 *
77 * For input ,the class uses one 1024 bytes buffer on stack
78 * to which it does recv() calls. And then recieved data is
79 * distributed where its needed. 1024 matches pritey well the
80 * traffic generated by client for now.
81 * 
82 * The input/output do speculative reads/writes (AKA it tryes
83 * to read all data avaible in the kernel buffer or tryes to
84 * write everything avaible in userspace buffer),
85 * which is ok for using with Level and Edge Trigered IO
86 * notification.
87 *
88 */
89class WorldSocket : protected WorldHandler
90{
91public:
92  /// Declare some friends
93  friend class ACE_Acceptor< WorldSocket, ACE_SOCK_ACCEPTOR >;
94  friend class WorldSocketMgr;
95  friend class ReactorRunnable;
96
97  /// Declare the acceptor for this class
98  typedef ACE_Acceptor< WorldSocket, ACE_SOCK_ACCEPTOR > Acceptor;
99
100  /// Mutex type used for various syncronizations.
101  typedef ACE_Thread_Mutex LockType;
102  typedef ACE_Guard<LockType> GuardType;
103
104  /// Queue for storing packets for which there is no space.
105  typedef ACE_Unbounded_Queue< WorldPacket* > PacketQueueT;
106
107  /// Check if socket is closed.
108  bool IsClosed (void) const;
109
110  /// Close the socket.
111  void CloseSocket (void);
112
113  /// Get address of connected peer.
114  const std::string& GetRemoteAddress (void) const;
115
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);
120
121  /// Add refference to this object.
122  long AddReference (void);
123
124  /// Remove refference to this object.
125  long RemoveReference (void);
126
127protected:
128  /// things called by ACE framework.
129  WorldSocket (void);
130  virtual ~WorldSocket (void);
131
132  /// Called on open ,the void* is the acceptor.
133  virtual int open (void *);
134
135  /// Called on failures inside of the acceptor, don't call from your code.
136  virtual int close (int);
137
138  /// Called when we can read from the socket.
139  virtual int handle_input (ACE_HANDLE = ACE_INVALID_HANDLE);
140
141  /// Called when the socket can write.
142  virtual int handle_output (ACE_HANDLE = ACE_INVALID_HANDLE);
143
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);
147
148  /// Called by WorldSocketMgr/ReactorRunnable.
149  int Update (void);
150
151private:
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);
156
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);
161
162  /// process one incoming packet.
163  /// @param new_pct received packet ,note that you need to delete it.
164  int ProcessIncoming (WorldPacket* new_pct);
165
166  /// Called by ProcessIncoming() on CMSG_AUTH_SESSION.
167  int HandleAuthSession (WorldPacket& recvPacket);
168
169  /// Called by ProcessIncoming() on CMSG_PING.
170  int HandlePing (WorldPacket& recvPacket);
171
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);
175
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 ();
181
182private:
183  /// Time in which the last ping was received
184  ACE_Time_Value m_LastPingTime;
185 
186  /// Keep track of overspeed pings ,to prevent ping flood.
187  uint32 m_OverSpeedPings;
188
189  /// Address of the remote peer
190  std::string m_Address;
191
192  /// Class used for managing encryption of the headers
193  AuthCrypt m_Crypt;
194
195  /// Mutex lock to protect m_Session
196  LockType m_SessionLock;
197
198  /// Session to which recieved packets are routed
199  WorldSession* m_Session;
200
201  /// here are stored the fragmens of the recieved data
202  WorldPacket* m_RecvWPct;
203
204  /// This block actually refers to m_RecvWPct contents,
205  /// which alows 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;
208
209  /// Fragment of the recieved header.
210  ACE_Message_Block m_Header;
211
212  /// Mutex for protecting otuput related data.
213  LockType m_OutBufferLock;
214
215  /// Buffer used for writing output.
216  ACE_Message_Block *m_OutBuffer;
217
218  /// Size of the m_OutBuffer.
219  size_t m_OutBufferSize;
220
221  /// Here are stored packets for which there was no space on m_OutBuffer,
222  /// this alows not-to kick player if its buffer is overflowed.
223  PacketQueueT m_PacketQueue;
224
225  /// True if the socket is registered with the reactor for output
226  bool m_OutActive;
227
228  uint32 m_Seed;
229};
230
231#endif  /* _WORLDSOCKET_H */
232
233/// @}
Note: See TracBrowser for help on using the browser.