OpenJPH
Open-source implementation of JPEG2000 Part-15
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
43namespace ojph
44{
45 namespace net
46 {
47
49 //
50 //
51 //
52 //
53 //
55
58 {
59 this->s = s;
60 }
61
64 {
65
67 {
68 #ifdef OJPH_OS_WINDOWS
69 ::closesocket(s);
70 #else
71 ::close(s);
72 #endif
74 }
75 }
76
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
105
108 {
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 }
121 }
122
125 {
126 assert(ojph_socket_manager_counter >= 1);
129 {
130 #ifdef _MSC_VER
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
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 OJPH_OS_APPLE) || \
169 ((_POSIX_C_SOURCE >= 200112L) && !_GNU_SOURCE)
170 // it is not clear if the returned value is in buf or in v
171 int t = strerror_r(errnum, (char*)buf, max_buf_size);
172 if (t != 0)
173 OJPH_ERROR(0x00080002, "Error retrieving a text message for "
174 "socket error number %d\n", errnum);
175 buf[max_buf_size - 1] = 0;
176 #else
177 v = strerror_r(errnum, (char*)buf, max_buf_size);
178 #endif
179 std::string str;
180 str = v;
181 return str;
182 }
183
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
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
~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.
socket()
default constructor
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
uint32_t ui32
Definition ojph_defs.h:54
#define OJPH_ERROR(t,...)
#define OJPH_INVALID_SOCKET
int ojph_socket