Planeshift
NetManager Class Reference

This is the network thread handling packets sending/receiving to/from network other threads can register a message queue and the network thread will sort messages for them and put them in their queue. More...

#include <netmanager.h>

Public Member Functions

virtual void Broadcast (MsgEntry *me, int scope, int guild=-1)
 This broadcasts the same msg out to a bunch of Clients. More...
 
void CheckLinkDead (void)
 Checks for and deletes link dead clients. More...
 
ClientGetAnyClient (int cnum)
 Gets a client with the specified id. More...
 
ClientGetClient (int cnum)
 Gets a client with the specified id. More...
 
ClientConnectionSetGetConnections ()
 Gets a list of all connected clients. More...
 
bool Initialize (CacheManager *cachemanager, int client_firstmsg, int npcclient_firstmsg, int timeout=15000)
 Initialize the network thread. More...
 
virtual void Multicast (MsgEntry *me, const csArray< PublishDestination > &multi, uint32_t except, float range)
 Sends the given message me to all the clients in the list. More...
 
 NetManager ()
 
void Run ()
 This is the main thread function. More...
 
virtual bool SendMessage (MsgEntry *me)
 Sends the given message to the client listed in the message. More...
 
bool SendMessageDelayed (MsgEntry *me, csTicks delay)
 Queues the message for sending later, so the calling classes don't have to all manage this themselves. More...
 
 ~NetManager ()
 
- Public Member Functions inherited from NetBase
void AddFilterLogMessage (int type)
 Add a new message type to the LogMessage message filter list. More...
 
bool AddMsgQueue (MsgQueue *, objID minID=0, objID maxID=0xffffffff)
 This adds another message queue (for other threads) These queues are for reading off. More...
 
bool Bind (const char *addr, int port)
 Binds the socket to the specified address (only needed on server. More...
 
bool Bind (const IN_ADDR &addr, int port)
 
bool CheckIn (void)
 this receives an Incoming Packet and analyses it More...
 
bool Flush (MsgQueue *queue)
 Flush all messages in given queue. More...
 
iEngine * GetEngine ()
 Get the Engine. More...
 
csTicks GetPing (void)
 
psNetMsgProfilesGetProfs ()
 
uint32_t GetRandomID ()
 return a random ID that can be used for messages More...
 
void InvertLogMessageFilter ()
 Invert the LogMessage filter. More...
 
bool IsReady ()
 Is this connection ready to use? More...
 
csString LogMessageFilter (const char *arg)
 Pars and configure user filter settings. More...
 
void LogMessageFilterClear ()
 Clear all message type from the LogMessage message filter list. More...
 
void LogMessageFilterDump ()
 Dump the current filter settings. More...
 
void LogMessages (char dir, MsgEntry *me)
 Log the message to LOG_MESSAGE. More...
 
 NetBase (int outqueuelen=100)
 you can specify how much messages the ouptput queue can contain before being full. More...
 
void ProcessNetwork (csTicks timeout)
 This function is the heart of NetBase - it look for new incoming packets and sends packets in the outgoing queue. More...
 
bool QueueMessage (MsgEntry *me)
 This adds a completed message to any queues that are signed up. More...
 
void RemoveFilterLogMessage (int type)
 Remove a message type from the LogMessage message filter list. More...
 
void RemoveMsgQueue (MsgQueue *)
 this removes a queue More...
 
virtual bool SendMessage (MsgEntry *me, NetPacketQueueRefCount *queue)
 
bool SendOut (void)
 sendOut sends the next packet in the outgoing message queue More...
 
void SetEngine (iEngine *engine)
 Set the Engine. More...
 
void SetLogMessageFilterHex (bool filterhex)
 Set the filter hex messages flag. More...
 
void SetMsgStrings (csStringSet *msgstrings, csStringHashReversible *msgstringshash)
 Set the MsgString Hash. More...
 
void ToggleReceiveMessageFilter ()
 Toggle the global receive LogMessage filter. More...
 
void ToggleSendMessageFilter ()
 Toggle the global send LogMessage filter. More...
 
virtual ~NetBase ()
 

Static Public Member Functions

static NetManagerCreate (CacheManager *cacheManager, int client_firstmsg, int npcclient_firstmsg, int timeout=15000)
 
static void Destroy ()
 
- Static Public Member Functions inherited from NetBase
static AccessPointersGetAccessPointers ()
 Get the access pointers. More...
 

Protected Member Functions

virtual ConnectionGetConnByIP (LPSOCKADDR_IN addr)
 Returns a NetManager::Connection from the given IP address. More...
 
virtual ConnectionGetConnByNum (uint32_t clientnum)
 Returns a NetManager::Connection from the given client number. More...
 
virtual bool HandleUnknownClient (LPSOCKADDR_IN addr, MsgEntry *msg)
 Handles/connects a new client from an unknown IP address. More...
 
- Protected Member Functions inherited from NetBase
bool BuildMessage (psNetPacketEntry *pkt, Connection *&connection, LPSOCKADDR_IN addr)
 This takes incoming packets and rebuilds psMessages from them. More...
 
csPtr< MsgEntryCheckCompleteMessage (uint32_t client, uint32_t id)
 This adds the incoming packet to the pending packets tree, and builds the psMessageBytes struct and MsgEntry struct if complete. More...
 
bool CheckDoublePackets (Connection *connection, psNetPacketEntry *pkt)
 This tries to drop packets that received doubled. More...
 
void CheckFragmentTimeouts (void)
 This checks the list of packets waiting to be assembled into complete messages. More...
 
void CheckResendPkts (void)
 This cycles through set of pkts awaiting ack and resends old ones. More...
 
void Close (bool force=true)
 
bool FilterLogMessage (int type, char dir)
 Check if the given message type should be logged or not. More...
 
int GetIPByName (LPSOCKADDR_IN addr, const char *name)
 some helper functions... More...
 
bool HandleAck (psNetPacketEntry *pkt, Connection *connection, LPSOCKADDR_IN addr)
 This takes incoming packets and examines them for priority. More...
 
void HandleCompletedMessage (MsgEntry *me, Connection *&connection, LPSOCKADDR_IN addr, psNetPacketEntry *pkt)
 This receives only fully reassembled messages and adds to appropriate queues. More...
 
bool Init (bool autobind=true)
 This initialises the socket lib and creates a listening UDP socket, if you're the client you should set port to zero, so a random free port is user. More...
 
int RecvFrom (LPSOCKADDR_IN addr, socklen_t *socklen, void *buf, unsigned int maxsize)
 small inliner for receiving packets... More...
 
bool SendFinalPacket (psNetPacketEntry *pkt)
 Send packet to the clientnum given by clientnum in psNetPacketEntry. More...
 
bool SendFinalPacket (psNetPacketEntry *pkt, LPSOCKADDR_IN addr)
 This only sends out a packet. More...
 
bool SendMergedPackets (NetPacketQueue *q)
 This attempts to merge as many packets as possible into one before sending. More...
 
bool SendSinglePacket (psNetPacketEntry *pkt)
 This does the sending and puts the packet in "awaiting ack" if necessary. More...
 
int SendTo (LPSOCKADDR_IN addr, const void *data, unsigned int size)
 Wrapper to encapsulate the sendto call and provide for retry if the buffer is full. More...
 

Additional Inherited Members

- Public Types inherited from NetBase
enum  broadcasttype {
  BC_EVERYONEBUTSELF = 1, BC_GROUP = 2, BC_GUILD = 3, BC_SECTOR = 4,
  BC_EVERYONE = 5, BC_FINALPACKET = 6
}
 
- Public Attributes inherited from NetBase
struct timeval timeout
 The timeout to use when waiting for new incoming packets. More...
 
- Protected Attributes inherited from NetBase
unsigned int avgIndex
 
csHash< csRef< psNetPacketEntry >, PacketKeyawaitingack
 Packets Awaiting Ack pool. More...
 
csArray< MsgQueue * > inqueues
 Incoming message queue vector. More...
 
csTicks lastSendReport
 
csRef< NetPacketQueueRefCountNetworkQueue
 Outgoing message queue. More...
 
psNetMsgProfilesprofs
 
bool ready
 is the connection ready? More...
 
unsigned int resendIndex
 
size_t resends [RESENDAVGCOUNT]
 
GenericRefQueue< NetPacketQueueRefCount, csWeakRef > senders
 weak referenced list of outbound queues with waiting data so disconnected clients won't receive packets More...
 
SendQueueStats_t sendStats [NETAVGCOUNT]
 
long totalcountin
 total packages transferred by this object More...
 
long totalcountout
 
long totaltransferin
 total bytes transferred by this object More...
 
long totaltransferout
 
- Static Protected Attributes inherited from NetBase
static int socklibrefcount
 System Socket lib initialized? More...
 

Detailed Description

This is the network thread handling packets sending/receiving to/from network other threads can register a message queue and the network thread will sort messages for them and put them in their queue.

Definition at line 49 of file netmanager.h.

Constructor & Destructor Documentation

NetManager::NetManager ( )
NetManager::~NetManager ( )

Member Function Documentation

virtual void NetManager::Broadcast ( MsgEntry me,
int  scope,
int  guild = -1 
)
virtual

This broadcasts the same msg out to a bunch of Clients.

Which clients recieve this message, depend on the scope.DON'T use this in the client app, it's only here for the MsgHandler class!

Parameters
meIs the message to be broadcast to other clients.
scopeDetermines which clients will recieve the broadcasted message. Scope can be one of:
guildIs the ID of the guild of the owner of the message. This is only used when scope is set to BC_GUILD. By default is set to -1 and is unused.
See also
MsgEntry

Implements NetBase.

void NetManager::CheckLinkDead ( void  )

Checks for and deletes link dead clients.

This is called periodicly to detect linkdead clients. If it finds a link dead client then it will delete the client's connection.

static NetManager* NetManager::Create ( CacheManager cacheManager,
int  client_firstmsg,
int  npcclient_firstmsg,
int  timeout = 15000 
)
static
static void NetManager::Destroy ( )
static
Client* NetManager::GetAnyClient ( int  cnum)

Gets a client with the specified id.

Parameters
cnumThe id of the client to retrive.
Returns
Returns a pointer to the specified client or NULL if not found.
Client* NetManager::GetClient ( int  cnum)

Gets a client with the specified id.

Parameters
cnumThe id of the client to retrive.
Returns
Returns a pointer to the specified client or NULL if not found or client not ready.
virtual Connection* NetManager::GetConnByIP ( LPSOCKADDR_IN  addr)
protectedvirtual

Returns a NetManager::Connection from the given IP address.

Parameters
addrIs the IP address of client we want to find the connection for.
Returns
Returns a Connection object that contains the connection for the given IP address.

Implements NetBase.

virtual Connection* NetManager::GetConnByNum ( uint32_t  clientnum)
protectedvirtual

Returns a NetManager::Connection from the given client number.

Parameters
clientnumIs the client number we wish to get the connection for.
Returns
Returns a Connection object that contains the connection for the given client number.

Implements NetBase.

ClientConnectionSet* NetManager::GetConnections ( )
inline

Gets a list of all connected clients.

Returns
Returns a pointer to the list of all the clients.

Definition at line 131 of file netmanager.h.

virtual bool NetManager::HandleUnknownClient ( LPSOCKADDR_IN  addr,
MsgEntry msg 
)
protectedvirtual

Handles/connects a new client from an unknown IP address.

This is called when we have recieved a message from a client that we don't know yet. This happens when a new client connects to us. First thing it does is check to make sure the msgtype is equal to firstmsg. If not then we have recieved some random packet. If firstmsg is accepted then it adds this new client to our client list and then changed the client number on the message so an acknowledge can be sent back later.

Parameters
addrIs the IP address of the client we have not seen before.
msgIs the message recieved from the unknown client.
Returns
Returns true if the client has been verified and connected or false if it is a bad client or failed to connect properly.

Implements NetBase.

bool NetManager::Initialize ( CacheManager cachemanager,
int  client_firstmsg,
int  npcclient_firstmsg,
int  timeout = 15000 
)

Initialize the network thread.

Initializes firstmsg and timeout is the timout in ms for a connection. If no packets are sent or received in this time then the connection is made linkdead.

Parameters
cachemanagerPointer to the cachemanager
client_firstmsgIs the message type that must be first received from a client to determine if it is a valid client.
npcclient_firstmsgIs the message type that must be first received from a npc client to determine if it is a valid npc client.
timeoutIs the timeout value for a connection given in ms. The default value given is 15000ms.
Returns
Returns success or faliure for initializing.
virtual void NetManager::Multicast ( MsgEntry me,
const csArray< PublishDestination > &  multi,
uint32_t  except,
float  range 
)
virtual

Sends the given message me to all the clients in the list.

Sends the given message me to all the clients in the list (clientlist) which is of size count. This will send the message to all the clients except the client which has a client number given in the variable except.

Note
(Brendon): Why is multi not const & ?
Parameters
meIs the message to be sent to other clients.
multiIs a vector of all the clients to send this message to.
exceptIs a client number for a client NOT to send this message to. This would usually be the client trying to send the message.
rangeIs the maximum distance the client must be away to be out of "message reception range".

Implements NetBase.

void NetManager::Run ( )

This is the main thread function.

If it hasn't been initialized it waits until the server is ready to go (i.e. is initialized) if so then it sits in a loop where it calls NetBase::ProcessNetwork() and also checks all the dead links. This method also calculates the data transfer rate.

virtual bool NetManager::SendMessage ( MsgEntry me)
virtual

Sends the given message to the client listed in the message.

MsgEntry has info on who the message is going to.

Parameters
meIs a message MsgEntry which contains the message and the client number to send the message to.
Returns
Returns success or faliure.

Reimplemented from NetBase.

bool NetManager::SendMessageDelayed ( MsgEntry me,
csTicks  delay 
)

Queues the message for sending later, so the calling classes don't have to all manage this themselves.


The documentation for this class was generated from the following file: