OpenJPH
Open-source implementation of JPEG2000 Part-15
Toggle main menu visibility
Loading...
Searching...
No Matches
ojph_sockets.cpp
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.cpp
34
// Author: Aous Naman
35
// Date: 17 April 2024
36
//***************************************************************************/
37
38
#include <cassert>
39
#include <string.h>
40
#include "
ojph_message.h
"
41
#include "
ojph_sockets.h
"
42
43
namespace
ojph
44
{
45
namespace
net
46
{
47
49
//
50
//
51
//
52
//
53
//
55
57
socket::socket
(
const
ojph_socket
&
s
)
58
{
59
this->s =
s
;
60
}
61
63
void
socket::close
()
64
{
65
66
if
(
s
!=
OJPH_INVALID_SOCKET
)
67
{
68
#ifdef OJPH_OS_WINDOWS
69
::closesocket(
s
);
70
#else
71
::close
(
s
);
72
#endif
73
s
=
OJPH_INVALID_SOCKET
;
74
}
75
}
76
78
bool
socket::set_blocking_mode
(
bool
block)
79
{
80
#ifdef OJPH_OS_WINDOWS
81
u_long mode = block ? 0 : 1;
82
return
ioctlsocket(
s
, FIONBIO, &mode) == 0;
83
#else
84
int
flags = fcntl(
s
, F_GETFL);
85
if
(flags == -1)
// error
86
return
false
;
87
if
(block)
88
flags &= ~O_NONBLOCK;
89
else
90
flags |= O_NONBLOCK;
91
return
fcntl(
s
, F_SETFL, flags) != -1;
92
#endif
93
}
94
96
//
97
//
98
//
99
//
100
//
102
104
int
socket_manager::ojph_socket_manager_counter
= 0;
105
107
socket_manager::socket_manager
()
108
{
109
if
(
ojph_socket_manager_counter
== 0)
110
{
111
#ifdef OJPH_OS_WINDOWS
112
WSADATA wsa;
113
if
(WSAStartup(MAKEWORD(2,2), &wsa) != 0)
114
{
115
std::string err =
get_last_error_message
();
116
OJPH_ERROR
(0x00080001,
"Could not create socket : %s\n"
, err.data());
117
}
118
#endif
119
}
120
++
ojph_socket_manager_counter
;
121
}
122
124
socket_manager::~socket_manager
()
125
{
126
assert(
ojph_socket_manager_counter
>= 1);
127
--
ojph_socket_manager_counter
;
128
if
(
ojph_socket_manager_counter
== 0)
129
{
130
#ifdef OJPH_OS_WINDOWS
131
WSACleanup();
132
#endif
133
}
134
}
135
137
socket
socket_manager::create_socket
(
int
domain,
int
type,
int
protocol)
138
{
139
socket
s(
::socket
(domain, type, protocol));
140
return
s;
141
}
142
144
int
socket_manager::get_last_error
()
145
{
146
#ifdef OJPH_OS_WINDOWS
147
return
WSAGetLastError();
148
#else
149
return
errno;
150
#endif
151
}
152
154
std::string
socket_manager::get_error_message
(
int
errnum)
155
{
156
if
( errnum == 0 )
157
return
std::string(
""
);
158
const
int
max_buf_size = 1024;
159
char
buf[max_buf_size];
160
char
*v = buf;
161
#ifdef OJPH_OS_WINDOWS
162
size_t
size
= FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM
163
| FORMAT_MESSAGE_IGNORE_INSERTS,
164
NULL, errnum,
165
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
166
buf, max_buf_size, NULL);
167
buf[max_buf_size - 1] = 0;
168
#elif (defined __GLIBC__) && \
169
((defined _GNU_SOURCE) || (_POSIX_C_SOURCE < 200112L))
170
v = strerror_r(errnum, (
char
*)buf, max_buf_size);
171
#else
172
// it is not clear if the returned value is in buf or in v
173
int
t = strerror_r(errnum, (
char
*)buf, max_buf_size);
174
if
(t != 0)
175
OJPH_ERROR
(0x00080002,
"Error retrieving a text message for "
176
"socket error number %d\n"
, errnum);
177
buf[max_buf_size - 1] = 0;
178
#endif
179
std::string str;
180
str = v;
181
return
str;
182
}
183
185
std::string
socket_manager::get_last_error_message
()
186
{
187
int
errnum =
get_last_error
();
188
return
get_error_message
(errnum);
189
}
190
192
ui32
socket_manager::get_addr
(
const
sockaddr_in& addr)
193
{
194
#ifdef OJPH_OS_WINDOWS
195
return
addr.sin_addr.S_un.S_addr;
196
#else
197
return
addr.sin_addr.s_addr;
198
#endif
199
}
200
201
}
// !net namespace
202
}
// !ojph namespace
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::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_message.h
OJPH_ERROR
#define OJPH_ERROR(t,...)
Definition
ojph_message.h:288
ojph_sockets.h
OJPH_INVALID_SOCKET
#define OJPH_INVALID_SOCKET
Definition
ojph_sockets.h:62
ojph_socket
int ojph_socket
Definition
ojph_sockets.h:61
ojph::size
Definition
ojph_base.h:48
src
apps
others
ojph_sockets.cpp
Generated by
1.18.0