networking.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 
00005 #ifndef BALL_SYSTEM_NETWORKING_H
00006 #define BALL_SYSTEM_NETWORKING_H
00007 
00008 #ifndef BALL_COMMON_GLOBAL_HH
00009 # include<BALL/COMMON/global.h>
00010 #endif
00011 
00012 #ifndef BALL_DATATYPE_STRING_H
00013 # include <BALL/DATATYPE/string.h>
00014 #endif
00015 
00016 #ifdef BALL_HAS_BOOST_ASIO
00017 # include <boost/asio.hpp>
00018 #else
00019 #define BOOST_DATE_TIME_NO_LIB
00020 #define BOOST_REGEX_NO_LIB
00021 # include <asio.hpp>
00022 #endif
00023 
00024 #include <QtCore/qthread.h>
00025 
00026 namespace BALL
00027 {
00035   class BALL_EXPORT TCPIOStream
00036     : public BALL_ASIO_NAMESPACE::ip::tcp::iostream
00037   {
00038     public:
00039       TCPIOStream() 
00040         : BALL_ASIO_NAMESPACE::ip::tcp::iostream()
00041       {
00042       }
00043 
00044       TCPIOStream(const String& hostname, const String& protocol)
00045         : BALL_ASIO_NAMESPACE::ip::tcp::iostream(hostname, protocol)
00046       {
00047       }
00048 
00049       TCPIOStream(const String& hostname, Position port)
00050         : BALL_ASIO_NAMESPACE::ip::tcp::iostream(hostname, String(port))
00051       {
00052       }
00053   };
00054 
00064   class BALL_EXPORT TCPServer
00065   {
00066     public:
00067       TCPServer(Size port, bool restart = true)
00068         : port_(port), 
00069           restart_(restart),
00070           connected_stream_(),
00071           io_service_(),
00072           acceptor_(io_service_)
00073       {};
00074 
00075       virtual ~TCPServer();
00076 
00077       virtual void activate();
00078       virtual void deactivate();
00079 
00080       virtual void startAccepting();
00081       virtual void handleConnection();
00082       virtual void connectionRequested();
00083 
00084       void setPort(Size port);
00085       Size getPort() const;
00086 
00087     protected:
00088       Size port_; 
00089       bool restart_;
00090 
00091       TCPIOStream connected_stream_;
00092 
00093       BALL_ASIO_NAMESPACE::io_service io_service_;
00094 
00095       BALL_ASIO_NAMESPACE::ip::tcp::acceptor acceptor_;
00096   };
00097 
00100   class BALL_EXPORT TCPServerThread
00101     : public TCPServer,
00102       public virtual QThread
00103   {
00104     public:
00105       TCPServerThread(Size port, bool asynchronous = true, bool restart = true);
00106       
00107       virtual void run();
00108       virtual void deactivate();
00109       virtual void activate_async();
00110       virtual void handleAsyncConnection();
00111 
00113       bool isRunning();
00114 
00115     protected:
00116       bool use_async_io_;
00117       bool is_running_;
00118   };
00119 } // namespace BALL
00120 
00121 #endif  // BALL_SYSTEM_NETWORKING_H