tcp_socket.h
Go to the documentation of this file.
1 #ifndef TCPSOCKET_H
2 #define TCPSOCKET_H
3 
4 namespace argos {
5  class CTCPSocket;
6 }
7 
8 #include <argos3/core/utility/datatypes/byte_array.h>
9 #include <argos3/core/utility/datatypes/datatypes.h>
10 
11 #include <unordered_set>
12 
13 namespace argos {
14 
15  class CTCPSocket {
16 
17  public:
18 
19  enum class EEvent : UInt32 {
20  InputReady,
22  HangUp,
25  };
26 
27  public:
28 
29  CTCPSocket(int n_stream = -1);
30 
31  CTCPSocket(const CTCPSocket& c_other) = delete;
32 
33  CTCPSocket(CTCPSocket&& c_other);
34 
35  ~CTCPSocket();
36 
37  CTCPSocket& operator=(const CTCPSocket& c_other) = delete;
38 
39  CTCPSocket& operator=(CTCPSocket&& c_other);
40 
45  inline bool operator==(const CTCPSocket& c_other) const {
46  return m_nStream == c_other.m_nStream;
47  }
48 
53  inline bool IsConnected() const {
54  return m_nStream != -1;
55  }
56 
61  inline int GetStream() const {
62  return m_nStream;
63  }
64 
69  inline const std::string& GetAddress() const {
70  return m_strAddress;
71  }
72 
81  void Connect(const std::string& str_hostname,
82  SInt32 n_port);
83 
93  void Listen(SInt32 n_port,
94  SInt32 n_queue_length = 10);
95 
106  void Accept(CTCPSocket& c_socket);
107 
112  void Disconnect();
113 
118  std::unordered_set<EEvent> GetEvents();
119 
126  void SendBuffer(const UInt8* pun_buffer,
127  size_t un_size);
128 
136  bool ReceiveBuffer(UInt8* pun_buffer,
137  size_t un_size);
138 
152  void SendByteArray(const CByteArray& c_byte_array);
153 
168  bool ReceiveByteArray(CByteArray& c_byte_array);
169 
170  void SendMsg(const CByteArray& c_payload, bool b_more = false);
171 
172  void RecvMsg(CByteArray& c_payload);
173 
174  private:
175 
177  int m_nStream;
179  std::string m_strAddress;
180 
181  };
182 
183 }
184 
191 namespace std {
192  template<> struct hash<argos::CTCPSocket::EEvent> {
193  size_t operator()(const argos::CTCPSocket::EEvent& e_event) const {
194  return static_cast<size_t>(e_event);
195  }
196  };
197 }
198 
199 #endif
signed int SInt32
32-bit signed integer.
Definition: datatypes.h:93
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
unsigned char UInt8
8-bit unsigned integer.
Definition: datatypes.h:60
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
Byte array utility class.
Definition: byte_array.h:28
void Accept(CTCPSocket &c_socket)
Accept a connection from a client.
Definition: tcp_socket.cpp:149
void SendByteArray(const CByteArray &c_byte_array)
Sends the passed byte array through the socket.
Definition: tcp_socket.cpp:231
CTCPSocket & operator=(const CTCPSocket &c_other)=delete
int GetStream() const
Returns the socket stream.
Definition: tcp_socket.h:61
bool ReceiveByteArray(CByteArray &c_byte_array)
Receives the passed byte array through the socket.
Definition: tcp_socket.cpp:242
CTCPSocket(int n_stream=-1)
Definition: tcp_socket.cpp:20
bool operator==(const CTCPSocket &c_other) const
Returns true if the two sockets refer to same file descriptor.
Definition: tcp_socket.h:45
void Disconnect()
Close the socket.
Definition: tcp_socket.cpp:165
void SendMsg(const CByteArray &c_payload, bool b_more=false)
Definition: tcp_socket.cpp:258
void RecvMsg(CByteArray &c_payload)
Definition: tcp_socket.cpp:272
void Listen(SInt32 n_port, SInt32 n_queue_length=10)
Listens for connections on the specified local port.
Definition: tcp_socket.cpp:101
void SendBuffer(const UInt8 *pun_buffer, size_t un_size)
Sends the passed buffer through the socket.
Definition: tcp_socket.cpp:195
const std::string & GetAddress() const
Returns a string containing the IPv4 address in dot notation.
Definition: tcp_socket.h:69
std::unordered_set< EEvent > GetEvents()
Check the socket for events.
Definition: tcp_socket.cpp:173
bool ReceiveBuffer(UInt8 *pun_buffer, size_t un_size)
Fills the passed buffer with the data received through the socket.
Definition: tcp_socket.cpp:212
CTCPSocket(const CTCPSocket &c_other)=delete
void Connect(const std::string &str_hostname, SInt32 n_port)
Connects this socket to the specified hostname and port.
Definition: tcp_socket.cpp:61
bool IsConnected() const
Returns true if the socket is connected.
Definition: tcp_socket.h:53
size_t operator()(const argos::CTCPSocket::EEvent &e_event) const
Definition: tcp_socket.h:193