Loading...
Searching...
No Matches
ojph_sockets.h
Go to the documentation of this file.
//***************************************************************************/
// This software is released under the 2-Clause BSD license, included
// below.
//
// Copyright (c) 2024, Aous Naman
// Copyright (c) 2024, Kakadu Software Pty Ltd, Australia
// Copyright (c) 2024, The University of New South Wales, Australia
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//***************************************************************************/
// This file is part of the OpenJPH software implementation.
// File: ojph_socket.h
// Author: Aous Naman
// Date: 17 April 2024
//***************************************************************************/
#ifndef OJPH_SOCKET_H
#define OJPH_SOCKET_H
#include <string>
#include "ojph_arch.h"
#ifdef OJPH_OS_WINDOWS
#include <winsock2.h>
#include <WS2tcpip.h>
typedef SOCKET ojph_socket;
#define OJPH_INVALID_SOCKET (INVALID_SOCKET)
#define OJPH_EWOULDBLOCK (WSAEWOULDBLOCK)
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
typedef int ojph_socket;
#define OJPH_INVALID_SOCKET (-1)
#define OJPH_EWOULDBLOCK (EWOULDBLOCK)
#endif
namespace ojph
{
namespace net
{
//
//
//
//
//
//************************************************************************/
class socket {
public:
socket() { s = OJPH_INVALID_SOCKET; }
socket(const ojph_socket& s);
void close();
bool set_blocking_mode(bool block);
ojph_socket intern() { return s; }
private:
ojph_socket s;
};
//
//
//
//
//
//************************************************************************/
class socket_manager {
public:
socket_manager();
~socket_manager();
socket create_socket(int domain, int type, int protocol);
int get_last_error();
std::string get_error_message(int errnum);
std::string get_last_error_message();
static ui32 get_addr(const sockaddr_in& addr);
private:
static int ojph_socket_manager_counter;
};
} // !net namespace
} // !ojph namespace
#endif // !OJPH_SOCKET_H
ojph::net::socket_manager::get_error_message
std::string get_error_message(int errnum)
Abstructs obtaining a textual message for an errnum.
Definition ojph_sockets.cpp:154
ojph::net::socket_manager::socket_manager
socket_manager()
default constructor
Definition ojph_sockets.cpp:107
ojph::net::socket_manager::get_addr
static ui32 get_addr(const sockaddr_in &addr)
Abstractly obtains the 32-bit IPv4 address integer.
Definition ojph_sockets.cpp:192
ojph::net::socket_manager::ojph_socket_manager_counter
static int ojph_socket_manager_counter
Definition ojph_sockets.h:228
ojph::net::socket_manager::~socket_manager
~socket_manager()
default constructor
Definition ojph_sockets.cpp:124
ojph::net::socket_manager::create_socket
socket create_socket(int domain, int type, int protocol)
Abstructs socket creation.
Definition ojph_sockets.cpp:137
ojph::net::socket_manager::get_last_error_message
std::string get_last_error_message()
Abstructs obtaining a textual message for GetLastError/errno.
Definition ojph_sockets.cpp:185
ojph::net::socket_manager::get_last_error
int get_last_error()
Abstructs get last error or errno.
Definition ojph_sockets.cpp:144
ojph::net::socket
A small wrapper for socket that only abstract Winsock2.
Definition ojph_sockets.h:88
ojph::net::socket::intern
ojph_socket intern()
provides access to the internal socket handle
Definition ojph_sockets.h:118
ojph::net::socket::socket
socket()
default constructor
Definition ojph_sockets.h:93
ojph::net::socket::close
void close()
Abstracts socket closing function.
Definition ojph_sockets.cpp:63
ojph::net::socket::set_blocking_mode
bool set_blocking_mode(bool block)
Sets the blocking mode.
Definition ojph_sockets.cpp:78
ojph::net::socket::s
ojph_socket s
int for Linux/MacOS and SOCKET for Windows
Definition ojph_sockets.h:121
ojph::net
Definition ojph_sockets.h:69
ojph
Definition ojph_img_io.h:52
ojph::ui32
uint32_t ui32
Definition ojph_defs.h:54
ojph_arch.h
OJPH_INVALID_SOCKET
#define OJPH_INVALID_SOCKET
Definition ojph_sockets.h:62
ojph_socket
int ojph_socket
Definition ojph_sockets.h:61