PixelLightAPI  .
Host.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: Host.h                                         *
00003  *
00004  *  Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/)
00005  *
00006  *  This file is part of PixelLight.
00007  *
00008  *  PixelLight is free software: you can redistribute it and/or modify
00009  *  it under the terms of the GNU Lesser General Public License as published by
00010  *  the Free Software Foundation, either version 3 of the License, or
00011  *  (at your option) any later version.
00012  *
00013  *  PixelLight is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  *  GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public License
00019  *  along with PixelLight. If not, see <http://www.gnu.org/licenses/>.
00020 \*********************************************************/
00021 
00022 
00023 #ifndef __PLCORE_HOST_H__
00024 #define __PLCORE_HOST_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include "PLCore/Container/List.h"
00032 #include "PLCore/System/Thread.h"
00033 #include "PLCore/Network/Socket.h"
00034 
00035 
00036 //[-------------------------------------------------------]
00037 //[ Namespace                                             ]
00038 //[-------------------------------------------------------]
00039 namespace PLCore {
00040 
00041 
00042 //[-------------------------------------------------------]
00043 //[ Forward declarations                                  ]
00044 //[-------------------------------------------------------]
00045 class Connection;
00046 
00047 
00048 //[-------------------------------------------------------]
00049 //[ Classes                                               ]
00050 //[-------------------------------------------------------]
00051 /**
00052 *  @brief
00053 *    Network host base class
00054 *
00055 *  @remarks
00056 *    Base class for a 'network partner'. That is any node in a network, which can open or accept connection
00057 *    to one or more other nodes. In a classic client/server architecture, one host is the client
00058 *    and another is the server to which all clients connect. In other network architectures, such
00059 *    as peer-to-peer, the network consists of several nodes of the same type, that connect to one another.
00060 */
00061 class Host {
00062 
00063 
00064     //[-------------------------------------------------------]
00065     //[ Friends                                               ]
00066     //[-------------------------------------------------------]
00067     public:
00068         friend class Connection;
00069 
00070 
00071     //[-------------------------------------------------------]
00072     //[ Public functions                                      ]
00073     //[-------------------------------------------------------]
00074     public:
00075         /**
00076         *  @brief
00077         *    Constructor
00078         */
00079         PLCORE_API Host();
00080 
00081         /**
00082         *  @brief
00083         *    Destructor
00084         */
00085         PLCORE_API virtual ~Host();
00086 
00087         /**
00088         *  @brief
00089         *    Connect to a server
00090         *
00091         *  @param[in] sServerName
00092         *    Server name
00093         *  @param[in] nPort
00094         *    Port number
00095         *
00096         *  @return
00097         *    Connection, a null pointer on error
00098         */
00099         PLCORE_API Connection *Connect(const String &sServerName, uint32 nPort);
00100 
00101         /**
00102         *  @brief
00103         *    Check if the host is currently listening for new connections
00104         *
00105         *  @return
00106         *    'true' if host is listening for new connections, else 'false'
00107         */
00108         inline bool IsListening() const;
00109 
00110         /**
00111         *  @brief
00112         *    Start listening for new connections
00113         *
00114         *  @param[in] nPort
00115         *    Port at which the host is listening
00116         *
00117         *  @remarks
00118         *    This function will start a listener thread and then return, so this function
00119         *    does not block the caller thread. When a connection is opened, it will call
00120         *    the virtual function CreateConnection() and then OnConnection() upon a successful
00121         *    connect. You can use SetMaxConnection() to set the maximum number of connections.
00122         */
00123         PLCORE_API void Listen(uint32 nPort);
00124 
00125         /**
00126         *  @brief
00127         *    Stop listening for new connections
00128         *
00129         *  @remarks
00130         *    This function will stop the host listening for new connection, but all current connections
00131         *    will still remain open. See CloseConnection() and Close() for closing the host completely.
00132         */
00133         PLCORE_API void ClosePort();
00134 
00135         /**
00136         *  @brief
00137         *    Close all connections
00138         *
00139         *  @remarks
00140         *    This function closes all active connections, but does not stop listening for new
00141         *    connections. See StopListen() and Close() for closing the host completely.
00142         */
00143         PLCORE_API void CloseConnections();
00144 
00145         /**
00146         *  @brief
00147         *    Close host (close all connections and stop listening for new connections)
00148         */
00149         inline void Close();
00150 
00151         /**
00152         *  @brief
00153         *    Returns the currently used port number
00154         *
00155         *  @return
00156         *    Number of the currently used port
00157         */
00158         inline uint32 GetPort() const;
00159 
00160         /**
00161         *  @brief
00162         *    Returns the maximum number of allowed connections
00163         *
00164         *  @return
00165         *    Maximum number of allowed connections
00166         */
00167         inline uint32 GetMaxConnections() const;
00168 
00169         /**
00170         *  @brief
00171         *    Sets the maximum number of allowed connections
00172         *
00173         *  @param[in] nMaxConnections
00174         *    Maximum number of allowed connections
00175         */
00176         inline void SetMaxConnections(uint32 nMaxConnections);
00177 
00178         /**
00179         *  @brief
00180         *    Get list of active connection
00181         *
00182         *  @return
00183         *    List of connections
00184         */
00185         inline const Container<Connection*> &GetConnections() const;
00186 
00187         /**
00188         *  @brief
00189         *    Remove inactive connections
00190         */
00191         PLCORE_API void RemoveInactiveConnections();
00192 
00193 
00194     //[-------------------------------------------------------]
00195     //[ Protected virtual Host functions                      ]
00196     //[-------------------------------------------------------]
00197     protected:
00198         /**
00199         *  @brief
00200         *    Create a new incoming connection
00201         *
00202         *  @return
00203         *    Connection
00204         */
00205         PLCORE_API virtual Connection *CreateIncomingConnection();
00206 
00207         /**
00208         *  @brief
00209         *    Create a new outgoing connection
00210         *
00211         *  @return
00212         *    Connection
00213         */
00214         PLCORE_API virtual Connection *CreateOutgoingConnection();
00215 
00216         /**
00217         *  @brief
00218         *    Called when a connection is established
00219         *
00220         *  @param[in] cConnection
00221         *    The new connection
00222         */
00223         PLCORE_API virtual void OnConnect(Connection &cConnection);
00224 
00225         /**
00226         *  @brief
00227         *    Called when a connection is closed
00228         *
00229         *  @param[in] cConnection
00230         *    The closed connection
00231         */
00232         PLCORE_API virtual void OnDisconnect(Connection &cConnection);
00233 
00234 
00235     //[-------------------------------------------------------]
00236     //[ Private functions                                     ]
00237     //[-------------------------------------------------------]
00238     private:
00239         /**
00240         *  @brief
00241         *    Copy constructor
00242         *
00243         *  @param[in] cSource
00244         *    Source to copy from
00245         */
00246         Host(const Host &cSource);
00247 
00248         /**
00249         *  @brief
00250         *    Copy operator
00251         *
00252         *  @param[in] cSource
00253         *    Source to copy from
00254         *
00255         *  @return
00256         *    Reference to this instance
00257         */
00258         Host &operator =(const Host &cSource);
00259 
00260         /**
00261         *  @brief
00262         *    Check for new connections
00263         */
00264         void CheckNewConnections();
00265 
00266         /**
00267         *  @brief
00268         *    Add connection
00269         *
00270         *  @param[in] pConnection
00271         *    Connection
00272         */
00273         inline void AddConnection(Connection *pConnection);
00274 
00275         /**
00276         *  @brief
00277         *    Removes connection
00278         *
00279         *  @param[in] cConnection
00280         *    Connection
00281         */
00282         inline void RemoveConnection(Connection *pConnection);
00283 
00284 
00285     //[-------------------------------------------------------]
00286     //[ Private data types                                    ]
00287     //[-------------------------------------------------------]
00288     private:
00289         /**
00290         *  @brief
00291         *    Server thread class
00292         */
00293         class ServerThread : public Thread {
00294 
00295 
00296             //[-------------------------------------------------------]
00297             //[ Friends                                               ]
00298             //[-------------------------------------------------------]
00299             friend class Host;
00300 
00301 
00302             //[-------------------------------------------------------]
00303             //[ Private functions                                     ]
00304             //[-------------------------------------------------------]
00305             private:
00306                 /**
00307                 *  @brief
00308                 *    Constructor
00309                 *
00310                 *  @param[in] cHost
00311                 *    Host
00312                 */
00313                 ServerThread(Host &cHost);
00314 
00315                 /**
00316                 *  @brief
00317                 *    Destructor
00318                 */
00319                 virtual ~ServerThread();
00320 
00321 
00322             //[-------------------------------------------------------]
00323             //[ Private virtual Thread functions                      ]
00324             //[-------------------------------------------------------]
00325             private:
00326                 virtual int Run();
00327 
00328 
00329             //[-------------------------------------------------------]
00330             //[ Private data                                          ]
00331             //[-------------------------------------------------------]
00332             private:
00333                 Host *m_pHost;  /**< Host that owns this thread */
00334 
00335 
00336         };
00337 
00338 
00339     //[-------------------------------------------------------]
00340     //[ Private data                                          ]
00341     //[-------------------------------------------------------]
00342     private:
00343         ServerThread        m_cServerThread;    /**< Thread that looks for new connections */
00344         bool                m_bListening;       /**< Host listening for connections? */
00345         uint32              m_nPort;            /**< Number of the used port */
00346         uint32              m_nMaxConnections;  /**< Maximum number of allowed connections */
00347         List<Connection*>   m_lstConnections;   /**< List of connections */
00348         Socket              m_cSocket;          /**< Server socket */
00349 
00350 
00351 };
00352 
00353 
00354 //[-------------------------------------------------------]
00355 //[ Namespace                                             ]
00356 //[-------------------------------------------------------]
00357 } // PLCore
00358 
00359 
00360 //[-------------------------------------------------------]
00361 //[ Implementation                                        ]
00362 //[-------------------------------------------------------]
00363 #include "PLCore/Network/Host.inl"
00364 
00365 
00366 #endif // __PLCORE_HOST_H__


PixelLight PixelLight 0.9.11-R1
Copyright (C) 2002-2012 by The PixelLight Team
Last modified Thu Feb 23 2012 14:08:56
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported