OpenAstra
 
Loading...
Searching...
No Matches
socketdevice.h
1#ifndef SOCKETDEVICE_H
2#define SOCKETDEVICE_H
3
4//#include <stdint.h>
5
6#include <string>
7#include <mutex>
8#include <memory>
9
10class SocketDevice
11{
12public:
13 static std::string resolveHostname(const std::string &hostname);
14
15 enum proto { tcpip,
16 udpip,
17 unset };
18
19 uint64_t sentBytes() const { return _sent_bytes; }
20
21 ~SocketDevice();
22
23 SocketDevice(proto p,
24 const std::string& la,
25 int lp,
26 const std::string& ra,
27 int rp );
28// SocketDevice( int _existing_socket );
29 SocketDevice(proto p, int _existing_socket );
30
31 std::string getDeviceAddress() const;
32 inline std::string getError() const { return _error_string; }
33 inline bool hasIncomingData() const { return waitIncomingData(0); }
34
35 bool isInitialized();
36 bool initialize();
37 void shutdown();
38
39 inline bool canRead() const { return _canRead; }
40 inline bool canWrite() const { return _canWrite; }
41
42 bool setTimeout(int sec, int usec);
43
44 int getProto();
45
46 bool setRemoteIP( const std::string& ra );
47 bool setRemotePort( int rp );
48 inline std::string getLocalIP() const { return _localAddress; }
49 inline std::string getRemoteIP() const { return _remoteAddress; }
50 inline int getLocalPort() const { return _localPort; }
51 inline int getRemotePort() const { return _remotePort; }
52
53 std::unique_ptr<SocketDevice> accept();
54 bool bind();
55 bool listen();
56 bool connect();
57
58 void setTcpNodelay(bool s);
59 bool setReuseAddr();
60
61 bool setBroadcast();
62
63 void setSoLinger(int timeout );
64
65 template<typename T>
66 bool readData( T& data )
67 {
68 return _readLoop( &data, sizeof(data) ) == sizeof(data);
69 }
70 bool readString( std::string& string );
71
72 bool readData( void* data, int size )
73 {
74 return _readLoop( data, size ) == size;
75 }
76
77 template<typename T>
78 bool writeData( const T& data )
79 {
80 return _writeLoop( &data, sizeof(data) ) == sizeof(data);
81 }
82 bool writeString( const std::string& string );
83
84 bool writeData( const void* data, int size )
85 {
86 return _writeLoop( data, size ) == size;
87 }
88
89public:
90 void joinMulticastGroup();
91 bool setMulticastLoopback(bool loopback_enabled);
92 bool setMulticastTTL(int multicast_ttl);
93
94 int incomingDataSize() const;
95 bool waitIncomingData( int64_t usecs ) const;
96
97 void setDelayConnect( bool d );
98 bool getDelayConnect() const;
99
100 bool setSocketReceiveBufferSize(int desired_socket_size);
101 bool setSocketTransmitBufferSize(int desired_socket_size);
102
103 bool isLocalPortValid() const;
104 bool isRemotePortValid() const;
105
106 inline int getFd() const { return _socket; }
107
108private:
109 static std::string getErrnoString(const std::string &extra);
110
111 int _readLoop(void* data, int length );
112 int _writeLoop(const void *data, int length );
113
114 void updateAddresses();
115 void setErrorString( const std::string& err );
116 void setCanRead( bool cr );
117 void setCanWrite( bool cw );
118
119 uint64_t _sent_bytes;
120 bool _canRead;
121 bool _canWrite;
122 bool _isInitialized;
123 std::string _error_string;
124
125 int _localPort;
126 int _remotePort;
127 proto _proto;
128 std::mutex _mtx;
129
130 int _socket;
131
132 std::string _localAddress;
133 std::string _remoteAddress;
134
135 bool _delay_connect;
136 bool _set_reuseaddr;
137
138};
139
140#endif // SOCKETDEVICE_H