BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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 
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
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 );
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 
fc::copy
void copy(const path &from, const path &to)
Definition: filesystem.cpp:241
fc::future< size_t >
fc::tcp_socket::bind
void bind(const fc::ip::endpoint &local_endpoint)
Definition: tcp_socket.cpp:160
fc::tcp_socket::is_open
bool is_open() const
Definition: tcp_socket.cpp:94
fc::tcp_server::get_port
uint16_t get_port() const
Definition: tcp_socket.cpp:363
fc::tcp_socket::get
void get(char &c)
Definition: tcp_socket.hpp:31
fc::tcp_socket::set_io_hooks
void set_io_hooks(tcp_socket_io_hooks *new_hooks)
Definition: tcp_socket.cpp:236
fwd.hpp
fc
Definition: api.hpp:15
fc::tcp_socket::close
virtual void close()
Definition: tcp_socket.cpp:103
fc::tcp_socket::impl
Definition: tcp_socket.cpp:28
fc::tcp_socket::enable_keep_alives
void enable_keep_alives(const fc::microseconds &interval)
Definition: tcp_socket.cpp:174
fc::tcp_server::set_reuse_address
void set_reuse_address(bool enable=true)
Definition: tcp_socket.cpp:310
fc::asio::tcp::endpoint
boost::asio::ip::tcp::endpoint endpoint
Definition: asio.hpp:239
fc::ip::endpoint
Definition: ip.hpp:65
fc::tcp_socket::eof
virtual bool eof() const
Definition: tcp_socket.cpp:112
fc::istream::get
virtual char get()
Definition: iostream.cpp:267
asio.hpp
iostream.hpp
fc::tcp_server::listen
void listen(uint16_t port)
Definition: tcp_socket.cpp:333
fc::tcp_socket::readsome
virtual size_t readsome(char *buffer, size_t max)
Definition: tcp_socket.cpp:147
fc::tcp_socket::connect_to
void connect_to(const fc::ip::endpoint &remote_endpoint)
Definition: tcp_socket.cpp:156
fc::tcp_socket::~tcp_socket
~tcp_socket()
Definition: tcp_socket.cpp:100
fc::tcp_server::get_local_endpoint
fc::ip::endpoint get_local_endpoint() const
Definition: tcp_socket.cpp:356
fc::istream::read
istream & read(char *buf, size_t len)
Definition: iostream.cpp:274
fc::tcp_server::close
void close()
Definition: tcp_socket.cpp:288
fc::tcp_server::impl
Definition: tcp_socket.cpp:268
fc::tcp_socket
Definition: tcp_socket.hpp:14
fc::tcp_server::~tcp_server
~tcp_server()
Definition: tcp_socket.cpp:297
fc::tcp_socket::remote_endpoint
fc::ip::endpoint remote_endpoint() const
Definition: tcp_socket.cpp:126
fc::tcp_socket_ptr
std::shared_ptr< tcp_socket > tcp_socket_ptr
Definition: tcp_socket.hpp:67
fc::microseconds
Definition: time.hpp:12
fc::tcp_server::accept
void accept(tcp_socket &s)
Definition: tcp_socket.cpp:302
fc::tcp_socket::open
void open()
Definition: tcp_socket.cpp:89
fc::tcp_socket::operator=
tcp_socket & operator=(tcp_socket &copy)=delete
future.hpp
fc::tcp_socket::flush
virtual void flush()
Definition: tcp_socket.cpp:102
fc::tcp_socket::writesome
virtual size_t writesome(const char *buffer, size_t len)
Definition: tcp_socket.cpp:116
time.hpp
fc::tcp_socket::local_endpoint
fc::ip::endpoint local_endpoint() const
Definition: tcp_socket.cpp:137
fc::tcp_socket::set_reuse_address
void set_reuse_address(bool enable=true)
Definition: tcp_socket.cpp:241
fc::tcp_server::tcp_server
tcp_server()
Definition: tcp_socket.cpp:294
fc::iostream
Definition: iostream.hpp:61
fc::fwd
Used to forward declare value types.
Definition: fwd.hpp:10
fc::tcp_socket::tcp_socket
tcp_socket()
Definition: tcp_socket.cpp:98
fc::tcp_socket_io_hooks
Definition: tcp_socket_io_hooks.hpp:6
fc::tcp_server
Definition: tcp_socket.hpp:70