Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 1 | #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 | |
| 12 | namespace udpsocket |
| 13 | { |
| 14 | |
| 15 | std::string Channel::getRemoteAddress() const |
| 16 | { |
| 17 | char tmp[INET_ADDRSTRLEN] = { 0 }; |
Tom Joseph | c35524e | 2016-08-29 08:17:59 -0500 | [diff] [blame] | 18 | inet_ntop(AF_INET6, &address.inAddr.sin6_addr, tmp, sizeof(tmp)); |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 19 | return std::string(tmp); |
| 20 | } |
| 21 | |
| 22 | std::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 Joseph | c0f5b5d | 2017-02-09 17:47:50 +0530 | [diff] [blame] | 40 | address.addrSize = static_cast<socklen_t>(sizeof(address.inAddr)); |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 41 | |
| 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 | { |
| 53 | std::cout << "I> Channel::Read : DataIn Fd[" << sockfd << "] Req[" |
| 54 | << bufferSize << "] Recv[" << readDataLen << "]\n"; |
| 55 | } |
| 56 | else if (readDataLen == 0) // Peer has performed an orderly shutdown |
| 57 | { |
| 58 | std::cerr << "E> Channel::Read : Connection Closed Fd[" << sockfd |
| 59 | << "]\n"; |
| 60 | outBuffer.resize(0); |
| 61 | rc = -1; |
| 62 | } |
| 63 | else if (readDataLen < 0) // Error |
| 64 | { |
| 65 | rc = -errno; |
| 66 | std::cerr << "E> Channel::Read : Receive Error Fd[" << sockfd << "]" |
| 67 | << "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 | |
| 78 | int 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 | { |
Tom Joseph | c0f5b5d | 2017-02-09 17:47:50 +0530 | [diff] [blame] | 101 | address.addrSize = |
| 102 | static_cast<socklen_t>(sizeof(address.inAddr)); |
Tom Joseph | aaeb29e | 2016-08-10 06:36:33 -0500 | [diff] [blame] | 103 | do |
| 104 | { |
| 105 | writeDataLen = sendto(sockfd, // File Descriptor |
| 106 | outputPtr, // Message |
| 107 | bufferSize, // Length |
| 108 | MSG_NOSIGNAL, // Flags |
| 109 | &address.sockAddr,// Destination Address |
| 110 | address.addrSize);// Address Length |
| 111 | |
| 112 | if (writeDataLen < 0) |
| 113 | { |
| 114 | rc = -errno; |
| 115 | std::cerr << "Channel::Write: Write failed with errno:" |
| 116 | << rc << "\n"; |
| 117 | } |
| 118 | else if (static_cast<size_t>(writeDataLen) < bufferSize) |
| 119 | { |
| 120 | rc = -1; |
| 121 | std::cerr << "Channel::Write: Complete data not written" |
| 122 | " to the socket\n"; |
| 123 | } |
| 124 | } |
| 125 | while ((writeDataLen < 0) && (-(rc) == EINTR)); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | // Spurious wake up |
| 130 | std::cerr << "E> Spurious wake up on select (writeset)\n"; |
| 131 | spuriousWakeup = true; |
| 132 | } |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | if (rc == 0) |
| 137 | { |
| 138 | // Timed out |
| 139 | rc = -1; |
| 140 | std::cerr << "E> We timed out on select call (writeset)\n"; |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | // Error |
| 145 | rc = -errno; |
| 146 | std::cerr << "E> select call (writeset) had an error : " |
| 147 | << rc << "\n"; |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | } |
| 152 | while (spuriousWakeup); |
| 153 | |
| 154 | return rc; |
| 155 | } |
| 156 | |
| 157 | } // namespace udpsocket |