OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2012 Intel Corporation. All Rights Reserved. |
| 3 * |
| 4 * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 * copy of this software and associated documentation files (the |
| 6 * "Software"), to deal in the Software without restriction, including |
| 7 * without limitation the rights to use, copy, modify, merge, publish, |
| 8 * distribute, sub license, and/or sell copies of the Software, and to |
| 9 * permit persons to whom the Software is furnished to do so, subject to |
| 10 * the following conditions: |
| 11 * |
| 12 * The above copyright notice and this permission notice (including the |
| 13 * next paragraph) shall be included in all copies or substantial portions |
| 14 * of the Software. |
| 15 * |
| 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 22 * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 */ |
| 24 |
| 25 /* |
| 26 * C++ wrapper around an TCP socket |
| 27 */ |
| 28 |
| 29 #include <stdio.h> |
| 30 #include <stdlib.h> |
| 31 #include <sys/types.h> // for data types |
| 32 #include <sys/socket.h> // for socket(), connect(), send(), recv() |
| 33 #include <netinet/in.h> // for IPPROTO_TCP, sockadd_in |
| 34 #include <arpa/inet.h> // for inet_ntoa() |
| 35 #include <unistd.h> // for close() |
| 36 #include <netdb.h> // for hostent, gethostbyname() |
| 37 #include <fcntl.h> // for fcntl() |
| 38 #include <errno.h> |
| 39 |
| 40 #include <cstring> // for memset |
| 41 |
| 42 #include "TCPSocketServer.h" |
| 43 |
| 44 using std::string; |
| 45 |
| 46 |
| 47 TCPSocketServer::TCPSocketServer(unsigned short localPort) throw(std::runtime_er
ror) : |
| 48 sockDesc(-1), |
| 49 connSockDesc(-1) |
| 50 { |
| 51 // create new socket |
| 52 if ((sockDesc = socket(PF_INET, SOCK_STREAM, 0)) < 0) { |
| 53 throw std::runtime_error("Socket creation failed (socket())"); |
| 54 } |
| 55 |
| 56 ::memset(&sockAddr, 0, sizeof(sockAddr)); |
| 57 ::memset(&connSockAddr, 0, sizeof(connSockAddr)); |
| 58 |
| 59 sockAddr.sin_family = AF_INET; |
| 60 sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 61 sockAddr.sin_port = htons(localPort); |
| 62 |
| 63 if (bind(sockDesc, (sockaddr *) &sockAddr, sizeof(sockAddr)) < 0) |
| 64 { |
| 65 throw std::runtime_error("Bind to local port failed (bind())"); |
| 66 } |
| 67 |
| 68 if (listen(sockDesc, 2) < 0) |
| 69 { |
| 70 throw std::runtime_error("Socket initialization failed (listen())"); |
| 71 } |
| 72 } |
| 73 |
| 74 |
| 75 /* Destructor */ |
| 76 TCPSocketServer::~TCPSocketServer() |
| 77 { |
| 78 if (connSockDesc > 0) { |
| 79 ::close(connSockDesc); |
| 80 } |
| 81 |
| 82 ::close(sockDesc); |
| 83 } |
| 84 |
| 85 |
| 86 /* Listen for an incoming connection */ |
| 87 void TCPSocketServer::accept(string &remoteAddr, unsigned short &remotePort) thr
ow (std::runtime_error) |
| 88 { |
| 89 if (connSockDesc > 0) { |
| 90 throw std::runtime_error("accept() called, but socket is already connect
ed"); |
| 91 } |
| 92 |
| 93 socklen_t connSockLen = sizeof(connSockAddr); |
| 94 if ((connSockDesc = ::accept(sockDesc, (sockaddr *) &connSockAddr, &connSock
Len)) < 0) { |
| 95 throw std::runtime_error("Connection accept failed (accept())"); |
| 96 } |
| 97 |
| 98 remoteAddr = inet_ntoa(connSockAddr.sin_addr); |
| 99 remotePort = ntohs(connSockAddr.sin_port); |
| 100 } |
| 101 |
| 102 |
| 103 /* Communication over socket */ |
| 104 /* Receive data */ |
| 105 ssize_t TCPSocketServer::recv(void *buffer, const size_t &bufferLen) throw (std:
:runtime_error) |
| 106 { |
| 107 if (connSockDesc <= 0) { |
| 108 throw std::runtime_error("recv() called, but socket is not connected. Ca
ll accept() first"); |
| 109 } |
| 110 |
| 111 int rval = ::read(connSockDesc, buffer, bufferLen); |
| 112 |
| 113 if (rval <= 0) { |
| 114 // EOF (connection closed by remote host) or error: |
| 115 // reset state, so a new accept() call will succeed |
| 116 connSockDesc = -1; |
| 117 ::memset(&connSockAddr, 0, sizeof(connSockAddr)); |
| 118 } |
| 119 |
| 120 if (rval == -1) { |
| 121 throw std::runtime_error("Error reading from socket (read())"); |
| 122 } |
| 123 |
| 124 return rval; |
| 125 } |
| 126 |
| 127 |
| 128 /* Send data */ |
| 129 ssize_t TCPSocketServer::send(const void *buffer, const int &bufferLen) throw (s
td::runtime_error) |
| 130 { |
| 131 if (connSockDesc <= 0) { |
| 132 throw std::runtime_error("send() called, but socket is not connected. Ca
ll accept() first"); |
| 133 } |
| 134 |
| 135 int rval = ::write(connSockDesc, buffer, bufferLen); |
| 136 |
| 137 if (rval <= 0) { |
| 138 // EOF (connection closed by remote host) or error: |
| 139 // reset state, so a new accept() call will succeed |
| 140 connSockDesc = -1; |
| 141 ::memset(&connSockAddr, 0, sizeof(connSockAddr)); |
| 142 } |
| 143 |
| 144 if (rval == -1) { |
| 145 throw std::runtime_error("Error reading from socket (read())"); |
| 146 } |
| 147 |
| 148 return rval; |
| 149 } |
| 150 |
| 151 ssize_t TCPSocketServer::send(const string &message) throw (std::runtime_error) |
| 152 { |
| 153 send(message.c_str(), message.length()); |
| 154 } |
| 155 |
| 156 |
| 157 int TCPSocketServer::recv_data(unsigned char *data, int size) |
| 158 { |
| 159 int total = 0; |
| 160 try { |
| 161 int pos = 0; |
| 162 int recvMsgSize; |
| 163 for (;;) { |
| 164 // try to receive a message |
| 165 recvMsgSize = recv(&data[pos], size); |
| 166 if ((recvMsgSize < 0) && (errno == EWOULDBLOCK)) { |
| 167 // no data received on non-blocking socket |
| 168 usleep(100); |
| 169 } else { |
| 170 total += recvMsgSize; |
| 171 if (recvMsgSize != size) { |
| 172 pos += recvMsgSize; |
| 173 size -= recvMsgSize; |
| 174 } else { |
| 175 break; |
| 176 } |
| 177 } |
| 178 } |
| 179 } |
| 180 catch (const std::exception& e) { |
| 181 printf("%s\n", e.what()); |
| 182 exit(1); |
| 183 } |
| 184 return total; |
| 185 } |
| 186 |
| 187 unsigned int TCPSocketServer::recv_uint32() |
| 188 { |
| 189 unsigned int buffer; |
| 190 recv_data((unsigned char*)&buffer, 4); |
| 191 return buffer; |
| 192 } |
OLD | NEW |