29 #include <csutil/ref.h> 30 #include <csutil/weakref.h> 31 #include <csutil/weakreferenced.h> 32 #include <csutil/refcount.h> 33 #include <csutil/strset.h> 34 #include <csutil/array.h> 41 #define NUM_BROADCAST 0xffffffff 42 #define MAXQUEUESIZE 20000 43 #define MAXCLIENTQUEUESIZE 5000 44 #define MAXPACKETHISTORY 1009 // Should be a prime to improve performance of hash 47 #define NETAVGCOUNT 400 48 #define RESENDAVGCOUNT 200 53 #define SENDTO_MAX_RETRIES 200 55 #define SENDTO_SELECT_TIMEOUT_SEC 0 57 #define SENDTO_SELECT_TIMEOUT_USEC 10000 81 class csStringHashReversible;
94 #ifndef CS_PLATFORM_WIN32 95 #define WSAEWOULDBLOCK EAGAIN 106 PublishDestination(
int client,
void*
object,
float dist,
float min_dist) : client(client), object(object), dist(dist), min_dist(min_dist) {}
139 const char* Request(csStringID
id)
const;
146 csStringID Request(
const char*
string)
const;
171 virtual bool SendMessage (
MsgEntry* me);
177 virtual void Broadcast (
MsgEntry* me,
int scope,
int guildID) = 0;
195 virtual void Multicast (
MsgEntry* me,
const csArray<PublishDestination>& multi, uint32_t except,
float range) = 0;
214 void ProcessNetwork (csTicks timeout);
231 bool Bind(
const char* addr,
int port);
232 bool Bind(
const IN_ADDR &addr,
int port);
243 BC_EVERYONEBUTSELF = 1,
251 csTicks
GetPing(
void) {
return netInfos.GetAveragePingTicks();}
256 struct timeval timeout;
259 void SetMsgStrings(csStringSet* msgstrings, csStringHashReversible* msgstringshash)
261 accessPointers.msgstrings = msgstrings;
262 accessPointers.msgstringshash = msgstringshash;
266 void SetEngine(iEngine* engine) { accessPointers.engine = engine; }
280 void LogMessages(
char dir,
MsgEntry* me);
287 csString LogMessageFilter(
const char *arg);
294 void AddFilterLogMessage(
int type);
301 void RemoveFilterLogMessage(
int type);
307 void LogMessageFilterClear();
332 void LogMessageFilterDump();
335 uint32_t GetRandomID();
344 bool FilterLogMessage(
int type,
char dir);
356 struct timeval timeout;
369 FILE *f = fopen(
"hexdump.txt",
"a");
374 ptr = (
const char *)data;
375 len = ((40<size) ? 40 : size);
376 fprintf(f,
"\n------------\nSending %d bytes:\n",len);
377 for ( i = 0 ; i < len ; i++ )
378 fprintf(f,
"%02x " ,(*(ptr+i)&0xff));
379 for ( i = len ; i < 16 ; i++ )
382 for ( i = 0 ; i < len ; i++ )
391 fprintf(f,
"\n-----------------\n");
403 #ifdef CS_PLATFORM_WIN32 409 printf(
"In while loop on EAGAIN... retry #%d.\n", retries);
414 FD_SET(mysocket,&wfds);
417 memset(&timeout,0,
sizeof(
struct timeval));
432 totaltransferout += size;
437 #ifdef CS_PLATFORM_WIN32 438 Error2(
"NetBase::SendTo() gave up trying to send a packet with errno=%d.", WSAGetLastError());
440 Error2(
"NetBase::SendTo() gave up trying to send a packet with errno=%d.", errno);
452 unsigned int maxsize)
462 FD_SET (mysocket, &
set);
463 #ifndef CS_PLATFORM_WIN32 465 FD_SET (pipe_fd[0], &
set);
469 struct timeval prevTimeout = timeout;
472 if (
SOCK_SELECT(csMax(mysocket, pipe_fd[0]) + 1, &
set, NULL, NULL, &timeout) < 1)
474 timeout = prevTimeout;
478 #ifndef CS_PLATFORM_WIN32 479 if(FD_ISSET(pipe_fd[0], &
set))
482 if(read(pipe_fd[0], throwaway, 32) == -1)
489 timeout = prevTimeout;
491 if(!FD_ISSET(mysocket, &
set))
498 totaltransferin += err;
513 virtual Connection *GetConnByNum (uint32_t clientnum) = 0;
526 bool Init(
bool autobind =
true);
528 void Close(
bool force =
true);
541 void CheckResendPkts(
void);
556 void CheckFragmentTimeouts(
void);
562 csPtr<MsgEntry> CheckCompleteMessage(uint32_t
client,uint32_t
id);
569 void HandleCompletedMessage(
MsgEntry *me,
648 csHash<csRef<psNetPacketEntry> ,
PacketKey> packets;
653 csRandomGen* randomgen;
656 CS::Threading::Mutex mutex;
665 csArray<int> logmessagefilter;
672 } LogMsgFilterSetting_t;
674 LogMsgFilterSetting_t logmsgfiltersetting;
static AccessPointers * GetAccessPointers()
Get the access pointers.
This class holds data for a connection.
void SetLogMessageFilterHex(bool filterhex)
Set the filter hex messages flag.
virtual ~NetPacketQueueRefCount()
#define SOCK_RECVFROM(a, b, c, d, e, f)
int heartbeat
number of attempts to keep alive connection without ack response
bool ready
is the connection ready?
csHash< csRef< psNetPacketEntry >, PacketKey > awaitingack
Packets Awaiting Ack pool.
static int socklibrefcount
System Socket lib initialized?
bool valid
Is this already a valid connection?
NetPacketQueueRefCount(int qlen)
float devRTT
RTT deviance.
Gives informations about the network connection.
uint32_t GetNextPacketID()
Struct used by MessageCracker and ToString to distribute a number of access pointers.
GenericRefQueue< MsgEntry > MsgQueue
float estRTT
Estimated RTT.
uint32_t resends
Number of resends.
The structure of 1 queue entry (pointer to a message)
#define SOCK_SELECT(max, read, write, except, timeout)
int RecvFrom(LPSOCKADDR_IN addr, socklen_t *socklen, void *buf, unsigned int maxsize)
small inliner for receiving packets...
void SetEngine(iEngine *engine)
Set the Engine.
csStringHashReversible * msgstringshash
void RemoveFromWindow(uint32_t bytes)
Remove from transmission window when an ack is received.
uint32_t clientnum
the client num
#define SENDTO_SELECT_TIMEOUT_SEC
PublishDestination(int client, void *object, float dist, float min_dist)
int pcknumin
The Number of the last incoming packet.
void SetMsgStrings(csStringSet *msgstrings, csStringHashReversible *msgstringshash)
Set the MsgString Hash.
csHash< uint32_t > packethistoryhash
const unsigned int WINDOW_MAX_SIZE
int pcknumout
The Number of the last outgoing packet.
#define SENDTO_SELECT_TIMEOUT_USEC
GenericRefQueue< psNetPacketEntry > NetPacketQueue
void ToggleSendMessageFilter()
Toggle the global send LogMessage filter.
SOCKADDR_IN addr
The INet Adress of the client.
#define SENDTO_MAX_RETRIES
csString nameAddr
The adress if provided, usually in clients, else an empty csstring.
This class acts as a base for client/server net classes.
psNetMsgProfiles * GetProfs()
void SetPending(bool flag)
This flag ensures the same object is not queued twice. Doesn't have to mutexed here because these are...
csRef< NetPacketQueueRefCount > NetworkQueue
Outgoing message queue.
void AddToWindow(uint32_t bytes)
Add to window when reliable data is in transit.
iEngine * GetEngine()
Get the Engine.
A queue of smart pointers with locking facilties for multi-threading.
bool IsWindowFull()
Check if the reliable transmission window is full.
void InvertLogMessageFilter()
Invert the LogMessage filter.
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.
#define SOCK_SENDTO(a, b, c, d, e, f)
bool IsReady()
Is this connection ready to use?
uint32_t sends
Number of reliable sends.
GenericRefQueue< NetPacketQueueRefCount, csWeakRef > senders
weak referenced list of outbound queues with waiting data so disconnected clients won't receive packe...
void ToggleReceiveMessageFilter()
Toggle the global receive LogMessage filter.
Statistics of receiving or sending of network messages.
void * buf
buffer for split up packets, allocated when needed
csArray< MsgQueue * > inqueues
Incoming message queue vector.
csTicks lastRecvPacketTime
last time packet was received from this connection