Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

netcontrol.h

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002    this file is part of rcssserver3D
00003    Fri May 9 2003
00004    Copyright (C) 2003 Koblenz University
00005    $Id: netcontrol.h,v 1.3 2004/04/30 13:07:55 fruit Exp $
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; version 2 of the License.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 #ifndef OXYGEN_NETCONTROL_H
00021 #define OXYGEN_NETCONTROL_H
00022 
00023 #include "simcontrolnode.h"
00024 #include "netbuffer.h"
00025 #include <rcssnet/socket.hpp>
00026 #include <boost/shared_array.hpp>
00027 
00028 namespace oxygen
00029 {
00030 class GameControlServer;
00031 class NetMessage;
00032 
00045 class NetControl : public SimControlNode
00046 {
00047 public:
00048     enum ESocketType
00049         {
00050             ST_TCP,
00051             ST_UDP
00052         };
00053 
00054     struct Client
00055     {
00056     public:
00057         int id;
00058         rcss::net::Addr addr;
00059         boost::shared_ptr<rcss::net::Socket> socket;
00060 
00061     public:
00062         Client() : id(-1) {};
00063         Client(int i, const rcss::net::Addr& a,
00064                boost::shared_ptr<rcss::net::Socket> s =
00065                boost::shared_ptr<rcss::net::Socket>()
00066                ) : id(i), addr(a), socket(s) {};
00067     };
00068 
00069     // mapping from a client remote address to Client struct
00070     typedef std::map<rcss::net::Addr, boost::shared_ptr<Client> > TAddrMap;
00071 
00072     // mapping from a client remote address to a corresponding buffer
00073     typedef std::map<rcss::net::Addr, boost::shared_ptr<NetBuffer> > TBufferMap;
00074 
00075     typedef std::list<rcss::net::Addr> TAddrList;
00076 
00077 public:
00078     NetControl();
00079     virtual ~NetControl();
00080 
00082     virtual void InitSimulation();
00083 
00085     virtual void DoneSimulation();
00086 
00089     virtual void StartCycle();
00090 
00092     virtual void EndCycle();
00093 
00095     virtual void ClientConnect(boost::shared_ptr<Client> client);
00096 
00098     virtual void ClientDisconnect(boost::shared_ptr<Client> client);
00099 
00101     void SetServerPort(rcss::net::Addr::PortType port);
00102 
00104     rcss::net::Addr::PortType GetServerPort();
00105 
00108     void SetServerType(ESocketType type);
00109 
00112     ESocketType GetServerType();
00113 
00115     void SendMessage(boost::shared_ptr<Client> client,
00116                      const std::string& msg);
00117 
00119     void SendMessage(const rcss::net::Addr& addr,
00120                              const std::string& msg);
00121 
00123     static boost::shared_ptr<rcss::net::Socket>
00124     NetControl::CreateSocket(ESocketType type);
00125 
00126 protected:
00129     std::string NetControl::DescribeSocketType();
00130 
00132     void AcceptTCPConnections();
00133 
00135     void NetControl::ReadMessages();
00136 
00138     void ReadTCPMessages();
00139 
00143     void ReadUDPMessages();
00144 
00149     void StoreFragment(const rcss::net::Addr& addr, int size);
00150 
00155     void AddClient(const rcss::net::Addr& from,
00156                    boost::shared_ptr<rcss::net::Socket> socket =
00157                    boost::shared_ptr<rcss::net::Socket>());
00158 
00162     void RemoveClient(const rcss::net::Addr& from);
00163 
00165     void CloseDeadConnections();
00166 
00167 protected:
00169     boost::shared_ptr<NetMessage> mNetMessage;
00170 
00172     rcss::net::Addr mLocalAddr;
00173 
00175     ESocketType mSocketType;
00176 
00178     boost::shared_ptr<rcss::net::Socket> mSocket;
00179 
00181     TAddrMap mClients;
00182 
00184     TAddrList mCloseClients;
00185 
00187     TBufferMap mBuffers;
00188 
00190     int mBufferSize;
00191 
00193     boost::shared_array<char> mBuffer;
00194 
00196     int mClientId;
00197 };
00198 
00199 DECLARE_CLASS(NetControl);
00200 
00201 } // namespace oxygen
00202 
00203 #endif // OXYGEN_NETCONTROL_H

Generated on Thu Apr 6 15:25:39 2006 for rcssserver3d by  doxygen 1.4.4