BitShares-Core  6.1.0
BitShares blockchain implementation and command-line interface software
tcp_socket.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <fc/fwd.hpp>
3 #include <fc/io/iostream.hpp>
4 #include <fc/time.hpp>
5 
6 #include <fc/asio.hpp>
7 #include <fc/thread/future.hpp>
8 
9 namespace fc {
10  namespace ip { class endpoint; }
11 
12  class tcp_socket_io_hooks;
13 
14  class tcp_socket : public virtual iostream
15  {
16  public:
17  tcp_socket();
18  tcp_socket( tcp_socket& copy ) = delete;
19  ~tcp_socket();
20  tcp_socket& operator=( tcp_socket& copy ) = delete;
21 
22  void connect_to( const fc::ip::endpoint& remote_endpoint );
23  void bind( const fc::ip::endpoint& local_endpoint );
24  void enable_keep_alives(const fc::microseconds& interval);
25  void set_io_hooks(tcp_socket_io_hooks* new_hooks);
26  void set_reuse_address(bool enable = true); // set SO_REUSEADDR
27  fc::ip::endpoint remote_endpoint() const;
28  fc::ip::endpoint local_endpoint() const;
29 
30  using istream::get;
31  void get( char& c )
32  {
33  read( &c, 1 );
34  }
35 
36 
39  virtual size_t readsome( char* buffer, size_t max );
40  virtual size_t readsome(const std::shared_ptr<char>& buffer, size_t max, size_t offset);
41  virtual bool eof()const;
43 
46  virtual size_t writesome( const char* buffer, size_t len );
47  virtual size_t writesome(const std::shared_ptr<const char>& buffer, size_t len, size_t offset);
48  virtual void flush();
49  virtual void close();
51 
52  void open();
53  bool is_open()const;
54 
55  private:
56  friend class tcp_server;
57  class impl;
58  fc::fwd<impl,
59  sizeof(void* /*vtable*/) +
60  sizeof(fc::future<size_t>) +
61  sizeof(fc::future<size_t>) +
62  sizeof(boost::asio::ip::tcp::socket) +
63  sizeof(tcp_socket_io_hooks*)
64  > my;
65 
66  };
67  typedef std::shared_ptr<tcp_socket> tcp_socket_ptr;
68 
69 
70  class tcp_server
71  {
72  public:
73  tcp_server();
74  ~tcp_server();
75 
76  void close();
77  void accept( tcp_socket& s );
78  void set_reuse_address(bool enable = true); // set SO_REUSEADDR, call before listen
79  void listen( uint16_t port );
80  void listen( const fc::ip::endpoint& ep );
81  fc::ip::endpoint get_local_endpoint() const;
82  uint16_t get_port()const;
83  private:
84  // non copyable
85  tcp_server( const tcp_server& );
86  tcp_server& operator=(const tcp_server& s );
87 
88  class impl;
89  impl* my;
90  };
91 
92 } // namesapce fc
93 
Used to forward declare value types.
Definition: fwd.hpp:10
std::shared_ptr< tcp_socket > tcp_socket_ptr
Definition: tcp_socket.hpp:67
size_t read(AsyncReadStream &s, const MutableBufferSequence &buf)
wraps boost::asio::async_read
Definition: asio.hpp:103
void accept(AcceptorType &acc, SocketType &sock)
wraps boost::asio::async_accept
Definition: asio.hpp:250
virtual char get()
Definition: iostream.cpp:267
boost::asio::ip::tcp::endpoint endpoint
Definition: asio.hpp:239
Definition: api.hpp:15
void copy(const path &from, const path &to)
Definition: filesystem.cpp:241