peerplays-fc/include/fc/network/tcp_socket.hpp
Eric Frias 2e5fdf952c Add a new version of tcp_socket::connect_to() that allows you to set the source port
(working, but not yet useful because we'll need to set SO_REUSEADDR)
2014-04-02 08:54:13 -04:00

72 lines
1.6 KiB
C++

#pragma once
#include <fc/utility.hpp>
#include <fc/fwd.hpp>
#include <fc/io/iostream.hpp>
namespace fc {
namespace ip { class endpoint; }
class tcp_socket : public virtual iostream
{
public:
tcp_socket();
~tcp_socket();
void connect_to( const fc::ip::endpoint& remote_endpoint );
void connect_to( const fc::ip::endpoint& remote_endpoint, const fc::ip::endpoint& local_endpoint );
fc::ip::endpoint remote_endpoint()const;
void get( char& c )
{
read( &c, 1 );
}
/// istream interface
/// @{
virtual size_t readsome( char* buffer, size_t max );
virtual bool eof()const;
/// @}
/// ostream interface
/// @{
virtual size_t writesome( const char* buffer, size_t len );
virtual void flush();
virtual void close();
/// @}
bool is_open()const;
private:
friend class tcp_server;
class impl;
#ifdef _WIN64
fc::fwd<impl,0x68> my;
#else
fc::fwd<impl,0x44> my;
#endif
};
typedef std::shared_ptr<tcp_socket> tcp_socket_ptr;
class tcp_server
{
public:
tcp_server();
~tcp_server();
void close();
void accept( tcp_socket& s );
void listen( uint16_t port );
void listen( const fc::ip::endpoint& ep );
uint16_t get_port()const;
private:
// non copyable
tcp_server( const tcp_server& );
tcp_server& operator=(const tcp_server& s );
class impl;
impl* my;
};
} // namesapce fc