BitShares-Core  4.0.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 namespace fc {
7  namespace ip { class endpoint; }
8 
9  class tcp_socket_io_hooks;
10 
11  class tcp_socket : public virtual iostream
12  {
13  public:
14  tcp_socket();
15  tcp_socket( tcp_socket& copy ) = delete;
16  ~tcp_socket();
17  tcp_socket& operator=( tcp_socket& copy ) = delete;
18 
19  void connect_to( const fc::ip::endpoint& remote_endpoint );
20  void bind( const fc::ip::endpoint& local_endpoint );
21  void enable_keep_alives(const fc::microseconds& interval);
22  void set_io_hooks(tcp_socket_io_hooks* new_hooks);
23  void set_reuse_address(bool enable = true); // set SO_REUSEADDR
24  fc::ip::endpoint remote_endpoint() const;
25  fc::ip::endpoint local_endpoint() const;
26 
27  using istream::get;
28  void get( char& c )
29  {
30  read( &c, 1 );
31  }
32 
33 
36  virtual size_t readsome( char* buffer, size_t max );
37  virtual size_t readsome(const std::shared_ptr<char>& buffer, size_t max, size_t offset);
38  virtual bool eof()const;
40 
43  virtual size_t writesome( const char* buffer, size_t len );
44  virtual size_t writesome(const std::shared_ptr<const char>& buffer, size_t len, size_t offset);
45  virtual void flush();
46  virtual void close();
48 
49  void open();
50  bool is_open()const;
51 
52  private:
53  friend class tcp_server;
54  class impl;
55  #ifdef _WIN64
57  #else
59  #endif
60  };
61  typedef std::shared_ptr<tcp_socket> tcp_socket_ptr;
62 
63 
64  class tcp_server
65  {
66  public:
67  tcp_server();
68  ~tcp_server();
69 
70  void close();
71  void accept( tcp_socket& s );
72  void set_reuse_address(bool enable = true); // set SO_REUSEADDR, call before listen
73  void listen( uint16_t port );
74  void listen( const fc::ip::endpoint& ep );
75  fc::ip::endpoint get_local_endpoint() const;
76  uint16_t get_port()const;
77  private:
78  // non copyable
79  tcp_server( const tcp_server& );
80  tcp_server& operator=(const tcp_server& s );
81 
82  class impl;
83  impl* my;
84  };
85 
86 } // namesapce fc
87 
Used to forward declare value types.
Definition: fwd.hpp:10
std::shared_ptr< tcp_socket > tcp_socket_ptr
Definition: tcp_socket.hpp:61
size_t read(AsyncReadStream &s, const MutableBufferSequence &buf)
wraps boost::asio::async_read
Definition: asio.hpp:104
void accept(AcceptorType &acc, SocketType &sock)
wraps boost::asio::async_accept
Definition: asio.hpp:251
virtual char get()
Definition: iostream.cpp:267
boost::asio::ip::tcp::endpoint endpoint
Definition: asio.hpp:240
Definition: api.hpp:15
void copy(const path &from, const path &to)
Definition: filesystem.cpp:241