blob: 667fbc2106cc6d7a705c594b399275c913e7d56b [file] [log] [blame]
Ratan Gupta37a7a072016-12-14 00:51:23 +05301#include "sock_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 };
Ratan Gupta70b85272017-01-05 20:52:18 +053018 inet_ntop(AF_INET6, &address.inAddr.sin6_addr, tmp, sizeof(tmp));
Ratan Gupta37a7a072016-12-14 00:51:23 +053019 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 {
Ratan Gupta1ee36392017-02-10 22:25:59 +053031 std::cerr << "Channel::Read : ioctl failed with errno = " << errno;
Ratan Gupta37a7a072016-12-14 00:51:23 +053032 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
Ratan Gupta5e008872017-02-10 12:01:46 +053040 address.addrSize = static_cast<socklen_t>(sizeof(address.inAddr));
Ratan Gupta37a7a072016-12-14 00:51:23 +053041
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
51 if (readDataLen > 0) // Data read from the socket
52 {
Ratan Gupta1ee36392017-02-10 22:25:59 +053053 std::cout << "Channel::Read : DataIn Fd[" << sockfd << "] Req["
Ratan Gupta37a7a072016-12-14 00:51:23 +053054 << bufferSize << "] Recv[" << readDataLen << "]\n";
55 }
56 else if (readDataLen == 0) // Peer has performed an orderly shutdown
57 {
Ratan Gupta1ee36392017-02-10 22:25:59 +053058 std::cerr << "Channel::Read : Connection Closed Fd[" << sockfd
Ratan Gupta37a7a072016-12-14 00:51:23 +053059 << "]\n";
60 outBuffer.resize(0);
61 rc = -1;
62 }
63 else if (readDataLen < 0) // Error
64 {
65 rc = -errno;
Ratan Gupta1ee36392017-02-10 22:25:59 +053066 std::cerr << "Channel::Read : Receive Error Fd[" << sockfd << "]"
Ratan Gupta37a7a072016-12-14 00:51:23 +053067 << "errno = " << rc << "\n";
68 outBuffer.resize(0);
69 }
70 }
71 while ((readDataLen < 0) && (-(rc) == EINTR));
72
73 // Resize the vector to the actual data read from the socket
74 outBuffer.resize(readDataLen);
75 return std::make_tuple(rc, std::move(outBuffer));
76}
77
78int Channel::write(buffer& inBuffer)
79{
80 int rc = 0;
81 auto outputPtr = inBuffer.data();
82 auto bufferSize = inBuffer.size();
83 auto spuriousWakeup = false;
84 ssize_t writeDataLen = 0;
85 timeval varTimeout = timeout;
86
87 fd_set writeSet;
88 FD_ZERO(&writeSet);
89 FD_SET(sockfd, &writeSet);
90
91 do
92 {
93 spuriousWakeup = false;
94
95 rc = select((sockfd + 1), nullptr, &writeSet, NULL, &varTimeout);
96
97 if (rc > 0)
98 {
99 if (FD_ISSET(sockfd, &writeSet))
100 {
Ratan Gupta5e008872017-02-10 12:01:46 +0530101 address.addrSize = static_cast<socklen_t>(sizeof(address.inAddr));
Ratan Gupta37a7a072016-12-14 00:51:23 +0530102 do
103 {
104 writeDataLen = sendto(sockfd, // File Descriptor
105 outputPtr, // Message
106 bufferSize, // Length
107 MSG_NOSIGNAL, // Flags
108 &address.sockAddr,// Destination Address
109 address.addrSize);// Address Length
110
111 if (writeDataLen < 0)
112 {
113 rc = -errno;
114 std::cerr << "Channel::Write: Write failed with errno:"
115 << rc << "\n";
116 }
117 else if (static_cast<size_t>(writeDataLen) < bufferSize)
118 {
119 rc = -1;
120 std::cerr << "Channel::Write: Complete data not written"
121 " to the socket\n";
122 }
123 }
124 while ((writeDataLen < 0) && (-(rc) == EINTR));
125 }
126 else
127 {
128 // Spurious wake up
Ratan Gupta1ee36392017-02-10 22:25:59 +0530129 std::cerr << "Spurious wake up on select (writeset)\n";
Ratan Gupta37a7a072016-12-14 00:51:23 +0530130 spuriousWakeup = true;
131 }
132 }
133 else
134 {
135 if (rc == 0)
136 {
137 // Timed out
138 rc = -1;
Ratan Gupta1ee36392017-02-10 22:25:59 +0530139 std::cerr << "We timed out on select call (writeset)\n";
Ratan Gupta37a7a072016-12-14 00:51:23 +0530140 }
141 else
142 {
143 // Error
144 rc = -errno;
Ratan Gupta1ee36392017-02-10 22:25:59 +0530145 std::cerr << "select call (writeset) had an error : "
Ratan Gupta37a7a072016-12-14 00:51:23 +0530146 << rc << "\n";
147 }
148
149 }
150 }
151 while (spuriousWakeup);
152
153 return rc;
154}
155
156} // namespace udpsocket
Ratan Gupta70b85272017-01-05 20:52:18 +0530157