2013-01-11 14:12:53 +00:00
|
|
|
#pragma once
|
2012-09-27 23:48:48 +00:00
|
|
|
#include <fc/utility.hpp>
|
|
|
|
|
#include <fc/fwd.hpp>
|
2013-06-05 19:19:00 +00:00
|
|
|
#include <fc/io/iostream.hpp>
|
2014-04-09 22:29:59 +00:00
|
|
|
#include <fc/time.hpp>
|
2012-09-27 23:48:48 +00:00
|
|
|
|
|
|
|
|
namespace fc {
|
2014-12-23 20:45:57 +00:00
|
|
|
namespace ip { class endpoint; }
|
2014-04-17 23:39:15 +00:00
|
|
|
|
|
|
|
|
class tcp_socket_io_hooks;
|
|
|
|
|
|
2014-12-23 20:45:57 +00:00
|
|
|
class tcp_socket : public virtual iostream
|
2013-06-05 19:19:00 +00:00
|
|
|
{
|
2012-09-27 23:48:48 +00:00
|
|
|
public:
|
|
|
|
|
tcp_socket();
|
|
|
|
|
~tcp_socket();
|
|
|
|
|
|
2014-03-27 18:12:27 +00:00
|
|
|
void connect_to( const fc::ip::endpoint& remote_endpoint );
|
2014-06-01 22:07:56 +00:00
|
|
|
void bind( const fc::ip::endpoint& local_endpoint );
|
2014-04-09 22:29:59 +00:00
|
|
|
void enable_keep_alives(const fc::microseconds& interval);
|
2014-05-08 13:04:45 +00:00
|
|
|
void set_io_hooks(tcp_socket_io_hooks* new_hooks);
|
|
|
|
|
void set_reuse_address(bool enable = true); // set SO_REUSEADDR
|
2014-04-29 23:54:43 +00:00
|
|
|
fc::ip::endpoint remote_endpoint() const;
|
|
|
|
|
fc::ip::endpoint local_endpoint() const;
|
2012-09-27 23:48:48 +00:00
|
|
|
|
2014-12-23 20:45:57 +00:00
|
|
|
using istream::get;
|
2013-12-09 05:48:28 +00:00
|
|
|
void get( char& c )
|
|
|
|
|
{
|
|
|
|
|
read( &c, 1 );
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-29 23:54:43 +00:00
|
|
|
|
2012-11-06 04:34:58 +00:00
|
|
|
/// istream interface
|
|
|
|
|
/// @{
|
|
|
|
|
virtual size_t readsome( char* buffer, size_t max );
|
2014-09-11 20:30:03 +00:00
|
|
|
virtual size_t readsome(const std::shared_ptr<char>& buffer, size_t max, size_t offset);
|
2012-11-06 04:34:58 +00:00
|
|
|
virtual bool eof()const;
|
|
|
|
|
/// @}
|
2012-09-27 23:48:48 +00:00
|
|
|
|
2012-11-06 04:34:58 +00:00
|
|
|
/// ostream interface
|
|
|
|
|
/// @{
|
2013-06-05 19:19:00 +00:00
|
|
|
virtual size_t writesome( const char* buffer, size_t len );
|
2014-09-11 20:30:03 +00:00
|
|
|
virtual size_t writesome(const std::shared_ptr<const char>& buffer, size_t len, size_t offset);
|
2012-11-06 04:34:58 +00:00
|
|
|
virtual void flush();
|
|
|
|
|
virtual void close();
|
|
|
|
|
/// @}
|
2012-10-22 00:54:52 +00:00
|
|
|
|
2014-04-29 23:54:43 +00:00
|
|
|
void open();
|
2012-11-06 04:34:58 +00:00
|
|
|
bool is_open()const;
|
2012-10-10 01:40:29 +00:00
|
|
|
|
2012-09-27 23:48:48 +00:00
|
|
|
private:
|
|
|
|
|
friend class tcp_server;
|
|
|
|
|
class impl;
|
2013-11-13 19:35:12 +00:00
|
|
|
#ifdef _WIN64
|
2014-07-22 14:04:55 +00:00
|
|
|
fc::fwd<impl,0x81> my;
|
2013-11-13 19:35:12 +00:00
|
|
|
#else
|
2014-07-21 14:40:21 +00:00
|
|
|
fc::fwd<impl,0x54> my;
|
2013-11-13 19:35:12 +00:00
|
|
|
#endif
|
2012-09-27 23:48:48 +00:00
|
|
|
};
|
2013-06-05 19:19:00 +00:00
|
|
|
typedef std::shared_ptr<tcp_socket> tcp_socket_ptr;
|
2012-09-27 23:48:48 +00:00
|
|
|
|
2014-12-23 20:45:57 +00:00
|
|
|
|
|
|
|
|
class tcp_server
|
2013-06-05 19:19:00 +00:00
|
|
|
{
|
2012-09-27 23:48:48 +00:00
|
|
|
public:
|
2012-11-06 04:34:58 +00:00
|
|
|
tcp_server();
|
2012-09-27 23:48:48 +00:00
|
|
|
~tcp_server();
|
|
|
|
|
|
2014-02-15 01:32:23 +00:00
|
|
|
void close();
|
|
|
|
|
void accept( tcp_socket& s );
|
2014-04-29 23:54:43 +00:00
|
|
|
void set_reuse_address(bool enable = true); // set SO_REUSEADDR, call before listen
|
2014-02-15 01:32:23 +00:00
|
|
|
void listen( uint16_t port );
|
|
|
|
|
void listen( const fc::ip::endpoint& ep );
|
2014-06-01 22:07:56 +00:00
|
|
|
fc::ip::endpoint get_local_endpoint() const;
|
2013-12-22 05:10:03 +00:00
|
|
|
uint16_t get_port()const;
|
2012-09-27 23:48:48 +00:00
|
|
|
private:
|
2012-11-06 04:34:58 +00:00
|
|
|
// non copyable
|
2014-12-23 20:45:57 +00:00
|
|
|
tcp_server( const tcp_server& );
|
2012-11-06 04:34:58 +00:00
|
|
|
tcp_server& operator=(const tcp_server& s );
|
|
|
|
|
|
2012-09-27 23:48:48 +00:00
|
|
|
class impl;
|
2012-11-06 04:34:58 +00:00
|
|
|
impl* my;
|
2012-09-27 23:48:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namesapce fc
|
|
|
|
|
|