Switch branches/tags
Nothing to show
Find file Copy path
d42d345 Jun 5, 2016
1 contributor

Users who have contributed to this file

81 lines (63 sloc) 1.98 KB
#include <string>
#include <vector>
#include <sstream>
namespace ThorsAnvil
namespace Socket
// An RAII base class for handling sockets.
// Socket is movable but not copyable.
class BaseSocket
int socketId;
static constexpr int invalidSocketId = -1;
// Designed to be a base class not used used directly.
BaseSocket(int socketId);
int getSocketId() const {return socketId;}
virtual ~BaseSocket();
// Moveable but not Copyable
BaseSocket(BaseSocket&& move) noexcept;
BaseSocket& operator=(BaseSocket&& move) noexcept;
void swap(BaseSocket& other) noexcept;
BaseSocket(BaseSocket const&) = delete;
BaseSocket& operator=(BaseSocket const&) = delete;
// User can manually call close
void close();
// A class that can read/write to a socket
class DataSocket: public BaseSocket
DataSocket(int socketId)
: BaseSocket(socketId)
template<typename F>
std::size_t getMessageData(char* buffer, std::size_t size, F scanForEnd = [](std::size_t){return false;});
void putMessageData(char const* buffer, std::size_t size);
void putMessageClose();
// A class the conects to a remote machine
// Allows read/write accesses to the remote machine
class ConnectSocket: public DataSocket
ConnectSocket(std::string const& host, int port);
// A server socket that listens on a port for a connection
class ServerSocket: public BaseSocket
static constexpr int maxConnectionBacklog = 5;
ServerSocket(int port);
// An accepts waits for a connection and returns a socket
// object that can be used by the client for communication
DataSocket accept();
#include "Socket.tpp"