Instrument Neutral Distributed Interface INDI 2.2.5
tcpsocket.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2022 by Pawel Soja <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18#pragma once
19
20#include <cstdint>
21#include <string>
22#include <memory>
23#include <functional>
24
25#include "indimacros.h"
26
29{
30 public:
59
70
71 public:
72 TcpSocket();
73 virtual ~TcpSocket();
74
75 public:
76 void setConnectionTimeout(int timeout);
77
78 public:
79 void connectToHost(const std::string &hostName, uint16_t port);
80 void disconnectFromHost();
81
82 public:
83 ssize_t write(const char *data, size_t size);
84 ssize_t write(const std::string &data);
85
86 public:
87 SocketError error() const;
88 std::string errorString() const;
89
90 public:
91 void onConnected(const std::function<void()> &callback);
92 void onDisconnected(const std::function<void()> &callback);
93 void onData(const std::function<void(const char *, size_t)> &callback);
94 void onErrorOccurred(const std::function<void(SocketError)> &callback);
95
96 public:
97 bool waitForDisconnected(int timeout = 2000) const;
98 bool waitForConnected(int timeout = 2000) const;
99
100 public:
101 int *socketDescriptor() const;
102
103 protected:
104 virtual void connected();
105 virtual void disconnected();
106 virtual void readyRead();
107 virtual void errorOccurred(SocketError);
108
109 protected:
110 void emitConnected() const;
111 void emitDisconnected() const;
112 void emitData(const char *data, size_t size) const;
114
115 protected:
116 void setSocketError(SocketError socketError);
117
118 protected:
119 friend class TcpSocketPrivate;
120 std::unique_ptr<TcpSocketPrivate> d_ptr;
121 TcpSocket(std::unique_ptr<TcpSocketPrivate> &&d);
122};
std::unique_ptr< TcpSocketPrivate > d_ptr
Definition tcpsocket.h:120
void onData(const std::function< void(const char *, size_t)> &callback)
bool waitForDisconnected(int timeout=2000) const
virtual void disconnected()
std::string errorString() const
@ ConnectedState
Definition tcpsocket.h:65
@ ConnectingState
Definition tcpsocket.h:64
@ ListeningState
Definition tcpsocket.h:67
@ ClosingState
Definition tcpsocket.h:68
@ HostLookupState
Definition tcpsocket.h:63
@ UnconnectedState
Definition tcpsocket.h:62
void emitConnected() const
void setConnectionTimeout(int timeout)
void connectToHost(const std::string &hostName, uint16_t port)
virtual ~TcpSocket()
@ ProxyNotFoundError
Definition tcpsocket.h:50
@ SslHandshakeFailedError
Definition tcpsocket.h:46
@ ProxyConnectionRefusedError
Definition tcpsocket.h:47
@ TemporaryError
Definition tcpsocket.h:55
@ ProxyConnectionClosedError
Definition tcpsocket.h:48
@ SslInternalError
Definition tcpsocket.h:53
@ RemoteHostClosedError
Definition tcpsocket.h:34
@ UnfinishedSocketOperationError
Definition tcpsocket.h:44
@ OperationError
Definition tcpsocket.h:52
@ ConnectionRefusedError
Definition tcpsocket.h:33
@ AddressInUseError
Definition tcpsocket.h:41
@ SocketAddressNotAvailableError
Definition tcpsocket.h:42
@ SslInvalidUserDataError
Definition tcpsocket.h:54
@ SocketAccessError
Definition tcpsocket.h:36
@ ProxyProtocolError
Definition tcpsocket.h:51
@ NetworkError
Definition tcpsocket.h:40
@ UnsupportedSocketOperationError
Definition tcpsocket.h:43
@ ProxyAuthenticationRequiredError
Definition tcpsocket.h:45
@ SocketTimeoutError
Definition tcpsocket.h:38
@ ProxyConnectionTimeoutError
Definition tcpsocket.h:49
@ SocketResourceError
Definition tcpsocket.h:37
@ HostNotFoundError
Definition tcpsocket.h:35
@ UnknownSocketError
Definition tcpsocket.h:57
@ DatagramTooLargeError
Definition tcpsocket.h:39
void emitErrorOccurred(SocketError error) const
void emitDisconnected() const
virtual void connected()
SocketError error() const
void emitData(const char *data, size_t size) const
void setSocketError(SocketError socketError)
int * socketDescriptor() const
void onErrorOccurred(const std::function< void(SocketError)> &callback)
void disconnectFromHost()
ssize_t write(const char *data, size_t size)
void onDisconnected(const std::function< void()> &callback)
bool waitForConnected(int timeout=2000) const
virtual void errorOccurred(SocketError)
virtual void readyRead()
void onConnected(const std::function< void()> &callback)