Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Examples/Version2/Socket.h
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
80 lines (63 sloc)
1.98 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef THORSANVIL_SOCKET_SOCKET_H | |
#define THORSANVIL_SOCKET_SOCKET_H | |
#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; | |
protected: | |
static constexpr int invalidSocketId = -1; | |
// Designed to be a base class not used used directly. | |
BaseSocket(int socketId); | |
int getSocketId() const {return socketId;} | |
public: | |
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 | |
{ | |
public: | |
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 | |
{ | |
public: | |
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; | |
public: | |
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" | |
#endif | |