summaryrefslogtreecommitdiff
path: root/implementation/endpoints/include/tcp_server_endpoint_impl.hpp
blob: 0aea5d80525662f1425a9afba52f0343caa04cc7 (plain)
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
// Copyright (C) 2014 BMW Group
// Author: Lutz Bichler (lutz.bichler@bmw.de)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef VSOMEIP_TCP_SERVER_ENDPOINT_IMPL_HPP
#define VSOMEIP_TCP_SERVER_ENDPOINT_IMPL_HPP

#include <map>

#include <boost/asio/ip/tcp.hpp>
#include <boost/enable_shared_from_this.hpp>

#include <vsomeip/defines.hpp>
#include "server_endpoint_impl.hpp"

namespace vsomeip {

typedef server_endpoint_impl<
			boost::asio::ip::tcp,
			VSOMEIP_MAX_TCP_MESSAGE_SIZE > tcp_server_endpoint_base_impl;

class tcp_server_endpoint_impl
	: public tcp_server_endpoint_base_impl {

public:
	tcp_server_endpoint_impl(std::shared_ptr< endpoint_host > _host,
			endpoint_type _local, boost::asio::io_service &_io);
	virtual ~tcp_server_endpoint_impl();

	void start();
	void stop();

	bool send_to(const std::shared_ptr<endpoint_definition> _target,
	             const byte_t *_data, uint32_t _size, bool _flush);
	void send_queued(endpoint_type _target, message_buffer_ptr_t _buffer);

	endpoint_type get_remote() const;
	bool get_multicast(service_t, event_t, endpoint_type &) const;

	unsigned short get_port() const;
	bool is_reliable() const;

	// dummies to implement endpoint_impl interface
	// TODO: think about a better design!
	void receive();
	void restart();

private:
	class connection
		: public boost::enable_shared_from_this< connection > {

	public:
		typedef boost::shared_ptr< connection > ptr;

		static ptr create(tcp_server_endpoint_impl *_server);
		socket_type & get_socket();

		void start();
		void stop();

		void send_queued(message_buffer_ptr_t _buffer);

	private:
		connection(tcp_server_endpoint_impl *_owner);
		void send_magic_cookie(message_buffer_ptr_t &_buffer);

		tcp_server_endpoint_impl::socket_type socket_;
		tcp_server_endpoint_impl *server_;

		message_buffer_t message_;

	private:
		bool is_magic_cookie() const;
		void receive_cbk(packet_buffer_ptr_t _buffer,
				boost::system::error_code const &_error, std::size_t _bytes);
	};

	boost::asio::ip::tcp::acceptor acceptor_;
	std::map< endpoint_type, connection::ptr > connections_;
	connection *current_;

private:
	void accept_cbk(connection::ptr _connection, boost::system::error_code const &_error);
};

} // namespace vsomeip

#endif // VSOMEIP_TCP_SERVER_ENDPOINT_IMPL_HPP