OpenJPH
Open-source implementation of JPEG2000 Part-15
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{
68namespace net
69{
70
72//
73//
74//
75//
76//
78
79//************************************************************************/
88class socket {
89public:
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:
122};
123
125//
126//
127//
128//
129//
131
132//************************************************************************/
146public:
155
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:
229};
230
231} // !net namespace
232} // !ojph namespace
233
234
235
236#endif // !OJPH_SOCKET_H
A small wrapper for some Winsock2 functionality.
Definition: ojph_sockets.h:145
std::string get_error_message(int errnum)
Abstructs obtaining a textual message for an errnum.
socket_manager()
default constructor
static ui32 get_addr(const sockaddr_in &addr)
Abstractly obtains the 32-bit IPv4 address integer.
static int ojph_socket_manager_counter
Definition: ojph_sockets.h:228
~socket_manager()
default constructor
socket create_socket(int domain, int type, int protocol)
Abstructs socket creation.
std::string get_last_error_message()
Abstructs obtaining a textual message for GetLastError/errno.
int get_last_error()
Abstructs get last error or errno.
A small wrapper for socket that only abstract Winsock2.
Definition: ojph_sockets.h:88
ojph_socket intern()
provides access to the internal socket handle
Definition: ojph_sockets.h:118
socket()
default constructor
Definition: ojph_sockets.h:93
void close()
Abstracts socket closing function.
bool set_blocking_mode(bool block)
Sets the blocking mode.
ojph_socket s
int for Linux/MacOS and SOCKET for Windows
Definition: ojph_sockets.h:121
uint32_t ui32
Definition: ojph_defs.h:54
#define OJPH_INVALID_SOCKET
Definition: ojph_sockets.h:62
int ojph_socket
Definition: ojph_sockets.h:61