blob: e77c6cfada18b10477cd485a5ec4ff858e39f327 [file] [log] [blame]
Tom Josephaaeb29e2016-08-10 06:36:33 -05001#include "socket_channel.hpp"
2
3#include <errno.h>
4#include <netinet/in.h>
5#include <sys/ioctl.h>
6#include <sys/socket.h>
7#include <unistd.h>
8
9#include <iostream>
10#include <string>
11
12namespace udpsocket
13{
14
15std::string Channel::getRemoteAddress() const
16{
17 char tmp[INET_ADDRSTRLEN] = { 0 };
Tom Josephc35524e2016-08-29 08:17:59 -050018 inet_ntop(AF_INET6, &address.inAddr.sin6_addr, tmp, sizeof(tmp));
Tom Josephaaeb29e2016-08-10 06:36:33 -050019 return std::string(tmp);
20}
21
22std::tuple<int, buffer> Channel::read()
23{
24 int rc = 0;
25 int readSize = 0;
26 ssize_t readDataLen = 0;
27 buffer outBuffer(0);
28
29 if (ioctl(sockfd, FIONREAD, &readSize) < 0)
30 {
31 std::cerr << "E> Channel::Read : ioctl failed with errno = " << errno;
32 rc = -errno;
33 return std::make_tuple(rc, std::move(outBuffer));
34 }
35
36 outBuffer.resize(readSize);
37 auto bufferSize = outBuffer.size();
38 auto outputPtr = outBuffer.data();
39
Tom Josephc0f5b5d2017-02-09 17:47:50 +053040 address.addrSize = static_cast<socklen_t>(sizeof(address.inAddr));
Tom Josephaaeb29e2016-08-10 06:36:33 -050041
42 do
43 {
44 readDataLen = recvfrom(sockfd, // File Descriptor
45 outputPtr , // Buffer
46 bufferSize, // Bytes requested
47 0, // Flags
48 &address.sockAddr, // Address
49 &address.addrSize); // Address Length
50
Tom Joseph092f3262017-03-31 10:25:16 +053051 if (readDataLen == 0) // Peer has performed an orderly shutdown
Tom Josephaaeb29e2016-08-10 06:36:33 -050052 {
53 std::cerr << "E> Channel::Read : Connection Closed Fd[" << sockfd
54 << "]\n";
55 outBuffer.resize(0);
56 rc = -1;
57 }
58 else if (readDataLen < 0) // Error
59 {
60 rc = -errno;
61 std::cerr << "E> Channel::Read : Receive Error Fd[" << sockfd << "]"
62 << "errno = " << rc << "\n";
63 outBuffer.resize(0);
64 }
65 }
66 while ((readDataLen < 0) && (-(rc) == EINTR));
67
68 // Resize the vector to the actual data read from the socket
69 outBuffer.resize(readDataLen);
70 return std::make_tuple(rc, std::move(outBuffer));
71}
72
73int Channel::write(buffer& inBuffer)
74{
75 int rc = 0;
76 auto outputPtr = inBuffer.data();
77 auto bufferSize = inBuffer.size();
78 auto spuriousWakeup = false;
79 ssize_t writeDataLen = 0;
80 timeval varTimeout = timeout;
81
82 fd_set writeSet;
83 FD_ZERO(&writeSet);
84 FD_SET(sockfd, &writeSet);
85
86 do
87 {
88 spuriousWakeup = false;
89
90 rc = select((sockfd + 1), nullptr, &writeSet, NULL, &varTimeout);
91
92 if (rc > 0)
93 {
94 if (FD_ISSET(sockfd, &writeSet))
95 {
Tom Josephc0f5b5d2017-02-09 17:47:50 +053096 address.addrSize =
97 static_cast<socklen_t>(sizeof(address.inAddr));
Tom Josephaaeb29e2016-08-10 06:36:33 -050098 do
99 {
100 writeDataLen = sendto(sockfd, // File Descriptor
101 outputPtr, // Message
102 bufferSize, // Length
103 MSG_NOSIGNAL, // Flags
104 &address.sockAddr,// Destination Address
105 address.addrSize);// Address Length
106
107 if (writeDataLen < 0)
108 {
109 rc = -errno;
110 std::cerr << "Channel::Write: Write failed with errno:"
111 << rc << "\n";
112 }
113 else if (static_cast<size_t>(writeDataLen) < bufferSize)
114 {
115 rc = -1;
116 std::cerr << "Channel::Write: Complete data not written"
117 " to the socket\n";
118 }
119 }
120 while ((writeDataLen < 0) && (-(rc) == EINTR));
121 }
122 else
123 {
124 // Spurious wake up
125 std::cerr << "E> Spurious wake up on select (writeset)\n";
126 spuriousWakeup = true;
127 }
128 }
129 else
130 {
131 if (rc == 0)
132 {
133 // Timed out
134 rc = -1;
135 std::cerr << "E> We timed out on select call (writeset)\n";
136 }
137 else
138 {
139 // Error
140 rc = -errno;
141 std::cerr << "E> select call (writeset) had an error : "
142 << rc << "\n";
143 }
144
145 }
146 }
147 while (spuriousWakeup);
148
149 return rc;
150}
151
152} // namespace udpsocket