File: networking.h

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 239,924 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; javascript: 164; makefile: 88
file content (116 lines) | stat: -rw-r--r-- 2,804 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#ifndef BALL_SYSTEM_NETWORKING_H
#define BALL_SYSTEM_NETWORKING_H

#ifndef BALL_COMMON_GLOBAL_HH
# include<BALL/COMMON/global.h>
#endif

#ifndef BALL_DATATYPE_STRING_H
# include <BALL/DATATYPE/string.h>
#endif

#include <boost/asio.hpp>

#include <QtCore/QThread>

namespace BALL
{
	/** This class provides a very thin layer around the asio (or boost::asio, depending
	 *  on your setup) functions for handling asynchronous io.
	 *
	 *  Please use this class instead of asio directly whenever possible, since we want to
	 *  encapsulate a number of details (asio - namespace, correct names of header files, ...)
	 *  to allow boost-integrated asio as well as the pure library.
	 */
	class BALL_EXPORT TCPIOStream
		: public boost::asio::ip::tcp::iostream
	{
		public:
			TCPIOStream() 
				: boost::asio::ip::tcp::iostream()
			{
			}

			TCPIOStream(const String& hostname, const String& protocol)
				: boost::asio::ip::tcp::iostream(hostname, protocol)
			{
			}

			TCPIOStream(const String& hostname, Position port)
				: boost::asio::ip::tcp::iostream(hostname, String(port))
			{
			}
	};

	/** This class provides a base for very simple TCP-based servers.
	 *
	 * 	By default, the server handles only one connection. The first request is
	 * 	accepted and converted into a TCPIOStream. If restart_ is set to true (default), 
	 * 	the server listens again after the connection has been terminated.
	 *
	 * 	Note that the server is not automatically started but rather waits for a call
	 * 	to "run".
	 */
	class BALL_EXPORT TCPServer
	{
		public:
			TCPServer(Size port, bool restart = true)
				: port_(port), 
					restart_(restart),
					connected_stream_(),
					io_service_(),
					acceptor_(io_service_)
			{};

			virtual ~TCPServer();

			virtual void activate();
			virtual void deactivate();

			virtual void startAccepting();
			virtual void handleConnection();
			virtual void connectionRequested();

			void setPort(Size port);
			Size getPort() const;

		protected:
			Size port_;	
			bool restart_;

			TCPIOStream connected_stream_;

			boost::asio::io_service io_service_;

			boost::asio::ip::tcp::acceptor acceptor_;
	};

	/** This class provides a simple TCP Server running in its own QThread.
	 */
	class BALL_EXPORT TCPServerThread
		: public TCPServer,
			public virtual QThread
	{
		public:
			TCPServerThread(Size port, bool asynchronous = true, bool restart = true);
			
			virtual void run();
			virtual void deactivate();
			virtual void activate_async();
			virtual void handleAsyncConnection();
			virtual void handleClose();

			/** Returns the state of the server. **/
			bool isRunning();

		protected:
			bool use_async_io_;
			bool is_running_;
	};
} // namespace BALL

#endif	// BALL_SYSTEM_NETWORKING_H