00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef NL_CALLBACK_NET_BASE_H
00027 #define NL_CALLBACK_NET_BASE_H
00028
00029 #undef USE_MESSAGE_RECORDER
00030
00031 #include "nel/misc/types_nl.h"
00032 #include "nel/misc/time_nl.h"
00033
00034 #include "nel/net/buf_net_base.h"
00035 #include "nel/net/message.h"
00036 #include "nel/net/inet_address.h"
00037
00038 #ifdef USE_MESSAGE_RECORDER
00039 #include "nel/net/message_recorder.h"
00040 #include <queue>
00041 #endif
00042
00043 #include <vector>
00044
00045
00046 namespace NLNET {
00047
00048 class CCallbackNetBase;
00049
00055 typedef void (*TMsgCallback) (CMessage &msgin, TSockId from, CCallbackNetBase &netbase);
00056
00057
00059 typedef struct
00060 {
00062 char *Key;
00064 TMsgCallback Callback;
00065
00066 } TCallbackItem;
00067
00068
00075 class CCallbackNetBase
00076 {
00077 public:
00078 virtual ~CCallbackNetBase() {}
00079
00080
00085 virtual void send (const CMessage &buffer, TSockId hostid = InvalidSockId, bool log = true) = 0;
00086
00087 uint64 getBytesSent () { return _BytesSent; }
00088 uint64 getBytesReceived () { return _BytesReceived; }
00089
00090 virtual uint64 getReceiveQueueSize () = 0;
00091 virtual uint64 getSendQueueSize () = 0;
00092
00097 virtual bool flush (TSockId hostid = InvalidSockId) = 0;
00098
00102 void addCallbackArray (const TCallbackItem *callbackarray, NLMISC::CStringIdArray::TStringId arraysize);
00103
00105 void setDefaultCallback(TMsgCallback defaultCallback) { _DefaultCallback = defaultCallback; }
00106
00108 void setDisconnectionCallback (TNetCallback cb, void *arg) { checkThreadId (); _DisconnectionCallback = cb; _DisconnectionCbArg = arg; }
00109
00111 virtual TSockId getSockId (TSockId hostid = InvalidSockId) = 0;
00112
00118 void authorizeOnly (const char *callbackName, TSockId hostid = InvalidSockId);
00119
00121 bool isAServer () const { checkThreadId (); return _IsAServer; }
00122
00124 NLMISC::CStringIdArray &getSIDA () { return _InputSIDA; }
00125
00127 virtual bool dataAvailable () { nlstop; return false; }
00129 virtual void update ( sint32 timeout=0 ) { nlstop; }
00131 virtual bool connected () const { nlstop; return false; }
00133 virtual void disconnect (TSockId hostid = InvalidSockId) { nlstop; }
00134
00136 virtual const CInetAddress& hostAddress (TSockId hostid);
00137
00139 void sendAllMyAssociations (TSockId to);
00140
00145 void setOtherSideAssociations (const char **associationarray, NLMISC::CStringIdArray::TStringId arraysize);
00146
00147 void displayAllMyAssociations ();
00148
00152 void ignoreAllUnknownId(bool b)
00153 {
00154 _InputSIDA.ignoreAllUnknownId (b);
00155 }
00156
00157
00158 enum TRecordingState { Off, Record, Replay };
00159
00160 protected:
00161
00162 uint64 _BytesSent, _BytesReceived;
00163
00165 TNetCallback _NewDisconnectionCallback;
00166
00168 CCallbackNetBase( TRecordingState rec=Off, const std::string& recfilename="", bool recordall=true );
00169
00171 void baseUpdate ( sint32 timeout=0 );
00172
00174 void processOneMessage ();
00175
00177 virtual void receive (CMessage &buffer, TSockId *hostid) = 0;
00178
00179
00180
00181
00182
00183 NLMISC::CStringIdArray _InputSIDA;
00184
00185
00186
00187
00188 NLMISC::CStringIdArray _OutputSIDA;
00189
00190
00191 std::vector<TCallbackItem> _CallbackArray;
00192
00193
00194 TMsgCallback _DefaultCallback;
00195
00196 bool _IsAServer;
00197 bool _FirstUpdate;
00198
00199
00200 #ifdef USE_MESSAGE_RECORDER
00201 bool replayDataAvailable();
00202 virtual bool replaySystemCallbacks() = 0;
00203 void noticeDisconnection( TSockId hostid );
00204
00205 TRecordingState _MR_RecordingState;
00206 sint64 _MR_UpdateCounter;
00207
00208 CMessageRecorder _MR_Recorder;
00209 #endif
00210
00211
00212 private:
00213
00214 NLMISC::TTime _LastUpdateTime;
00215 NLMISC::TTime _LastMovedStringArray;
00216
00217 TNetCallback _DisconnectionCallback;
00218 void *_DisconnectionCbArg;
00219
00220 friend void cbnbMessageAskAssociations (CMessage &msgin, TSockId from, CCallbackNetBase &netbase);
00221 friend void cbnbMessageRecvAssociations (CMessage &msgin, TSockId from, CCallbackNetBase &netbase);
00222
00223 friend void cbnbNewDisconnection (TSockId from, void *data);
00224
00225 protected:
00227 uint _ThreadId;
00228 void checkThreadId () const;
00229 };
00230
00231
00232 }
00233
00234
00235 #endif // NL_CALLBACK_NET_BASE_H
00236
00237