# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

net_manager.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #ifndef NL_NET_MANAGER_H
00027 #define NL_NET_MANAGER_H
00028 
00029 #include "nel/misc/types_nl.h"
00030 
00031 #include <string>
00032 #include <map>
00033 #include <vector>
00034 
00035 #include "nel/misc/time_nl.h"
00036 #include "nel/net/callback_net_base.h"
00037 #include "nel/net/naming_client.h"
00038 
00039 namespace NLNET {
00040 
00042 typedef void (*TNetManagerCallback) (const std::string &serviceName, TSockId from, void *arg);
00043 
00050 struct CBaseStruct
00051 {
00052         CBaseStruct (const std::string &sn) :
00053                 Name(sn), ConnectionCallback(NULL), ConnectionCbArg(NULL),
00054                 DisconnectionCallback(NULL), DisconnectionCbArg(NULL), Type(Unknown)
00055         { }
00056 
00059         std::string     Name;
00060 
00061         enum TBaseStructType { Unknown, Client, ClientWithAddr, Group, Server };
00062 
00063         std::vector<std::string>                ServiceNames;
00064 
00066         std::vector<CCallbackNetBase*>   NetBase;
00067 
00068         TNetManagerCallback                              ConnectionCallback;
00069         void                                                    *ConnectionCbArg;
00070 
00071         TNetManagerCallback                              DisconnectionCallback;
00072         void                                                    *DisconnectionCbArg;
00073 
00074         // autoretry is used only when Type is ClientWithAddr. If true, the CNetManager will retry to reconnect if it lost the connection
00075         bool                                                     AutoRetry;
00076 
00077         TBaseStructType                                  Type;
00078 };
00079 
00089 class CNetManager
00090 {
00091 public:
00092 
00096         static void     init (const CInetAddress *addr, CCallbackNetBase::TRecordingState rec );
00097 
00098         static void release ();
00099 
00104         static void     addServer (const std::string &serviceName, uint16 servicePort = 0, bool external = false);
00105 
00106         static void     addServer (const std::string &serviceName, uint16 servicePort, NLNET::TServiceId &sid, bool external = false);
00107 
00109         static void     addClient (const std::string &serviceName, const std::string &addr, bool autoRetry = true);
00110 
00112         static void     addClient (const std::string &serviceName);
00113 
00115         static void     addGroup (const std::string &groupName, const std::string &serviceName);
00116 
00118         static void     addCallbackArray (const std::string &serviceName, const TCallbackItem *callbackarray, NLMISC::CStringIdArray::TStringId arraysize);
00119 
00125         static void     update (NLMISC::TTime timeout = 0);
00126 
00128         static void     send (const std::string &serviceName, const CMessage &buffer, TSockId hostid = InvalidSockId);
00129 
00134         static void     setConnectionCallback (const std::string &serviceName, TNetManagerCallback cb, void *arg);
00135 
00140         static void     setDisconnectionCallback (const std::string &serviceName, TNetManagerCallback cb, void *arg);
00141 
00143         static NLMISC::CStringIdArray   &getSIDA (const std::string &serviceName);
00144 
00146         static CCallbackNetBase *getNetBase (const std::string &serviceName);
00147 
00148         static void setUpdateTimeout (uint32 timeout);
00149 
00150         static void createConnection(CBaseStruct &Base, const CInetAddress &Addr, const std::string& name);
00151 
00152         static uint64 getBytesSent ();
00153         static uint64 getBytesReceived ();
00154 
00155         static uint64 getReceiveQueueSize ();
00156         static uint64 getSendQueueSize ();
00157 
00158 private:
00159 
00160         typedef std::map<std::string, CBaseStruct>      TBaseMap;
00161         typedef TBaseMap::iterator                                      ItBaseMap;
00162 
00163         // Contains all the connections (client and server)
00164         static  TBaseMap        _BaseMap;
00165 
00166         static  CCallbackNetBase::TRecordingState _RecordingState;
00167 
00168         // used to synchonize the timeout in the update function
00169         static  NLMISC::TTime _NextUpdateTime;
00170 
00171         // Finds the service or add it if not found
00172         static  ItBaseMap find (const std::string &serviceName);
00173 
00174         friend  void RegistrationBroadcast (const std::string &name, TServiceId sid, const std::vector<CInetAddress> &addr);
00175 
00176 
00177         // It's a static class, you can't instanciate it
00178         CNetManager() { }
00179 };
00180 
00181 
00182 } // NLNET
00183 
00184 
00185 #endif // NL_NET_MANAGER_H
00186 
00187 /* End of net_manager.h */