TCP server for the vision library.
Change-Id: Id73304cabe3f746d72d51cc7dfe5adbe61c20c16
diff --git a/aos/vision/events/tcp_client.cc b/aos/vision/events/tcp_client.cc
new file mode 100644
index 0000000..41485f9
--- /dev/null
+++ b/aos/vision/events/tcp_client.cc
@@ -0,0 +1,67 @@
+#include "aos/vision/events/tcp_client.h"
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <unistd.h>
+
+#include "aos/common/logging/logging.h"
+
+namespace aos {
+namespace events {
+
+namespace {
+int MakeSocketNonBlocking(int sfd) {
+ int flags;
+
+ PCHECK(flags = fcntl(sfd, F_GETFL, 0));
+
+ flags |= O_NONBLOCK;
+ PCHECK(fcntl(sfd, F_SETFL, flags));
+
+ return 0;
+}
+
+int OpenClient(const char *hostname, int portno) {
+ int sockfd;
+ struct sockaddr_in serveraddr;
+ struct hostent *server;
+ /* socket: create the socket */
+ PCHECK(sockfd = socket(AF_INET, SOCK_STREAM, 0));
+
+ /* gethostbyname: get the server's DNS entry */
+ server = gethostbyname(hostname);
+ if (server == NULL) {
+ fprintf(stderr, "ERROR, no such host as %s\n", hostname);
+ exit(-1);
+ }
+
+ /* build the server's Internet address */
+ bzero((char *)&serveraddr, sizeof(serveraddr));
+ serveraddr.sin_family = AF_INET;
+ bcopy((char *)server->h_addr, (char *)&serveraddr.sin_addr.s_addr,
+ server->h_length);
+ serveraddr.sin_port = htons(portno);
+
+ /* connect: create a connection with the server */
+ PCHECK(connect(sockfd, (const struct sockaddr *)&serveraddr,
+ sizeof(serveraddr)));
+ PCHECK(MakeSocketNonBlocking(sockfd));
+
+ return sockfd;
+}
+} // namespace
+
+TcpClient::TcpClient(const char *hostname, int portno)
+ : EpollEvent(OpenClient(hostname, portno)) {}
+
+} // namespace events
+} // namespace aos