jbus/include/jbus/Socket.hpp

82 lines
2.2 KiB
C++
Raw Normal View History

2018-10-06 20:42:04 -07:00
#pragma once
2017-01-06 19:13:23 -08:00
#include <cstddef>
#include <cstdint>
2017-01-06 19:13:23 -08:00
#include <string>
#ifdef _WIN32
#include <BaseTsd.h>
using SOCKET = UINT_PTR;
#endif
struct sockaddr_in;
2018-12-07 21:20:55 -08:00
namespace jbus::net {
2017-01-06 19:13:23 -08:00
/** IP address class derived from SFML */
2018-12-07 21:20:55 -08:00
class IPAddress {
uint32_t m_address = 0;
bool m_valid = false;
2017-01-06 19:13:23 -08:00
void resolve(const std::string& address) noexcept;
2017-01-06 19:13:23 -08:00
public:
explicit IPAddress(const std::string& address) noexcept { resolve(address); }
2017-01-06 19:13:23 -08:00
uint32_t toInteger() const noexcept;
explicit operator bool() const noexcept { return m_valid; }
2017-01-06 19:13:23 -08:00
};
/** Server-oriented TCP socket class derived from SFML */
2018-12-07 21:20:55 -08:00
class Socket {
2017-01-07 00:24:16 -08:00
#ifndef _WIN32
2018-12-07 21:20:55 -08:00
using SocketTp = int;
2017-01-07 00:24:16 -08:00
#else
2018-12-07 21:20:55 -08:00
using SocketTp = SOCKET;
2017-01-07 00:24:16 -08:00
#endif
2018-12-07 21:20:55 -08:00
SocketTp m_socket = -1;
bool m_isBlocking;
2017-01-06 19:13:23 -08:00
bool openSocket() noexcept;
void setRemoteSocket(int remSocket) noexcept;
2017-01-06 19:13:23 -08:00
public:
2018-12-07 21:20:55 -08:00
enum class EResult { OK, Error, Busy };
2017-01-06 19:13:23 -08:00
2017-01-07 00:24:16 -08:00
#ifdef _WIN32
static EResult LastWSAError() noexcept;
2017-01-07 00:24:16 -08:00
#endif
explicit Socket(bool blocking) noexcept : m_isBlocking(blocking) {}
~Socket() noexcept { close(); }
2018-12-07 21:20:55 -08:00
Socket(const Socket& other) = delete;
Socket& operator=(const Socket& other) = delete;
Socket(Socket&& other) noexcept : m_socket(other.m_socket), m_isBlocking(other.m_isBlocking) { other.m_socket = -1; }
Socket& operator=(Socket&& other) noexcept {
2018-12-07 21:20:55 -08:00
close();
m_socket = other.m_socket;
other.m_socket = -1;
m_isBlocking = other.m_isBlocking;
return *this;
}
void setBlocking(bool blocking) noexcept;
bool isOpen() const noexcept { return m_socket != -1; }
bool openAndListen(const IPAddress& address, uint32_t port) noexcept;
EResult accept(Socket& remoteSocketOut, sockaddr_in& fromAddress) noexcept;
EResult accept(Socket& remoteSocketOut) noexcept;
2018-12-07 21:20:55 -08:00
EResult accept(Socket& remoteSocketOut, std::string& fromHostname);
void close() noexcept;
EResult send(const void* buf, size_t len, size_t& transferred) noexcept;
EResult send(const void* buf, size_t len) noexcept;
EResult recv(void* buf, size_t len, size_t& transferred) noexcept;
EResult recv(void* buf, size_t len) noexcept;
2018-12-07 21:20:55 -08:00
explicit operator bool() const noexcept { return isOpen(); }
2018-12-07 21:20:55 -08:00
SocketTp GetInternalSocket() const noexcept { return m_socket; }
2017-01-06 19:13:23 -08:00
};
2018-12-07 21:20:55 -08:00
} // namespace jbus::net