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 #ifndef __TCP_SOCKET_H__ |
| 30 #define __TCP_SOCKET_H__ |
| 31 |
| 32 #include <stdexcept> |
| 33 |
| 34 #include <netinet/in.h> // for IPPROTO_TCP, sockadd_in |
| 35 |
| 36 #ifndef SERVER_ADDR |
| 37 #define SERVER_ADDR "localhost" |
| 38 #endif |
| 39 #ifndef SERVER_PORT |
| 40 #define SERVER_PORT 8888 |
| 41 #endif |
| 42 |
| 43 |
| 44 class TCPSocketServer |
| 45 { |
| 46 public: |
| 47 /* Constructors */ |
| 48 /** |
| 49 * Construct generic TCPSocket |
| 50 * XXX: Don't use, use the constructor with explicit port specification inste
ad |
| 51 **/ |
| 52 //TCPSocketServer() throw(std::runtime_error); |
| 53 |
| 54 /** |
| 55 * Construct TCPSocket that binds to the given local port |
| 56 * parameters: |
| 57 * - localPort: port to bind to |
| 58 **/ |
| 59 TCPSocketServer(unsigned short localPort) throw(std::runtime_error); |
| 60 |
| 61 |
| 62 /* Destructor */ |
| 63 ~TCPSocketServer(); |
| 64 |
| 65 |
| 66 /* Handle incoming connections */ |
| 67 /** |
| 68 * Listen for an incoming connection. |
| 69 * This call blocks until a connection with a remote peer has been establishe
d. |
| 70 * parameters: |
| 71 * - remoteAddr: (OUT) contains address of peer |
| 72 * - remotePort: (OUT) contains port of peer |
| 73 * return value: |
| 74 * none |
| 75 **/ |
| 76 void accept(std::string &remoteAddr, unsigned short &remotePort) throw(std::
runtime_error); |
| 77 |
| 78 |
| 79 /* Communication over socket */ |
| 80 /** |
| 81 * Receive data from remote peer. |
| 82 * parameters: |
| 83 * - buffer: buffer to receive data |
| 84 * - bufferLen: maximum number of bytes to receive |
| 85 * return value: |
| 86 * number of bytes received, 0 means connection closed by peer |
| 87 **/ |
| 88 ssize_t recv(void *buffer, const size_t &bufferLen) throw (std::runtime_erro
r); |
| 89 |
| 90 /** |
| 91 * Send data to remote peer. |
| 92 * parameters: |
| 93 * - buffer: buffer to send |
| 94 * - bufferLen: number of bytes in buffer |
| 95 * return value: |
| 96 * number of bytes actually written |
| 97 **/ |
| 98 ssize_t send(const void *buffer, const int &bufferLen) throw (std::runtime_e
rror); |
| 99 |
| 100 /** |
| 101 * Sends the given string over the TCP connection. |
| 102 * This is a convenience method which calls the previous method with the corr
ect |
| 103 * length parameter. |
| 104 * parameters: |
| 105 * - message: message to send |
| 106 * return value: |
| 107 * number of bytes actually written |
| 108 **/ |
| 109 ssize_t send(const std::string &message) throw (std::runtime_error); |
| 110 |
| 111 |
| 112 int recv_data(unsigned char *data, int size); |
| 113 unsigned int recv_uint32(); |
| 114 |
| 115 |
| 116 private: |
| 117 // don't allow value semantics on this object |
| 118 TCPSocketServer(const TCPSocketServer &sock); |
| 119 void operator=(const TCPSocketServer &sock); |
| 120 |
| 121 int sockDesc; // listening socket descriptor |
| 122 sockaddr_in sockAddr; // structure keeping IP and port of peer |
| 123 |
| 124 int connSockDesc; // connected socket descriptor |
| 125 sockaddr_in connSockAddr; |
| 126 }; |
| 127 |
| 128 #endif // __TCP_SOCKET_H__ |
OLD | NEW |