2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-08-07 20:52:32 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#pragma once
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "base/XBase.h"
|
2025-03-19 22:11:47 +00:00
|
|
|
#include "common/Common.h"
|
2025-08-07 21:05:29 +00:00
|
|
|
#include "io/IOException.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-06-27 12:40:18 +00:00
|
|
|
/**
|
2025-08-07 20:52:32 +00:00
|
|
|
* @brief SocketException generic socket exception
|
2025-06-27 12:40:18 +00:00
|
|
|
*/
|
2025-08-07 20:52:32 +00:00
|
|
|
class SocketException : public XBase
|
2025-06-27 12:40:18 +00:00
|
|
|
{
|
|
|
|
|
using XBase::XBase;
|
|
|
|
|
};
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//! Socket bad address exception
|
|
|
|
|
/*!
|
|
|
|
|
Thrown when attempting to create an invalid network address.
|
|
|
|
|
*/
|
2025-08-07 20:52:32 +00:00
|
|
|
class SocketAddressException : public SocketException
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2012-06-10 16:50:54 +00:00
|
|
|
public:
|
2024-07-02 19:07:06 +00:00
|
|
|
//! Failure codes
|
2025-07-08 14:21:13 +00:00
|
|
|
enum class SocketError
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-07-08 14:21:13 +00:00
|
|
|
Unknown, //!< Unknown error
|
|
|
|
|
NotFound, //!< The hostname is unknown
|
|
|
|
|
NoAddress, //!< The hostname is valid but has no IP address
|
|
|
|
|
Unsupported, //!< The hostname is valid but has no supported address
|
|
|
|
|
BadPort //!< The port is invalid
|
2024-07-02 19:07:06 +00:00
|
|
|
};
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-08-07 20:52:32 +00:00
|
|
|
SocketAddressException(SocketError, const std::string &hostname, int port) noexcept;
|
|
|
|
|
~SocketAddressException() throw() override = default;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
//! @name accessors
|
|
|
|
|
//@{
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
//! Get the error code
|
2025-07-08 14:21:13 +00:00
|
|
|
SocketError getError() const noexcept;
|
2024-07-02 19:07:06 +00:00
|
|
|
//! Get the hostname
|
2025-07-08 01:45:40 +00:00
|
|
|
std::string getHostname() const noexcept;
|
2024-07-02 19:07:06 +00:00
|
|
|
//! Get the port
|
2025-07-08 01:45:40 +00:00
|
|
|
int getPort() const noexcept;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
//@}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
protected:
|
2024-07-02 19:07:06 +00:00
|
|
|
// XBase overrides
|
2025-04-28 01:27:48 +00:00
|
|
|
std::string getWhat() const throw() override;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
private:
|
2025-07-08 14:21:13 +00:00
|
|
|
SocketError m_error;
|
2025-01-06 00:55:07 +00:00
|
|
|
std::string m_hostname;
|
2024-07-02 19:07:06 +00:00
|
|
|
int m_port;
|
2012-06-10 16:50:54 +00:00
|
|
|
};
|
|
|
|
|
|
2025-06-27 12:40:18 +00:00
|
|
|
/**
|
2025-08-07 20:52:32 +00:00
|
|
|
* @brief SocketIOCloseException - Thrown if a stream cannot be closed.
|
2025-06-27 12:40:18 +00:00
|
|
|
*/
|
2025-08-07 21:05:29 +00:00
|
|
|
class SocketIOCloseException : public IOCloseException
|
2025-06-27 12:40:18 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2025-08-07 21:05:29 +00:00
|
|
|
SocketIOCloseException() : IOCloseException(), m_state(kDone)
|
2025-06-27 12:40:18 +00:00
|
|
|
{
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
2025-08-07 21:05:29 +00:00
|
|
|
explicit SocketIOCloseException(const std::string &msg) : IOCloseException(msg), m_state(kFirst)
|
2025-06-27 12:40:18 +00:00
|
|
|
{
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
2025-08-07 20:52:32 +00:00
|
|
|
~SocketIOCloseException() throw() override = default;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-06-27 12:40:18 +00:00
|
|
|
const char *what() const throw() override
|
|
|
|
|
{
|
|
|
|
|
if (m_state == kFirst) {
|
|
|
|
|
m_state = kFormat;
|
|
|
|
|
m_formatted = getWhat();
|
|
|
|
|
m_state = kDone;
|
|
|
|
|
}
|
|
|
|
|
if (m_state == kDone) {
|
|
|
|
|
return m_formatted.c_str();
|
|
|
|
|
} else {
|
2025-08-07 21:05:29 +00:00
|
|
|
return IOCloseException::what();
|
2025-06-27 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-06-27 12:40:18 +00:00
|
|
|
protected:
|
|
|
|
|
std::string getWhat() const throw() override;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-06-27 12:40:18 +00:00
|
|
|
private:
|
|
|
|
|
enum EState
|
|
|
|
|
{
|
|
|
|
|
kFirst,
|
|
|
|
|
kFormat,
|
|
|
|
|
kDone
|
|
|
|
|
};
|
|
|
|
|
mutable EState m_state;
|
|
|
|
|
mutable std::string m_formatted;
|
|
|
|
|
};
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-06-27 12:40:18 +00:00
|
|
|
/**
|
2025-08-07 20:52:32 +00:00
|
|
|
* @brief SocketWithWhatException - generic SocketException Exception with a generic `what` method impl
|
2025-06-27 12:40:18 +00:00
|
|
|
*/
|
2025-08-07 20:52:32 +00:00
|
|
|
class SocketWithWhatException : public SocketException
|
2025-06-27 12:40:18 +00:00
|
|
|
{
|
|
|
|
|
public:
|
2025-08-07 20:52:32 +00:00
|
|
|
SocketWithWhatException() : SocketException(), m_state(kDone)
|
2025-06-27 12:40:18 +00:00
|
|
|
{
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
2025-08-07 20:52:32 +00:00
|
|
|
explicit SocketWithWhatException(const std::string &msg) : SocketException(msg), m_state(kFirst)
|
2025-06-27 12:40:18 +00:00
|
|
|
{
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
2025-08-07 20:52:32 +00:00
|
|
|
~SocketWithWhatException() throw() override = default;
|
2025-06-27 12:40:18 +00:00
|
|
|
|
|
|
|
|
const char *what() const throw() override
|
|
|
|
|
{
|
|
|
|
|
if (m_state == kFirst) {
|
|
|
|
|
m_state = kFormat;
|
|
|
|
|
m_formatted = getWhat();
|
|
|
|
|
m_state = kDone;
|
|
|
|
|
}
|
|
|
|
|
if (m_state == kDone) {
|
|
|
|
|
return m_formatted.c_str();
|
|
|
|
|
} else {
|
2025-08-07 20:52:32 +00:00
|
|
|
return SocketException::what();
|
2025-06-27 12:40:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
enum EState
|
|
|
|
|
{
|
|
|
|
|
kFirst,
|
|
|
|
|
kFormat,
|
|
|
|
|
kDone
|
|
|
|
|
};
|
|
|
|
|
mutable EState m_state;
|
|
|
|
|
mutable std::string m_formatted;
|
|
|
|
|
};
|
2025-06-29 08:25:40 +00:00
|
|
|
|
|
|
|
|
/**
|
2025-08-07 20:52:32 +00:00
|
|
|
* @brief SocketBindException - Thrown when a socket cannot be bound to an address.
|
2025-06-29 08:25:40 +00:00
|
|
|
*/
|
2025-08-07 20:52:32 +00:00
|
|
|
class SocketBindException : public SocketWithWhatException
|
2025-06-29 08:25:40 +00:00
|
|
|
{
|
2025-08-07 20:52:32 +00:00
|
|
|
using SocketWithWhatException::SocketWithWhatException;
|
2025-06-29 08:25:40 +00:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
std::string getWhat() const throw() override;
|
|
|
|
|
};
|
|
|
|
|
|
2025-06-27 12:40:18 +00:00
|
|
|
/**
|
2025-08-07 20:52:32 +00:00
|
|
|
* @brief SocketAddressInUseException
|
2025-06-27 12:40:18 +00:00
|
|
|
* Thrown when a socket cannot be bound to an address because the address is already in use.
|
|
|
|
|
*/
|
2025-08-07 20:52:32 +00:00
|
|
|
class SocketAddressInUseException : public SocketWithWhatException
|
2025-06-27 12:40:18 +00:00
|
|
|
{
|
2025-08-07 20:52:32 +00:00
|
|
|
using SocketWithWhatException::SocketWithWhatException;
|
2025-06-29 08:25:40 +00:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
std::string getWhat() const throw() override;
|
2025-06-27 12:40:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2025-08-07 20:52:32 +00:00
|
|
|
* @brief SocketConnectException - Thrown when a socket cannot connect to a remote endpoint.
|
2025-06-27 12:40:18 +00:00
|
|
|
*/
|
2025-08-07 20:52:32 +00:00
|
|
|
class SocketConnectException : public SocketWithWhatException
|
2025-06-27 12:40:18 +00:00
|
|
|
{
|
2025-08-07 20:52:32 +00:00
|
|
|
using SocketWithWhatException::SocketWithWhatException;
|
2025-06-27 12:40:18 +00:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
std::string getWhat() const throw() override;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2025-08-07 20:52:32 +00:00
|
|
|
* @brief SocketCreateException - Thrown when a socket cannot be created (by the operating system).
|
2025-06-27 12:40:18 +00:00
|
|
|
*/
|
2025-08-07 20:52:32 +00:00
|
|
|
class SocketCreateException : public SocketWithWhatException
|
2025-06-27 12:40:18 +00:00
|
|
|
{
|
2025-08-07 20:52:32 +00:00
|
|
|
using SocketWithWhatException::SocketWithWhatException;
|
2025-06-27 12:40:18 +00:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
std::string getWhat() const throw() override;
|
|
|
|
|
};
|