GComm  0.2.3
asio_tcp.hpp
1 /*
2  * Copyright (C) 2010 Codership Oy <info@codership.com>
3  */
4 
5 #ifndef GCOMM_ASIO_TCP_HPP
6 #define GCOMM_ASIO_TCP_HPP
7 
8 #include "socket.hpp"
9 #include "asio_protonet.hpp"
10 
11 #include <boost/bind.hpp>
12 #include <boost/array.hpp>
13 #include <boost/enable_shared_from_this.hpp>
14 #include <vector>
15 #include <deque>
16 
17 namespace gcomm
18 {
19  class AsioTcpSocket;
20  class AsioTcpAcceptor;
21 }
22 
23 // TCP Socket implementation
24 
26  public gcomm::Socket,
27  public boost::enable_shared_from_this<AsioTcpSocket>
28 {
29 public:
30  AsioTcpSocket(AsioProtonet& net, const gu::URI& uri);
31  ~AsioTcpSocket();
32  void failed_handler(const asio::error_code& ec, const std::string& func, int line);
33 #ifdef HAVE_ASIO_SSL_HPP
34  void handshake_handler(const asio::error_code& ec);
35 #endif // HAVE_ASIO_SSL_HPP
36  void connect_handler(const asio::error_code& ec);
37  void connect(const gu::URI& uri);
38  void close();
39  void write_handler(const asio::error_code& ec,
40  size_t bytes_transferred);
41  int send(const Datagram& dg);
42  size_t read_completion_condition(
43  const asio::error_code& ec,
44  const size_t bytes_transferred);
45  void read_handler(const asio::error_code& ec,
46  const size_t bytes_transferred);
47  void async_receive();
48  size_t mtu() const;
49  std::string local_addr() const;
50  std::string remote_addr() const;
51  State state() const { return state_; }
52  SocketId id() const { return &socket_; }
53 private:
54  friend class gcomm::AsioTcpAcceptor;
55 
57  void operator=(const AsioTcpSocket&);
58 
59  void read_one(boost::array<asio::mutable_buffer, 1>& mbs);
60  void write_one(const boost::array<asio::const_buffer, 2>& cbs);
61  void close_socket();
62 
63  // call to assign local/remote addresses at the point where it
64  // is known that underlying socket is live
65  void assign_local_addr();
66  void assign_remote_addr();
67 
68  AsioProtonet& net_;
69  asio::ip::tcp::socket socket_;
70 #ifdef HAVE_ASIO_SSL_HPP
71  asio::ssl::stream<asio::ip::tcp::socket>* ssl_socket_;
72 #endif // HAVE_ASIO_SSL_HPP
73  std::deque<Datagram> send_q_;
74  std::vector<gu::byte_t> recv_buf_;
75  size_t recv_offset_;
76  State state_;
77  // Querying addresses from failed socket does not work,
78  // so need to maintain copy for diagnostics logging
79  std::string local_addr_;
80  std::string remote_addr_;
81 };
82 
83 
85 {
86 public:
87 
88  AsioTcpAcceptor(AsioProtonet& net, const gu::URI& uri);
89  ~AsioTcpAcceptor();
90  void accept_handler(
91  SocketPtr socket,
92  const asio::error_code& error);
93  void listen(const gu::URI& uri);
94  std::string listen_addr() const;
95  void close();
96  SocketPtr accept();
97 
98  State state() const
99  {
100  gu_throw_fatal << "TODO:";
101  }
102 
103  SocketId id() const { return &acceptor_; }
104 
105 private:
106  AsioProtonet& net_;
107  asio::ip::tcp::acceptor acceptor_;
108  SocketPtr accepted_socket_;
109 };
110 
111 #endif // GCOMM_ASIO_TCP_HPP
Definition: asio_protonet.hpp:29
Definition: asio_tcp.hpp:84
Definition: asio_tcp.hpp:25
Definition: socket.hpp:72
Definition: socket.hpp:29
Datagram container.
Definition: datagram.hpp:151