Deskflow 1.22.0.197
Keyboard and mouse sharing utility
Loading...
Searching...
No Matches
XSocket.h
Go to the documentation of this file.
1/*
2 * Deskflow -- mouse and keyboard sharing utility
3 * SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
4 * SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
5 * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
6 */
7
8#pragma once
9
10#include "base/XBase.h"
11#include "common/Common.h"
12#include "io/XIO.h"
13
17class XSocket : public XBase
18{
19 using XBase::XBase;
20};
21
23
26class XSocketAddress : public XSocket
27{
28public:
38
39 XSocketAddress(SocketError, const std::string &hostname, int port) noexcept;
40 ~XSocketAddress() throw() override = default;
41
43
44
46 SocketError getError() const noexcept;
48 std::string getHostname() const noexcept;
50 int getPort() const noexcept;
51
53
54protected:
55 // XBase overrides
56 std::string getWhat() const throw() override;
57
58private:
59 SocketError m_error;
60 std::string m_hostname;
61 int m_port;
62};
63
68{
69public:
70 XSocketIOClose() : XIOClose(), m_state(kDone)
71 {
72 // do nothing
73 }
74 explicit XSocketIOClose(const std::string &msg) : XIOClose(msg), m_state(kFirst)
75 {
76 // do nothing
77 }
78 ~XSocketIOClose() throw() override = default;
79
80 const char *what() const throw() override
81 {
82 if (m_state == kFirst) {
83 m_state = kFormat;
84 m_formatted = getWhat();
85 m_state = kDone;
86 }
87 if (m_state == kDone) {
88 return m_formatted.c_str();
89 } else {
90 return XIOClose::what();
91 }
92 }
93
94protected:
95 std::string getWhat() const throw() override;
96
97private:
98 enum EState
99 {
100 kFirst,
101 kFormat,
102 kDone
103 };
104 mutable EState m_state;
105 mutable std::string m_formatted;
106};
107
112{
113public:
114 XSocketWithWhat() : XSocket(), m_state(kDone)
115 {
116 // do nothing
117 }
118 explicit XSocketWithWhat(const std::string &msg) : XSocket(msg), m_state(kFirst)
119 {
120 // do nothing
121 }
122 ~XSocketWithWhat() throw() override = default;
123
124 const char *what() const throw() override
125 {
126 if (m_state == kFirst) {
127 m_state = kFormat;
128 m_formatted = getWhat();
129 m_state = kDone;
130 }
131 if (m_state == kDone) {
132 return m_formatted.c_str();
133 } else {
134 return XSocket::what();
135 }
136 }
137
138private:
139 enum EState
140 {
141 kFirst,
142 kFormat,
143 kDone
144 };
145 mutable EState m_state;
146 mutable std::string m_formatted;
147};
148
152class XSocketBind : public XSocketWithWhat
153{
155
156protected:
157 std::string getWhat() const throw() override;
158};
159
164class XSocketAddressInUse : public XSocketWithWhat
165{
167
168protected:
169 std::string getWhat() const throw() override;
170};
171
175class XSocketConnect : public XSocketWithWhat
176{
178
179protected:
180 std::string getWhat() const throw() override;
181};
182
186class XSocketCreate : public XSocketWithWhat
187{
189
190protected:
191 std::string getWhat() const throw() override;
192};
XBase()
Use getWhat() as the result of what()
Definition XBase.cpp:17
virtual std::string getWhat() const noexcept
Get a human readable string describing the exception.
Definition XBase.h:31
const char * what() const override
Reason for exception.
Definition XBase.cpp:27
The XIOClose - Thrown if a stream cannot be closed.
Definition XIO.h:24
XSocketAddressInUse Thrown when a socket cannot be bound to an address because the address is already...
Definition XSocket.h:165
std::string getWhat() const override
Get a human readable string describing the exception.
Definition XSocket.cpp:77
XSocketAddress(SocketError, const std::string &hostname, int port) noexcept
Definition XSocket.cpp:15
SocketError getError() const noexcept
Get the error code.
Definition XSocket.cpp:23
int getPort() const noexcept
Get the port.
Definition XSocket.cpp:33
std::string getHostname() const noexcept
Get the hostname.
Definition XSocket.cpp:28
SocketError
Failure codes.
Definition XSocket.h:31
@ NotFound
The hostname is unknown.
Definition XSocket.h:33
@ Unknown
Unknown error.
Definition XSocket.h:32
@ BadPort
The port is invalid.
Definition XSocket.h:36
@ Unsupported
The hostname is valid but has no supported address.
Definition XSocket.h:35
@ NoAddress
The hostname is valid but has no IP address.
Definition XSocket.h:34
~XSocketAddress() override=default
std::string getWhat() const override
Get a human readable string describing the exception.
Definition XSocket.cpp:38
XSocketBind - Thrown when a socket cannot be bound to an address.
Definition XSocket.h:153
std::string getWhat() const override
Get a human readable string describing the exception.
Definition XSocket.cpp:68
XSocketConnect - Thrown when a socket cannot connect to a remote endpoint.
Definition XSocket.h:176
std::string getWhat() const override
Get a human readable string describing the exception.
Definition XSocket.cpp:86
XSocketConnect - Thrown when a socket cannot be created (by the operating system).
Definition XSocket.h:187
std::string getWhat() const override
Get a human readable string describing the exception.
Definition XSocket.cpp:95
~XSocketIOClose() override=default
XSocketIOClose()
Definition XSocket.h:70
const char * what() const override
Definition XSocket.h:80
XSocketIOClose(const std::string &msg)
Definition XSocket.h:74
XSocketWithWhat(const std::string &msg)
Definition XSocket.h:118
XSocketWithWhat()
Definition XSocket.h:114
const char * what() const override
Definition XSocket.h:124
~XSocketWithWhat() override=default
XSocket generic socket exception.
Definition XSocket.h:18
Definition Config.h:29