lugre_net.h

Go to the documentation of this file.
00001 /*
00002 http://www.opensource.org/licenses/mit-license.php  (MIT-License)
00003 
00004 Copyright (c) 2007 Lugre-Team
00005 
00006 Permission is hereby granted, free of charge, to any person obtaining a copy
00007 of this software and associated documentation files (the "Software"), to deal
00008 in the Software without restriction, including without limitation the rights
00009 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010 copies of the Software, and to permit persons to whom the Software is
00011 furnished to do so, subject to the following conditions:
00012 
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022 THE SOFTWARE.
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 //winsock workaround
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 // SOCKET = int
00053 
00054 class   lua_State;
00055 
00056 namespace Lugre {
00057 
00058 class cConnection;
00059 class cBroadcast;
00060 class cNetListener;
00061 
00062 // ****** ****** ****** cNet
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 // ****** ****** ****** cConnection
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     (); // local connection, mbOwnBuffers
00114     cConnection     (cConnection* con); // local connection, !mbOwnBuffers
00115     cConnection     (const int iSocket,const char* szHost,const int iPort,const uint32 iRemoteAddr); // connect out
00116     cConnection     (const int iSocket,const uint32 iRemoteAddr); // connection coming in
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 // ****** ****** ****** cBroadcast
00130 
00131 class cBroadcast { public:
00132     cFIFO                   mOutBuffer;
00133     std::set<cConnection*>  mlCons;
00134 
00135     cBroadcast();
00136     ~cBroadcast();
00137     void    Step        ();
00138 };
00139 
00140 // ****** ****** ****** cNetListener
00141 
00142 
00143 class cNetListener : public cSmartPointable {
00144 public:
00145     std::set<cConnection*>  mlCons; // this is used for handling new connections
00146     int     miPort;
00147     int     miListenSocket;
00148 
00149     cNetListener    (int iListenSocket,int iPort);
00150     ~cNetListener   ();
00151     void    Step    ();
00152     cConnection*    PopAccepted();
00153 };
00154 
00155 // ****** ****** ****** cUDP_ReceiveSocket
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 // ****** ****** ****** cUDP_SendSocket
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 // ****** ****** ****** lua binding
00187 
00188 void    LuaRegisterNet  (lua_State *L);
00189 
00190 };
00191 
00192 #endif

Generated on Wed Feb 8 06:00:12 2012 for cpp by  doxygen 1.5.6