Planeshift
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Static Protected Attributes

NetBase Class Reference

This class acts as a base for client/server net classes. More...

#include <netbase.h>

List of all members.

Classes

struct  AccessPointers
 Struct used by MessageCracker and ToString to distribute a number of access pointers. More...
class  Connection
 This class holds data for a connection. More...
struct  LogMsgFilterSetting_t
struct  SendQueueStats_t
 Moving averages. More...

Public Types

enum  broadcasttype {
  BC_EVERYONEBUTSELF = 1, BC_GROUP = 2, BC_GUILD = 3, BC_SECTOR = 4,
  BC_EVERYONE = 5, BC_FINALPACKET = 6
}

Public Member Functions

void AddFilterLogMessage (int type)
 Add a new message type to the LogMessage message filter list.
bool AddMsgQueue (MsgQueue *, objID minID=0, objID maxID=0xffffffff)
 This adds another message queue (for other threads) These queues are for reading off.
bool Bind (const IN_ADDR &addr, int port)
bool Bind (const char *addr, int port)
 Binds the socket to the specified address (only needed on server.
virtual void Broadcast (MsgEntry *me, int scope, int guildID)=0
 Broadcast a message, DON'T USE this function, it's only for MsgHandler!
bool CheckIn (void)
 this receives an Incoming Packet and analyses it
bool Flush (MsgQueue *queue)
 Flush all messages in given queue.
iEngine * GetEngine ()
 Get the Engine.
csTicks GetPing (void)
psNetMsgProfilesGetProfs ()
uint32_t GetRandomID ()
 return a random ID that can be used for messages
void InvertLogMessageFilter ()
 Invert the LogMessage filter.
bool IsReady ()
 Is this connection ready to use?
csString LogMessageFilter (const char *arg)
 Pars and configure user filter settings.
void LogMessageFilterClear ()
 Clear all message type from the LogMessage message filter list.
void LogMessageFilterDump ()
 Dump the current filter settings.
void LogMessages (char dir, MsgEntry *me)
 Log the message to LOG_MESSAGE.
virtual void Multicast (MsgEntry *me, const csArray< PublishDestination > &multi, uint32_t except, float range)=0
 Sends the given message me to all the clients in the list.
 NetBase (int outqueuelen=100)
 you can specify how much messages the ouptput queue can contain before being full.
void ProcessNetwork (csTicks timeout)
 This function is the heart of NetBase - it look for new incoming packets and sends packets in the outgoing queue.
bool QueueMessage (MsgEntry *me)
 This adds a completed message to any queues that are signed up.
void RemoveFilterLogMessage (int type)
 Remove a message type from the LogMessage message filter list.
void RemoveMsgQueue (MsgQueue *)
 this removes a queue
virtual bool SendMessage (MsgEntry *me)
 Put a message into the outgoing queue.
virtual bool SendMessage (MsgEntry *me, NetPacketQueueRefCount *queue)
bool SendOut (void)
 sendOut sends the next packet in the outgoing message queue
void SetEngine (iEngine *engine)
 Set the Engine.
void SetLogMessageFilterHex (bool filterhex)
 Set the filter hex messages flag.
void SetMsgStrings (csStringSet *msgstrings, csStringHashReversible *msgstringshash)
 Set the MsgString Hash.
void ToggleReceiveMessageFilter ()
 Toggle the global receive LogMessage filter.
void ToggleSendMessageFilter ()
 Toggle the global send LogMessage filter.
virtual ~NetBase ()

Static Public Member Functions

static AccessPointersGetAccessPointers ()
 Get the access pointers.

Public Attributes

struct timeval timeout
 The timeout to use when waiting for new incoming packets.

Protected Member Functions

bool BuildMessage (psNetPacketEntry *pkt, Connection *&connection, LPSOCKADDR_IN addr)
 This takes incoming packets and rebuilds psMessages from them.
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.
bool CheckDoublePackets (Connection *connection, psNetPacketEntry *pkt)
 This tries to drop packets that received doubled.
void CheckFragmentTimeouts (void)
 This checks the list of packets waiting to be assembled into complete messages.
void CheckResendPkts (void)
 This cycles through set of pkts awaiting ack and resends old ones.
void Close (bool force=true)
bool FilterLogMessage (int type, char dir)
 Check if the given message type should be logged or not.
virtual ConnectionGetConnByIP (LPSOCKADDR_IN addr)=0
virtual ConnectionGetConnByNum (uint32_t clientnum)=0
int GetIPByName (LPSOCKADDR_IN addr, const char *name)
 some helper functions...
bool HandleAck (psNetPacketEntry *pkt, Connection *connection, LPSOCKADDR_IN addr)
 This takes incoming packets and examines them for priority.
void HandleCompletedMessage (MsgEntry *me, Connection *&connection, LPSOCKADDR_IN addr, psNetPacketEntry *pkt)
 This receives only fully reassembled messages and adds to appropriate queues.
virtual bool HandleUnknownClient (LPSOCKADDR_IN addr, MsgEntry *data)=0
 This function is called when we receive packets from an unknown client.
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.
int RecvFrom (LPSOCKADDR_IN addr, socklen_t *socklen, void *buf, unsigned int maxsize)
 small inliner for receiving packets...
bool SendFinalPacket (psNetPacketEntry *pkt)
 Send packet to the clientnum given by clientnum in psNetPacketEntry.
bool SendFinalPacket (psNetPacketEntry *pkt, LPSOCKADDR_IN addr)
 This only sends out a packet.
bool SendMergedPackets (NetPacketQueue *q)
 This attempts to merge as many packets as possible into one before sending.
bool SendSinglePacket (psNetPacketEntry *pkt)
 This does the sending and puts the packet in "awaiting ack" if necessary.
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.

Protected Attributes

unsigned int avgIndex
csHash< csRef
< psNetPacketEntry >
, PacketKey
awaitingack
 Packets Awaiting Ack pool.
csArray< MsgQueue * > inqueues
 Incoming message queue vector.
csTicks lastSendReport
csRef< NetPacketQueueRefCountNetworkQueue
 Outgoing message queue.
psNetMsgProfilesprofs
bool ready
 is the connection ready?
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
SendQueueStats_t sendStats [NETAVGCOUNT]
long totalcountin
 total packages transferred by this object
long totalcountout
long totaltransferin
 total bytes transferred by this object
long totaltransferout

Static Protected Attributes

static int socklibrefcount
 System Socket lib initialized?

Detailed Description

This class acts as a base for client/server net classes.

It tries to define as much common used code as possible while not trying to slow things down because of the generalisation

Definition at line 116 of file netbase.h.


Member Enumeration Documentation

Enumerator:
BC_EVERYONEBUTSELF 
BC_GROUP 
BC_GUILD 
BC_SECTOR 
BC_EVERYONE 
BC_FINALPACKET 

Definition at line 241 of file netbase.h.


Constructor & Destructor Documentation

NetBase::NetBase ( int  outqueuelen = 100)

you can specify how much messages the ouptput queue can contain before being full.

100 should be enough for the Client, but the server should increase this number

virtual NetBase::~NetBase ( ) [virtual]

Member Function Documentation

void NetBase::AddFilterLogMessage ( int  type)

Add a new message type to the LogMessage message filter list.

Parameters:
typeThe type of message to filter when loging
bool NetBase::AddMsgQueue ( MsgQueue ,
objID  minID = 0,
objID  maxID = 0xffffffff 
)

This adds another message queue (for other threads) These queues are for reading off.

Selection of the messages is done by a minimum and maximum ObjID

bool NetBase::Bind ( const char *  addr,
int  port 
)

Binds the socket to the specified address (only needed on server.

bool NetBase::Bind ( const IN_ADDR &  addr,
int  port 
)
virtual void NetBase::Broadcast ( MsgEntry me,
int  scope,
int  guildID 
) [pure virtual]

Broadcast a message, DON'T USE this function, it's only for MsgHandler!

Implemented in psNetConnection, and NetManager.

bool NetBase::BuildMessage ( psNetPacketEntry pkt,
Connection *&  connection,
LPSOCKADDR_IN  addr 
) [protected]

This takes incoming packets and rebuilds psMessages from them.

If/when a complete message is reassembled, it calls HandleCompletedMessage().

csPtr<MsgEntry> NetBase::CheckCompleteMessage ( uint32_t  client,
uint32_t  id 
) [protected]

This adds the incoming packet to the pending packets tree, and builds the psMessageBytes struct and MsgEntry struct if complete.

bool NetBase::CheckDoublePackets ( Connection connection,
psNetPacketEntry pkt 
) [protected]

This tries to drop packets that received doubled.

void NetBase::CheckFragmentTimeouts ( void  ) [protected]

This checks the list of packets waiting to be assembled into complete messages.

It forms a list of up to 10 (may be changed, check code) packets which are older than 10 seconds of age. These packets are removed from the list. This should usually be called with the same frequency as CheckResendPkts() though their functionality is not related.

bool NetBase::CheckIn ( void  )

this receives an Incoming Packet and analyses it

void NetBase::CheckResendPkts ( void  ) [protected]

This cycles through set of pkts awaiting ack and resends old ones.

This function must be called periodically by an outside agent, such as NetManager.

void NetBase::Close ( bool  force = true) [protected]
bool NetBase::FilterLogMessage ( int  type,
char  dir 
) [protected]

Check if the given message type should be logged or not.

Parameters:
typeThe message type to check for filtering
dirThe direction 'R' or 'S'
bool NetBase::Flush ( MsgQueue queue)

Flush all messages in given queue.

Parameters:
queueThe queue to flush
Returns:
True if all messages flushed.
static AccessPointers* NetBase::GetAccessPointers ( ) [inline, static]

Get the access pointers.

Definition at line 272 of file netbase.h.

virtual Connection* NetBase::GetConnByIP ( LPSOCKADDR_IN  addr) [protected, pure virtual]

Implemented in psNetConnection, and NetManager.

virtual Connection* NetBase::GetConnByNum ( uint32_t  clientnum) [protected, pure virtual]

Implemented in psNetConnection, and NetManager.

iEngine* NetBase::GetEngine ( ) [inline]

Get the Engine.

Definition at line 269 of file netbase.h.

int NetBase::GetIPByName ( LPSOCKADDR_IN  addr,
const char *  name 
) [protected]

some helper functions...

the getConnBy functions should be reimplemented in the client/server classes.

csTicks NetBase::GetPing ( void  ) [inline]

Definition at line 251 of file netbase.h.

psNetMsgProfiles* NetBase::GetProfs ( ) [inline]

Definition at line 253 of file netbase.h.

uint32_t NetBase::GetRandomID ( )

return a random ID that can be used for messages

bool NetBase::HandleAck ( psNetPacketEntry pkt,
Connection connection,
LPSOCKADDR_IN  addr 
) [protected]

This takes incoming packets and examines them for priority.

If pkt is ACK, it finds the awaiting ack pkt and removes it.

void NetBase::HandleCompletedMessage ( MsgEntry me,
Connection *&  connection,
LPSOCKADDR_IN  addr,
psNetPacketEntry pkt 
) [protected]

This receives only fully reassembled messages and adds to appropriate queues.

If pkt is HIGH priority, it creates relevant ack pkts and queues it to send back.

virtual bool NetBase::HandleUnknownClient ( LPSOCKADDR_IN  addr,
MsgEntry data 
) [protected, pure virtual]

This function is called when we receive packets from an unknown client.

This is useful for the server to handle clients that are just connecting

Implemented in psNetConnection, and NetManager.

bool NetBase::Init ( bool  autobind = true) [protected]

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.

void NetBase::InvertLogMessageFilter ( ) [inline]

Invert the LogMessage filter.

Definition at line 312 of file netbase.h.

bool NetBase::IsReady ( ) [inline]

Is this connection ready to use?

Definition at line 200 of file netbase.h.

csString NetBase::LogMessageFilter ( const char *  arg)

Pars and configure user filter settings.

Parameters:
argUser command argumets
void NetBase::LogMessageFilterClear ( )

Clear all message type from the LogMessage message filter list.

void NetBase::LogMessageFilterDump ( )

Dump the current filter settings.

void NetBase::LogMessages ( char  dir,
MsgEntry me 
)

Log the message to LOG_MESSAGE.

Parameters:
dirShould be R for received messages, S for sent messages and I for internal.
meLogged message.
virtual void NetBase::Multicast ( MsgEntry me,
const csArray< PublishDestination > &  multi,
uint32_t  except,
float  range 
) [pure 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".

Implemented in psNetConnection, and NetManager.

void NetBase::ProcessNetwork ( csTicks  timeout)

This function is the heart of NetBase - it look for new incoming packets and sends packets in the outgoing queue.

This is thought of being called from a thread in the server or regularely (between frames?) in the client It loops until both send and receive are done with all their queues or until the current time has passed timeout, then returns to caller.

bool NetBase::QueueMessage ( MsgEntry me)

This adds a completed message to any queues that are signed up.

int NetBase::RecvFrom ( LPSOCKADDR_IN  addr,
socklen_t *  socklen,
void *  buf,
unsigned int  maxsize 
) [inline, protected]

small inliner for receiving packets...

This just encapsulates the lowlevel socket funcs

Definition at line 451 of file netbase.h.

void NetBase::RemoveFilterLogMessage ( int  type)

Remove a message type from the LogMessage message filter list.

Parameters:
typeThe type of message to not filter.
void NetBase::RemoveMsgQueue ( MsgQueue )

this removes a queue

bool NetBase::SendFinalPacket ( psNetPacketEntry pkt,
LPSOCKADDR_IN  addr 
) [protected]

This only sends out a packet.

bool NetBase::SendFinalPacket ( psNetPacketEntry pkt) [protected]

Send packet to the clientnum given by clientnum in psNetPacketEntry.

bool NetBase::SendMergedPackets ( NetPacketQueue q) [protected]

This attempts to merge as many packets as possible into one before sending.

It empties the passed queue.

virtual bool NetBase::SendMessage ( MsgEntry me,
NetPacketQueueRefCount queue 
) [virtual]
virtual bool NetBase::SendMessage ( MsgEntry me) [virtual]

Put a message into the outgoing queue.

Reimplemented in NetManager.

bool NetBase::SendOut ( void  )

sendOut sends the next packet in the outgoing message queue

bool NetBase::SendSinglePacket ( psNetPacketEntry pkt) [protected]

This does the sending and puts the packet in "awaiting ack" if necessary.

int NetBase::SendTo ( LPSOCKADDR_IN  addr,
const void *  data,
unsigned int  size 
) [inline, protected]

Wrapper to encapsulate the sendto call and provide for retry if the buffer is full.

Definition at line 354 of file netbase.h.

void NetBase::SetEngine ( iEngine *  engine) [inline]

Set the Engine.

Definition at line 266 of file netbase.h.

void NetBase::SetLogMessageFilterHex ( bool  filterhex) [inline]

Set the filter hex messages flag.

Definition at line 327 of file netbase.h.

void NetBase::SetMsgStrings ( csStringSet *  msgstrings,
csStringHashReversible *  msgstringshash 
) [inline]

Set the MsgString Hash.

Definition at line 259 of file netbase.h.

void NetBase::ToggleReceiveMessageFilter ( ) [inline]

Toggle the global receive LogMessage filter.

Definition at line 317 of file netbase.h.

void NetBase::ToggleSendMessageFilter ( ) [inline]

Toggle the global send LogMessage filter.

Definition at line 322 of file netbase.h.


Member Data Documentation

Definition at line 633 of file netbase.h.

csHash<csRef<psNetPacketEntry>, PacketKey> NetBase::awaitingack [protected]

Packets Awaiting Ack pool.

Definition at line 611 of file netbase.h.

Incoming message queue vector.

Definition at line 608 of file netbase.h.

csTicks NetBase::lastSendReport [protected]

Definition at line 632 of file netbase.h.

Outgoing message queue.

Definition at line 602 of file netbase.h.

Definition at line 638 of file netbase.h.

bool NetBase::ready [protected]

is the connection ready?

Definition at line 617 of file netbase.h.

Definition at line 636 of file netbase.h.

size_t NetBase::resends[RESENDAVGCOUNT] [protected]

Definition at line 635 of file netbase.h.

weak referenced list of outbound queues with waiting data so disconnected clients won't receive packets

Definition at line 605 of file netbase.h.

SendQueueStats_t NetBase::sendStats[NETAVGCOUNT] [protected]

Definition at line 631 of file netbase.h.

int NetBase::socklibrefcount [static, protected]

System Socket lib initialized?

Definition at line 614 of file netbase.h.

struct timeval NetBase::timeout

The timeout to use when waiting for new incoming packets.

Definition at line 256 of file netbase.h.

long NetBase::totalcountin [protected]

total packages transferred by this object

Definition at line 622 of file netbase.h.

long NetBase::totalcountout [protected]

Definition at line 622 of file netbase.h.

long NetBase::totaltransferin [protected]

total bytes transferred by this object

Definition at line 620 of file netbase.h.

long NetBase::totaltransferout [protected]

Definition at line 620 of file netbase.h.


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