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

Revision 232, 7.8 kB (checked in by yumileroy, 17 years ago)

[svn] *** Source: MaNGOS ***
* Fixed english spelling in src/game/WorldSocket.h/cpp. Author: Derex
* [240_world.sql] Create new command .senditems and remove from moderator level command .sendmail possibility send items. Author: Vladimir
* Added new command: .sendmoney player "subject" "message" money - Sends a mail with money to a player. Author: fredi
* Correctly apply taken damage debufs/bonuses in cases non-physical melee damage. Author: Frankir
* Fix a crash in add friend/ignore callback. (check if player still logged in). Author: Hunuza
* Better args checking in .sendmoney command. Author: Vladimir

Original author: visagalis
Date: 2008-11-14 17:50:48-06:00

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