include/df/msgsvr.hpp

00001 #define MSG_MAXSIZE (4*1024)  //DF/1.3 : increased from 512
00002 #define MSG_MAXMSGS 64
00003 
00004 //use ::SendMsg() to send a message
00005 //use Event::MSG_NETWORK_DATA when you receive a message (id = MsgServerSession or MsgClient)
00006 
00007 class MsgServerSession : public NetworkSession {
00008     friend class MsgServer;
00009   _PROTECTED_:
00010     ExclusiveUsage UseData;
00011     uint32 MaxSize;
00012     uint32 MaxMsgs;
00013     uint32 curReadSize;  //current packet read so far
00014     uint32 curDataSize;  //current packet size
00015     BOOL gotlen;
00016     void SetError(char *str);
00017     PTR *DataStack;
00018     uint32 *SizeStack;
00019     int StackPos;  //0 = empty MaxMags = FULL
00020   public:
00021     //public data/members
00022     PTR Data;
00023     uint32 DataSize;
00024     void Process();
00025     BOOL ClearData();
00026     void SetMaxSize(uint32 NewSize) {MaxSize = NewSize;}
00027     void SetMaxMsgs(uint32 NewSize) {
00028       if (DataStack) return;
00029       MaxMsgs = NewSize;
00030     }
00031     BOOL SendMsg(PTR, int32 len);
00032     BOOL SendMsg(char *str);
00033     BOOL SendMsg(char c);
00034     //ctor/dtor
00035     MsgServerSession() {
00036       MaxSize = MSG_MAXSIZE;
00037       MaxMsgs = MSG_MAXMSGS;
00038       StackPos = 0;
00039       Data = NULL;
00040       DataStack = NULL;
00041       SizeStack = (uint32*)NULL;
00042       gotlen = FALSE;
00043       curReadSize = 0;
00044     }
00045     ~MsgServerSession();
00046     void AsyncRead();   //inbound data ready
00047 };
00048 
00049 class MsgServer : public NetworkServer {
00050   _PROTECTED_:
00051     /*
00052         Array<IPAddr> ClientsList;
00053         ExclusiveUsage ClientsListUsage;
00054     */
00055   public:
00056     int16 StartServer(int16 port);
00057     /*
00058         void AddClient(IPAddr *);
00059         BOOL DeleteClient(IPAddr *);
00060         BOOL IsClientValid(Network *);
00061         MsgServer() {
00062           IPAddr ip;
00063           ip.x[0]=127;
00064           ip.x[1]=0;
00065           ip.x[2]=0;
00066           ip.x[3]=1;
00067           AddClient(&ip);   //always trust local PC
00068         }
00069     */
00070 };
00071 
00072 class MsgClient : public Network, public SystemCode {
00073   _PROTECTED_:
00074     ExclusiveUsage UseData;
00075     IPAddr ip;
00076     uint16 port;
00077     BOOL gotlen;
00078     uint32 MaxSize;
00079     uint32 MaxMsgs;
00080     uint32 curReadSize;
00081     uint32 curDataSize;
00082     void SetError(char *str);
00083     PTR *DataStack;
00084     uint32 *SizeStack;
00085     int StackPos;  //0 = empty MaxMags = FULL
00086   public:
00087     BOOL Valid;
00088     MsgClient() {
00089       Valid = FALSE;
00090       MaxSize = MSG_MAXSIZE;
00091       MaxMsgs = MSG_MAXMSGS;
00092       Data = NULL;
00093       StackPos = 0;
00094       DataStack = NULL;
00095       SizeStack = (uint32*)NULL;
00096       gotlen = FALSE;
00097       curReadSize = 0;
00098     }
00099     ~MsgClient();
00100     void AsyncRead();   //inbound data ready
00101     void AsyncClose();  //connection lost
00102     void Process();
00103     //public data/members
00104     PTR Data;
00105     uint32 DataSize;
00106     BOOL SendMsg(PTR, int32 len);
00107     BOOL SendMsg(char *str);
00108     BOOL SendMsg(char c);
00109     BOOL Connect(IPAddr _ip, uint32 _port);
00110     BOOL Disconnect();
00111     BOOL ClearData();
00112     void SetMaxSize(uint32 NewSize) {MaxSize = NewSize;}
00113     void SetMaxMsgs(uint32 NewSize) {
00114       if (DataStack) return;
00115       MaxMsgs = NewSize;
00116     }
00117 };
00118 

Generated on Mon Mar 5 09:49:14 2007 for DigiForce by  doxygen 1.4.7