mpeg2enc: fix a typo
[platform/upstream/libva.git] / test / v4l_h264 / encode / TCPSocketClient.cpp
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 "TCPSocketClient.h"
30
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <unistd.h>
36 #include <netdb.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <cstring>
40 using std::string;
41
42 TCPSocketClient::TCPSocketClient(const std::string &remoteAddr, const unsigned short &remotePort) throw(std::runtime_error) :
43 sockDesc(-1)
44 {
45     ::memset(&sockAddr, 0, sizeof(sockAddr));
46     sockAddr.sin_family = AF_INET;
47     struct hostent* hp;
48     if ((hp = gethostbyname(remoteAddr.c_str())) == NULL) {
49         throw std::runtime_error("Unknown host " + remoteAddr);
50     }
51     bcopy(hp->h_addr, &sockAddr.sin_addr, hp->h_length);
52     sockAddr.sin_port = htons(remotePort);
53     if ((sockDesc = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
54         throw std::runtime_error("Socket creation failed (socket())");
55     }
56     /* Try to connect */
57     if (connect(sockDesc, (struct sockaddr *) &sockAddr, sizeof(sockAddr)) < 0) {
58         throw std::runtime_error("Error connecting to remote host (connect())");
59     }
60 }
61
62 /* Destructor */
63 TCPSocketClient::~TCPSocketClient()
64 {
65     if (sockDesc >= 0) {
66         ::close(sockDesc);
67     }
68 }
69
70 /* Communication over socket */
71 /* Receive data */
72 ssize_t TCPSocketClient::recv(void *buffer, const size_t &bufferLen) throw (std::runtime_error)
73 {
74     if (sockDesc < 0) {
75         throw std::runtime_error("socket is not connected (recv())");
76     }
77     int rval = ::read(sockDesc, buffer, bufferLen);
78     if (rval <= 0) {
79         // EOF (connection closed by remote host) or error:
80         // reset state, so a new accept() call will succeed
81         sockDesc = -1;
82         ::memset(&sockAddr, 0, sizeof(sockAddr));
83     }
84     if (rval == -1) {
85         throw std::runtime_error("Error reading from socket (read())");
86     }
87     return rval;
88 }
89
90 /* Send data */
91 ssize_t TCPSocketClient::send(const void *buffer, const int &bufferLen) throw (std::runtime_error)
92 {
93     if (sockDesc <= 0) {
94         throw std::runtime_error("socket is not connected (send())");
95     }
96
97     int rval = ::write(sockDesc, buffer, bufferLen);
98
99     if (rval <= 0) {
100         // EOF (connection closed by remote host) or error:
101         // reset state, so a new accept() call will succeed
102         sockDesc = -1;
103         ::memset(&sockAddr, 0, sizeof(sockAddr));
104     }
105
106     if (rval == -1) {
107         throw std::runtime_error("Error reading from socket (read())");
108     }
109
110     return rval;
111 }
112
113 ssize_t TCPSocketClient::send(const string &message) throw (std::runtime_error)
114 {
115     return send(message.c_str(), message.length());
116 }
117
118
119 ssize_t TCPSocketClient::send(unsigned int val) throw (std::runtime_error)
120 {
121     return send(&val, 4);
122 }
123