3 #include <argos3/core/utility/string_utilities.h>
10 #include <sys/socket.h>
13 #include <arpa/inet.h>
28 m_nStream(c_other.m_nStream),
29 m_strAddress(c_other.m_strAddress) {
31 c_other.m_nStream = -1;
32 c_other.m_strAddress.clear();
42 m_nStream = c_other.m_nStream;
43 m_strAddress = c_other.m_strAddress;
45 c_other.m_nStream = -1;
46 c_other.m_strAddress.clear();
66 ::addrinfo tHints, *ptInterfaceInfo;
67 ::memset(&tHints, 0,
sizeof(tHints));
68 tHints.ai_family = AF_INET;
69 tHints.ai_socktype = SOCK_STREAM;
70 nRetVal = ::getaddrinfo(str_hostname.c_str(),
79 ::addrinfo* ptInterface =
nullptr;
80 for(ptInterface = ptInterfaceInfo;
81 (ptInterface !=
nullptr) && (m_nStream == -1);
82 ptInterface = ptInterface->ai_next) {
83 m_nStream = ::socket(ptInterface->ai_family,
84 ptInterface->ai_socktype,
85 ptInterface->ai_protocol);
87 if(::connect(m_nStream,
89 ptInterface->ai_addrlen) == -1) {
95 ::freeaddrinfo(ptInterfaceInfo);
106 ::addrinfo tHints, *ptInterfaceInfo;
107 ::memset(&tHints, 0,
sizeof(tHints));
108 tHints.ai_family = AF_INET;
109 tHints.ai_socktype = SOCK_STREAM;
110 tHints.ai_flags = AI_PASSIVE;
111 nRetVal = ::getaddrinfo(
nullptr,
120 ::addrinfo* ptInterface =
nullptr;
121 for(ptInterface = ptInterfaceInfo;
122 (ptInterface !=
nullptr) && (m_nStream == -1);
123 ptInterface = ptInterface->ai_next) {
124 m_nStream = ::socket(ptInterface->ai_family,
125 ptInterface->ai_socktype,
126 ptInterface->ai_protocol);
129 if(::setsockopt(m_nStream, SOL_SOCKET, SO_REUSEADDR, &nTrue,
sizeof(nTrue)) != 0 ||
130 ::bind(m_nStream, ptInterface->ai_addr, ptInterface->ai_addrlen) != 0) {
135 ::freeaddrinfo(ptInterfaceInfo);
136 if(m_nStream == -1) {
140 if(::listen(m_nStream, n_queue_length) == -1) {
152 ::socklen_t tAddressLen =
sizeof(tAddress);
153 int nNewStream = ::accept(m_nStream, &tAddress, &tAddressLen);
154 if(nNewStream == -1) {
158 c_socket.m_nStream = nNewStream;
159 c_socket.m_strAddress = ::inet_ntoa(
reinterpret_cast< ::sockaddr_in*
>(&tAddress)->sin_addr);
174 std::unordered_set<EEvent> setEvents;
175 ::pollfd tFileDescriptor;
176 tFileDescriptor.fd = m_nStream;
177 tFileDescriptor.events = POLLIN | POLLOUT;
178 ::poll(&tFileDescriptor, 1, 1);
179 if(tFileDescriptor.revents & POLLIN)
181 if(tFileDescriptor.revents & POLLOUT)
183 if(tFileDescriptor.revents & POLLHUP)
185 if(tFileDescriptor.revents & POLLERR)
187 if(tFileDescriptor.revents & POLLNVAL)
199 nSent = ::send(m_nStream, pun_buffer, un_size, 0);
216 nReceived = ::recv(m_nStream, pun_buffer, un_size, 0);
221 if(nReceived == 0)
return false;
222 un_size -= nReceived;
223 pun_buffer += nReceived;
233 UInt32 unSizeNBO = htonl(c_byte_array.
Size());
247 c_byte_array.
Resize(ntohl(unSizeNBO));
261 *
reinterpret_cast<UInt16*
>(punHead) = htons(c_payload.
Size());
279 unPayloadSize = ntohs(*
reinterpret_cast<UInt16*
>(pchHead));
280 bMore = (pchHead[2] == 1);
285 c_payload.
AddBuffer(pchBuf, unPayloadSize);
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
signed int SInt32
32-bit signed integer.
unsigned int UInt32
32-bit unsigned integer.
unsigned char UInt8
8-bit unsigned integer.
unsigned short UInt16
16-bit unsigned integer.
The namespace containing all the ARGoS related code.
std::string ToString(const T &t_value)
Converts the given parameter to a std::string.
Byte array utility class.
size_t Size() const
Returns the current size of the byte array.
CByteArray & AddBuffer(const UInt8 *pun_buffer, size_t un_size)
Appends bytes to the byte array.
const UInt8 * ToCArray() const
Returns the contents of the byte array as a const c-style array.
void Resize(size_t un_size, UInt8 un_value=0)
Resizes the byte array to the wanted size.
void Accept(CTCPSocket &c_socket)
Accept a connection from a client.
void SendByteArray(const CByteArray &c_byte_array)
Sends the passed byte array through the socket.
CTCPSocket & operator=(const CTCPSocket &c_other)=delete
bool ReceiveByteArray(CByteArray &c_byte_array)
Receives the passed byte array through the socket.
CTCPSocket(int n_stream=-1)
void Disconnect()
Close the socket.
void SendMsg(const CByteArray &c_payload, bool b_more=false)
void RecvMsg(CByteArray &c_payload)
void Listen(SInt32 n_port, SInt32 n_queue_length=10)
Listens for connections on the specified local port.
void SendBuffer(const UInt8 *pun_buffer, size_t un_size)
Sends the passed buffer through the socket.
std::unordered_set< EEvent > GetEvents()
Check the socket for events.
bool ReceiveBuffer(UInt8 *pun_buffer, size_t un_size)
Fills the passed buffer with the data received through the socket.
void Connect(const std::string &str_hostname, SInt32 n_port)
Connects this socket to the specified hostname and port.