lugre_net.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef LUGRE_NET_H
00025 #define LUGRE_NET_H
00026
00027 #ifdef WIN32
00028 #include <winsock2.h>
00029 #endif
00030
00031 #include "lugre_fifo.h"
00032 #include "lugre_smartptr.h"
00033 #include <set>
00034 #include <string>
00035
00036
00037 #ifndef INVALID_SOCKET
00038 #define INVALID_SOCKET (SOCKET)(~0)
00039 #endif
00040
00041 #ifndef SOCKET
00042 #define SOCKET int
00043 #endif
00044
00045 #ifndef SOCKET_ERROR
00046 #define SOCKET_ERROR -1
00047 #endif
00048
00049
00052
00053
00054 class lua_State;
00055
00056 namespace Lugre {
00057
00058 class cConnection;
00059 class cBroadcast;
00060 class cNetListener;
00061
00062
00063
00064
00065 class cNet {
00066 public:
00067 std::set<cNetListener*> mlListener;
00068 std::set<cConnection*> mlCons;
00069 std::set<cConnection*> mlDyingCons;
00070 std::set<cConnection*> mlDeadCons;
00071 std::set<cBroadcast*> mlBroadCasts;
00072
00073 inline static cNet& GetSingleton () {
00074 static cNet* mSingleton = 0;
00075 if (!mSingleton) mSingleton = new cNet();
00076 return *mSingleton;
00077 }
00079 void Step ();
00080
00081 cConnection* Connect (const char* szHost,const int iPort);
00082 cConnection* Connect (const unsigned int iHost,const int iPort);
00083 cNetListener* Listen (const int iPort);
00084
00085 cConnection* PopDeadCon();
00086
00087 cNet ();
00088 ~cNet ();
00089
00090 static unsigned int GetHostByName (const char *szHost);
00091 static int ConnectSocket (uint32 iIP,const int iPort);
00092 static bool IsInvalidSocket (const int iSocket);
00093 static void CloseSocket (const int iSocket);
00094 static int Send (const int iSocket,const char* pBuffer,const int iBufferSize,const int iFlags);
00095 static int Recv (const int iSocket,char* pBuffer,const int iBufferSize,const int iFlags);
00096 };
00097
00098
00099
00100
00101
00102 class cConnection : public cSmartPointable {
00103 public:
00104 int miSocket;
00105 std::string msHost;
00106 int miPort;
00107 cFIFO* mpInBuffer;
00108 cFIFO* mpOutBuffer;
00109 bool mbOwnBuffers;
00110 uint32 miRemoteAddr;
00111 unsigned char mIP[4];
00112
00113 cConnection ();
00114 cConnection (cConnection* con);
00115 cConnection (const int iSocket,const char* szHost,const int iPort,const uint32 iRemoteAddr);
00116 cConnection (const int iSocket,const uint32 iRemoteAddr);
00117 virtual ~cConnection ();
00118 void Init ();
00119
00121 void Close ();
00122 void SendPush (cFIFO& source,const bool bWrite);
00123 void Step (const bool bRead,const bool bWrite, const bool bExcept);
00124 bool IsLocal ();
00126 const bool IsConnected();
00127 };
00128
00129
00130
00131 class cBroadcast { public:
00132 cFIFO mOutBuffer;
00133 std::set<cConnection*> mlCons;
00134
00135 cBroadcast();
00136 ~cBroadcast();
00137 void Step ();
00138 };
00139
00140
00141
00142
00143 class cNetListener : public cSmartPointable {
00144 public:
00145 std::set<cConnection*> mlCons;
00146 int miPort;
00147 int miListenSocket;
00148
00149 cNetListener (int iListenSocket,int iPort);
00150 ~cNetListener ();
00151 void Step ();
00152 cConnection* PopAccepted();
00153 };
00154
00155
00156
00158 class cUDP_ReceiveSocket : public cSmartPointable {
00159 public:
00160 int miPort;
00161 int miSocket;
00162
00163 cUDP_ReceiveSocket (const int iPort);
00164 ~cUDP_ReceiveSocket ();
00165
00167 int Receive (cFIFO& pFIFO,uint32& iAddr);
00168 };
00169
00170
00171
00173 class cUDP_SendSocket : public cSmartPointable {
00174 public:
00175 int miSocket;
00176
00177 cUDP_SendSocket ();
00178 ~cUDP_SendSocket ();
00179
00180 int Send (const uint32 iAddr,const int iPort,const char* pData,const int iDataLen);
00181 int Send (const uint32 iAddr,const int iPort,cFIFO& pFIFO,const int iDataLen=0);
00182
00183 void SetBroadcast (char broadcast);
00184 };
00185
00186
00187
00188 void LuaRegisterNet (lua_State *L);
00189
00190 };
00191
00192 #endif