peerplays-fc/include/fc/network/tcp_socket.hpp

87 lines
2.2 KiB
C++
Raw Permalink Normal View History

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>
#include <fc/io/iostream.hpp>
#include <fc/time.hpp>
2012-09-27 23:48:48 +00:00
namespace fc {
namespace ip { class endpoint; }
class tcp_socket_io_hooks;
class tcp_socket : public virtual iostream
{
2012-09-27 23:48:48 +00:00
public:
tcp_socket();
~tcp_socket();
void connect_to( const fc::ip::endpoint& remote_endpoint );
void bind( const fc::ip::endpoint& local_endpoint );
void enable_keep_alives(const fc::microseconds& interval);
void set_io_hooks(tcp_socket_io_hooks* new_hooks);
void set_reuse_address(bool enable = true); // set SO_REUSEADDR
fc::ip::endpoint remote_endpoint() const;
fc::ip::endpoint local_endpoint() const;
2012-09-27 23:48:48 +00:00
using istream::get;
2013-12-09 05:48:28 +00:00
void get( char& c )
{
read( &c, 1 );
}
/// istream interface
/// @{
virtual size_t readsome( char* buffer, size_t max );
virtual size_t readsome(const std::shared_ptr<char>& buffer, size_t max, size_t offset);
virtual bool eof()const;
/// @}
2012-09-27 23:48:48 +00:00
/// ostream interface
/// @{
virtual size_t writesome( const char* buffer, size_t len );
virtual size_t writesome(const std::shared_ptr<const char>& buffer, size_t len, size_t offset);
virtual void flush();
virtual void close();
/// @}
2012-10-22 00:54:52 +00:00
void open();
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
fc::fwd<impl,0x81> my;
2013-11-13 19:35:12 +00:00
#else
fc::fwd<impl,0x54> my;
2013-11-13 19:35:12 +00:00
#endif
2012-09-27 23:48:48 +00:00
};
typedef std::shared_ptr<tcp_socket> tcp_socket_ptr;
2012-09-27 23:48:48 +00:00
class tcp_server
{
2012-09-27 23:48:48 +00:00
public:
tcp_server();
2012-09-27 23:48:48 +00:00
~tcp_server();
void close();
void accept( tcp_socket& s );
void set_reuse_address(bool enable = true); // set SO_REUSEADDR, call before listen
void listen( uint16_t port );
void listen( const fc::ip::endpoint& ep );
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:
// non copyable
tcp_server( const tcp_server& );
tcp_server& operator=(const tcp_server& s );
2012-09-27 23:48:48 +00:00
class impl;
impl* my;
2012-09-27 23:48:48 +00:00
};
} // namesapce fc