OpenJPH: /startdir/src/OpenJPH/src/apps/common/ojph_sockets.h Source File

OpenJPH Open-source implementation of JPEG2000 Part-15

Loading...
Searching...
No Matches
ojph_sockets.h
Go to the documentation of this file.
1//***************************************************************************/
2// This software is released under the 2-Clause BSD license, included
3// below.
4//
5// Copyright (c) 2024, Aous Naman
6// Copyright (c) 2024, Kakadu Software Pty Ltd, Australia
7// Copyright (c) 2024, The University of New South Wales, Australia
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13// 1. Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//
16// 2. Redistributions in binary form must reproduce the above copyright
17// notice, this list of conditions and the following disclaimer in the
18// documentation and/or other materials provided with the distribution.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31//***************************************************************************/
32// This file is part of the OpenJPH software implementation.
33// File: ojph_socket.h
34// Author: Aous Naman
35// Date: 17 April 2024
36//***************************************************************************/
37
38#ifndef OJPH_SOCKET_H
39#define OJPH_SOCKET_H
40
41#include <string>
42#include "ojph_arch.h"
43
44#ifdef OJPH_OS_WINDOWS
45 #include <winsock2.h>
46 #include <WS2tcpip.h>
47
48 typedef SOCKET ojph_socket;
49 #define OJPH_INVALID_SOCKET (INVALID_SOCKET)
50 #define OJPH_EWOULDBLOCK (WSAEWOULDBLOCK)
51#else
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
56 #include <netdb.h>
57 #include <unistd.h>
58 #include <errno.h>
59 #include <fcntl.h>
60
61 typedef int ojph_socket;
62 #define OJPH_INVALID_SOCKET (-1)
63 #define OJPH_EWOULDBLOCK (EWOULDBLOCK)
64#endif
65
66namespace ojph
67{
68 namespace net
69{
70
72//
73//
74//
75//
76//
78
79//************************************************************************/
88 class socket {
89public:
93 socket() { s = OJPH_INVALID_SOCKET; }
94
98 socket(const ojph_socket& s);
99
103 void close();
104
111 bool set_blocking_mode(bool block);
112
118 ojph_socket intern() { return s; }
119
120private:
121 ojph_socket s;
122};
123
125//
126//
127//
128//
129//
131
132//************************************************************************/
145 class socket_manager {
146public:
154 socket_manager();
155
164 ~socket_manager();
165
178 socket create_socket(int domain, int type, int protocol);
179
188 int get_last_error();
189
199 std::string get_error_message(int errnum);
200
212 std::string get_last_error_message();
213
225 static ui32 get_addr(const sockaddr_in& addr);
226
227private:
228 static int ojph_socket_manager_counter;
229};
230
231} // !net namespace
232} // !ojph namespace
233
234
235
236#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