UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
socket.hpp
Go to the documentation of this file.
1#ifndef UPnPsdk_SOCKET_HPP
2#define UPnPsdk_SOCKET_HPP
3// Copyright (C) 2023+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
4// Redistribution only with this Copyright remark. Last modified: 2025-05-19
78#include <UPnPsdk/sockaddr.hpp>
79#include <UPnPsdk/synclog.hpp>
81#include <mutex>
82#include <memory>
83
84// To be portable with BSD socket error number constants I have to
85// define and use these macros with appended 'P' for portable.
86#ifdef _MSC_VER
87#define EBADFP WSAENOTSOCK
88#define ENOTCONNP WSAENOTCONN
89#define EINTRP WSAEINTR
90#define EFAULTP WSAEFAULT
91#define ENOMEMP WSA_NOT_ENOUGH_MEMORY
92#define EINVALP WSAEINVAL
93#define EACCESP WSAEACCES
94#define ENOBUFSP WSAENOBUFS
95#else
96#define EBADFP EBADF
97#define ENOTCONNP ENOTCONN
98#define EINTRP EINTR
99#define EFAULTP EFAULT
100#define ENOMEMP ENOMEM
101#define EINVALP EINVAL
102#define EACCESP EACCES
103#define ENOBUFSP ENOBUFS
104#endif
106
107namespace UPnPsdk {
108
129class UPnPsdk_API CSocket_basic {
130 public:
134
137 CSocket_basic(SOCKET a_sfd) ;
138
140 // I want to restrict to only move the resource.
141 // No copy constructor
142 CSocket_basic(const CSocket_basic&) = delete;
143 // No copy assignment operator
144 CSocket_basic& operator=(CSocket_basic) = delete;
146
147 // Destructor
148 virtual ~CSocket_basic();
149
150
178 void load();
180
181
194 operator SOCKET() const;
195
196
238 bool local_saddr(
246 SSockaddr* a_saddr = nullptr) const;
247
248
258 bool remote_saddr(
266 SSockaddr* a_saddr = nullptr) const;
267
268
272 int socktype() const;
273
281 int sockerr() const;
282
291 bool is_reuse_addr() const;
293
294 protected:
296 // This is the raw socket file descriptor
297 SOCKET m_sfd{INVALID_SOCKET};
298
299 // Mutex to protect socket object.
300 mutable ::pthread_mutex_t m_socket_mutex = PTHREAD_MUTEX_INITIALIZER;
301
302 // CSocket_basic::local_saddr() without pthread mutex lock/unlock for
303 // 'protected:' use only.
304 bool local_saddr_protected(SSockaddr* a_saddr) const;
306
307 private:
308 // Hint from the constructor what socket file descriptor to use.
309 const SOCKET m_sfd_hint{INVALID_SOCKET};
310};
311
312
320class UPnPsdk_API CSocket : public CSocket_basic {
321 public:
324 CSocket();
325
342 CSocket(CSocket&&);
343
363 CSocket& operator=(CSocket);
364
366 virtual ~CSocket();
367
459 void bind(
462 const int a_socktype,
465 const SSockaddr* const a_saddr = nullptr,
474 const int a_flags = 0);
475
484 void listen();
486
487
497 bool is_listen() const;
499
500 private:
501 bool m_listen{false};
502};
503
504
505// Portable handling of socket errors
506// ==================================
537class UPnPsdk_API CSocketErr {
538 public:
539 CSocketErr();
540 ~CSocketErr();
541 // Delete copy constructor
542 CSocketErr(const CSocketErr&) = delete;
543 // Delete assignment operator
544 CSocketErr& operator=(const CSocketErr&) = delete;
546 operator const int&();
548 void catch_error();
550 std::string error_str() const;
551
552 private:
553 int m_errno{}; // Cached error number
554};
555
556} // namespace UPnPsdk
557
558#endif // UPnPsdk_SOCKET_HPP
Class for portable handling of network socket errors.
Definition socket.hpp:537
Get information from a raw network socket file descriptor.
Definition socket.hpp:129
Manage all aspects of a network socket.
Definition socket.hpp:320
Reengineered Object Oriented UPnP+ program code.
Declaration of the Sockaddr class and some free helper functions.
Trivial ::sockaddr structures enhanced with methods.
Definition sockaddr.hpp:94
Define macro for synced logging to the console for detailed info and debug.