freenect_network.c
Go to the documentation of this file.
1 /*
2  * This file is part of the OpenKinect Project. http://www.openkinect.org
3  *
4  * Copyright (c) 2010 individual OpenKinect contributors. See the CONTRIB file
5  * for details.
6  *
7  * This code is licensed to you under the terms of the Apache License, version
8  * 2.0, or, at your option, the terms of the GNU General Public License,
9  * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
10  * or the following URLs:
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * http://www.gnu.org/licenses/gpl-2.0.txt
13  *
14  * If you redistribute this file in source form, modified or unmodified, you
15  * may:
16  * 1) Leave this header intact and distribute it under the same terms,
17  * accompanying it with the APACHE20 and GPL20 files, or
18  * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
19  * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
20  * In all cases you must keep the copyright notice intact and include a copy
21  * of the CONTRIB file.
22  *
23  * Binary distributions must follow the binary distribution requirements of
24  * either License.
25  */
26 
27 #include "freenect_network.h"
28 
30 int closing = 0;
31 #ifdef WIN32
32  struct addrinfo si_data;
33  PCSTR conf_port_data = "6001";
34  SOCKET data_socket = INVALID_SOCKET,
35  data_client_socket = INVALID_SOCKET;
36 #else
37  struct sockaddr_in si_data;
38  char *conf_ip = "127.0.0.1";
39  int s_data = -1,
41 #endif
43 //Init network
45 {
46  #ifndef _WIN32 //Init server UNIX
47  signal(SIGPIPE, SIG_IGN);
49  #else //Init server Winsock (Windows)
50  WORD wVersionRequested;
51  WSADATA wsaData;
52  int err;
53 
54  wVersionRequested = MAKEWORD(2, 2);
55 
56  err = WSAStartup(wVersionRequested, &wsaData);
57  if (err != 0) {
58  printf("WSAStartup failed with error: %d\n", err);
59  return 1;
60  }
62  #endif
65 
66  return 0;
67 }
68 
69 void freenect_network_read(char *buff, int *len){
70  int n;
71  #ifdef _WIN32 //Listen for data (Winsock)
72  n = recv(data_client_socket, buff, *len, 0);
73  #else //Listen for data (UNIX)
74  n = read(data_child, buff, *len);
75  #endif
76  *len = n;
77 }
78 
79 // Send Message with two bytes as metadata and pkg len as the 3rd byte
80 int freenect_network_sendMessage(int first, int second, unsigned char *data, int len) {
81  int n;
82  int m_len = 1 + 1 + sizeof(int);
83  unsigned char *msg = (unsigned char*) malloc(m_len + len);
84  memcpy(msg, &first, 1);
85  memcpy(msg + 1, &second, 1);
86  memcpy(msg + 2, &len, sizeof(int));
87  memcpy(msg + m_len, data, len);
88  #ifdef WIN32
89  n = send(data_client_socket, (char*)msg, m_len + len, 0);
90  #else
91  n = write(data_child, msg, m_len + len);
92  #endif
93  free((void*)msg);
94  return n;
95 }
96 
97 //Waiting for a client to connect
99 {
100  int childlen;
101  struct sockaddr_in childaddr;
102 
103  childlen = sizeof(childaddr);
104 
105  printf("### Wait client\n");
106  #ifdef _WIN32 //Wait for connection (Winsock)
107  data_client_socket = accept(data_socket, NULL, NULL);
108  if (data_client_socket == INVALID_SOCKET) {
109  if(!closing) printf("Error on accept() for data, exit data thread. %d\n", WSAGetLastError());
110  closesocket(data_socket);
111  WSACleanup();
112  }
113  #else //Wait for connection (UNIX)
114  data_child = accept(s_data, (struct sockaddr *)&childaddr, (unsigned int *)&childlen);
115  if ( data_child < 0 )
116  {
117  if(!closing) fprintf(stderr, "Error on accept() for data, exit data thread.\n");
118  }
119  #endif
120 
121  //callback
123 }
124 
125 //Close network socket
127 {
128  closing = 1;
129 #ifdef _WIN32
130  if ( data_socket != INVALID_SOCKET )
131  closesocket(data_socket);
132 #else
133  if ( s_data != -1 )
134  close(s_data), s_data = -1;
135 #endif
136 }
137 
138 #ifdef _WIN32
139 int freenect_network_initSocket(struct addrinfo si_type, PCSTR conf_port, SOCKET *the_socket){
140  struct addrinfo *result = NULL;
141  int iResult;
142  ZeroMemory(&si_type, sizeof (si_type));
143 
144  si_type.ai_family = AF_INET;
145  si_type.ai_socktype = SOCK_STREAM;
146  si_type.ai_protocol = IPPROTO_TCP;
147  si_type.ai_flags = AI_PASSIVE;
148 
149  iResult = getaddrinfo(NULL, conf_port, &si_type, &result);
150  if (iResult != 0) {
151  printf("Socket: getaddrinfo failed: %d\n", iResult);
152  WSACleanup();
153  return 1;
154  }
155 
156  *the_socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
157 
158  if (*the_socket == INVALID_SOCKET) {
159  printf("Socket: socket failed [%ld]\n", WSAGetLastError());
160  freeaddrinfo(result);
161  WSACleanup();
162  return 1;
163  }
164 
165  iResult = bind(*the_socket, result->ai_addr, (int)result->ai_addrlen);
166  if (iResult == SOCKET_ERROR) {
167  printf("Socket: bind failed: %d\n", WSAGetLastError());
168  freeaddrinfo(result);
169  closesocket(*the_socket);
170  WSACleanup();
171  return 1;
172  }
173 
174  freeaddrinfo(result);
175 
176  if ( listen(*the_socket, SOMAXCONN ) == SOCKET_ERROR ) {
177  printf( "Socket: listen failed [%ld]\n", WSAGetLastError() );
178  closesocket(*the_socket);
179  WSACleanup();
180  return 1;
181  }
182 
183  return 0;
184 }
185 #else
186 int freenect_network_initSocket(struct sockaddr_in si_type, int conf_port, int *the_socket) {
187  int optval = 1;
188  if ( (*the_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
189  {
190  fprintf(stderr, "Unable to create depth socket\n");
191  return -1;
192  }
193 
194  setsockopt(*the_socket, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(optval));
195 
196  memset((char *) &si_type, 0, sizeof(si_type));
197 
198  si_type.sin_family = AF_INET;
199  si_type.sin_port = htons(conf_port);
200  si_type.sin_addr.s_addr = inet_addr(conf_ip);
201 
202  if ( bind(*the_socket, (struct sockaddr *)&si_type,
203  sizeof(si_type)) < 0 )
204  {
205  fprintf(stderr, "Error at bind() for depth\n");
206  return -1;
207  }
208  if ( listen(*the_socket, 1) < 0 )
209  {
210  fprintf(stderr, "Error on listen() for depth\n");
211  return -1;
212  }
213 
214  return 0;
215 }
216 #endif
char * conf_ip
int conf_port_data
void freenect_network_wait()
callback freenect_network_connected
void freenect_network_read(char *buff, int *len)
int data_child
void freenect_network_close()
int closing
void(* callback)(void)
struct sockaddr_in si_data
int freenect_network_initSocket(struct sockaddr_in si_type, int conf_port, int *the_socket)
int freenect_network_init(callback cb)
int freenect_network_sendMessage(int first, int second, unsigned char *data, int len)
int s_data


libfreenect
Author(s): Hector Martin, Josh Blake, Kyle Machulis, OpenKinect community
autogenerated on Mon Jun 10 2019 13:46:42