Instrument Neutral Distributed Interface INDI 2.2.5
tcpsocket_p.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 "tcpsocket.h"
21#include "select.h"
22
23#include <fcntl.h>
24#include <signal.h>
25#include <cstring>
26#include <cstdio>
27
28#include <functional>
29#include <mutex>
30#include <thread>
31#include <atomic>
32#include <condition_variable>
33
35{
36 public:
37 static const char *unixDomainPrefix;
38
39 public:
40 SocketAddress() = default;
41 explicit SocketAddress(const std::string &hostName, unsigned short port);
42
43 public:
44 static bool isUnix(const std::string &hostName);
45
46 public:
47 bool isValid() const
48 {
49 return data() != nullptr;
50 }
51
52 const struct sockaddr *data() const
53 {
54 return mData.get();
55 }
56
57 size_t size() const
58 {
59 return mSize;
60 }
61
62 public:
63 const struct sockaddr *operator&() const
64 {
65 return data();
66 }
67
68 protected:
69 static SocketAddress afInet(const std::string &hostName, unsigned short port);
70 static SocketAddress afUnix(const std::string &hostName);
71
72 protected:
73 std::unique_ptr<struct sockaddr> mData;
74 size_t mSize;
75};
76
78{
79 public:
81 virtual ~TcpSocketPrivate();
82
83 public: // platform dependent
84 bool createSocket(int domain);
85 void closeSocket();
86 ssize_t recvSocket(void *dst, size_t size);
87 ssize_t sendSocket(const void *src, size_t size);
88 bool setNonblockSocket();
89
90 public: // low level helpers
91 bool connectSocket(const std::string &hostName, unsigned short port);
93 bool processSocket();
94
95 public: // TcpSocketPrivate API
96 ssize_t write(const void *data, size_t size);
97
98 void connectToHost(const std::string &hostName, unsigned short port);
99 void aboutToClose();
100
101 void joinThread(std::thread &thread);
102
103 public:
110 const std::string &errorString = "");
112
113 public:
117 int timeout{30000};
118
119 std::thread thread;
120 std::atomic<bool> isAboutToClose{false};
121
122 mutable std::mutex socketStateMutex;
123 mutable std::condition_variable socketStateChanged;
124
125 std::atomic<TcpSocket::SocketState> socketState{TcpSocket::UnconnectedState};
127 std::string errorString;
128
129 // events
130 std::function<void()> onConnected;
131 std::function<void()> onDisconnected;
132 std::function<void(const char *, size_t)> onData;
134};
std::unique_ptr< struct sockaddr > mData
Definition tcpsocket_p.h:73
SocketAddress()=default
bool isValid() const
Definition tcpsocket_p.h:47
const struct sockaddr * data() const
Definition tcpsocket_p.h:52
static bool isUnix(const std::string &hostName)
Definition tcpsocket.cpp:63
static const char * unixDomainPrefix
Definition tcpsocket_p.h:37
size_t size() const
Definition tcpsocket_p.h:57
static SocketAddress afUnix(const std::string &hostName)
static SocketAddress afInet(const std::string &hostName, unsigned short port)
Definition tcpsocket.cpp:39
const struct sockaddr * operator&() const
Definition tcpsocket_p.h:63
bool createSocket(int domain)
SocketFileDescriptor socketFd
std::thread thread
std::function< void()> onDisconnected
std::string errorString
std::atomic< TcpSocket::SocketState > socketState
ssize_t sendSocket(const void *src, size_t size)
std::function< void(const char *, size_t)> onData
std::condition_variable socketStateChanged
bool waitForConnectedSockets()
std::function< void(TcpSocket::SocketError)> onErrorOccurred
virtual ~TcpSocketPrivate()
Definition tcpsocket.cpp:73
TcpSocket::SocketError socketError
TcpSocket * parent
bool connectSocket(const std::string &hostName, unsigned short port)
void connectToHost(const std::string &hostName, unsigned short port)
std::function< void()> onConnected
void setSocketState(TcpSocket::SocketState state)
ssize_t write(const void *data, size_t size)
Definition tcpsocket.cpp:86
void setSocketError(TcpSocket::SocketError error, ErrorType errorType=ErrorTypeSystem, const std::string &errorString="")
std::mutex socketStateMutex
ssize_t recvSocket(void *dst, size_t size)
void joinThread(std::thread &thread)
std::atomic< bool > isAboutToClose
@ UnconnectedState
Definition tcpsocket.h:62
int SocketFileDescriptor
Definition select.h:38