Planeshift
|
This class acts as a base for client/server net classes. More...
#include <netbase.h>
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) |
psNetMsgProfiles * | GetProfs () |
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 AccessPointers * | GetAccessPointers () |
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< MsgEntry > | CheckCompleteMessage (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 Connection * | GetConnByIP (LPSOCKADDR_IN addr)=0 |
virtual Connection * | GetConnByNum (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< NetPacketQueueRefCount > | NetworkQueue |
Outgoing message queue. | |
psNetMsgProfiles * | profs |
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? |
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
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] |
void NetBase::AddFilterLogMessage | ( | int | type | ) |
Add a new message type to the LogMessage message filter list.
type | The type of message to filter when loging |
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 | ||
) |
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.
type | The message type to check for filtering |
dir | The direction 'R' or 'S' |
bool NetBase::Flush | ( | MsgQueue * | queue | ) |
Flush all messages in given queue.
queue | The queue to flush |
static AccessPointers* NetBase::GetAccessPointers | ( | ) | [inline, static] |
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.
int NetBase::GetIPByName | ( | LPSOCKADDR_IN | addr, |
const char * | name | ||
) | [protected] |
some helper functions...
the getConnBy functions should be reimplemented in the client/server classes.
psNetMsgProfiles* NetBase::GetProfs | ( | ) | [inline] |
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] |
bool NetBase::IsReady | ( | ) | [inline] |
csString NetBase::LogMessageFilter | ( | const char * | arg | ) |
Pars and configure user filter settings.
arg | User 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.
dir | Should be R for received messages, S for sent messages and I for internal. |
me | Logged 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.
me | Is the message to be sent to other clients. |
multi | Is a vector of all the clients to send this message to. |
except | Is a client number for a client NOT to send this message to. This would usually be the client trying to send the message. |
range | Is 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.
void NetBase::RemoveFilterLogMessage | ( | int | type | ) |
Remove a message type from the LogMessage message filter list.
type | The 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.
void NetBase::SetEngine | ( | iEngine * | engine | ) | [inline] |
void NetBase::SetLogMessageFilterHex | ( | bool | filterhex | ) | [inline] |
void NetBase::SetMsgStrings | ( | csStringSet * | msgstrings, |
csStringHashReversible * | msgstringshash | ||
) | [inline] |
void NetBase::ToggleReceiveMessageFilter | ( | ) | [inline] |
void NetBase::ToggleSendMessageFilter | ( | ) | [inline] |
unsigned int NetBase::avgIndex [protected] |
csHash<csRef<psNetPacketEntry>, PacketKey> NetBase::awaitingack [protected] |
csArray<MsgQueue*> NetBase::inqueues [protected] |
csTicks NetBase::lastSendReport [protected] |
csRef<NetPacketQueueRefCount> NetBase::NetworkQueue [protected] |
psNetMsgProfiles* NetBase::profs [protected] |
bool NetBase::ready [protected] |
unsigned int NetBase::resendIndex [protected] |
size_t NetBase::resends[RESENDAVGCOUNT] [protected] |
GenericRefQueue<NetPacketQueueRefCount, csWeakRef > NetBase::senders [protected] |
SendQueueStats_t NetBase::sendStats[NETAVGCOUNT] [protected] |
int NetBase::socklibrefcount [static, protected] |
struct timeval NetBase::timeout |
long NetBase::totalcountin [protected] |
long NetBase::totalcountout [protected] |
long NetBase::totaltransferin [protected] |
long NetBase::totaltransferout [protected] |