UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
platform_pipe.h File Reference
#include "config_unix.h"
#include "config_win32.h"
Include dependency graph for platform_pipe.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define PLATFORM_PIPE_READ   read
 
#define PLATFORM_PIPE_WRITE   write
 

Functions

void platform_pipe_close (fd_t pipe)
 
int platform_pipe_init (fd_t pipe[2])
 

Detailed Description

Author
Martin Pulec pulec.nosp@m.@ces.nosp@m.net.c.nosp@m.z

Definition in file platform_pipe.h.

Macro Definition Documentation

◆ PLATFORM_PIPE_READ

#define PLATFORM_PIPE_READ   read

Definition at line 62 of file platform_pipe.h.

Referenced by control_thread(), and state_root::should_exit_watcher().

◆ PLATFORM_PIPE_WRITE

#define PLATFORM_PIPE_WRITE   write

Definition at line 63 of file platform_pipe.h.

Referenced by state_root::broadcast_should_exit(), and udp_exit().

Function Documentation

◆ platform_pipe_close()

void platform_pipe_close ( fd_t pipe)

Definition at line 227 of file platform_pipe.cpp.

228{
229 CLOSESOCKET(pipe);
230#ifdef _WIN32
231 WSACleanup();
232#endif
233}
#define CLOSESOCKET

References CLOSESOCKET.

Referenced by state_root::~state_root(), control_done(), control_thread(), udp_exit(), udp_reader(), and ug_control_connection_done().

◆ platform_pipe_init()

int platform_pipe_init ( fd_t pipe[2])

Definition at line 155 of file platform_pipe.cpp.

156{
157#ifdef _WIN32
158 // This needs to be called twice, indeed, since WSACleanup will be run
159 // on both ends. This call is also required because not all pipes are
160 // destroyed before common_cleanup which calls WSACleanup
161 WSADATA wsaData;
162 int err = WSAStartup(MAKEWORD(2, 2), &wsaData);
163 if (err != 0) {
164 fprintf(stderr, "WSAStartup failed with error %d.", err);
165 return -1;
166 }
167 if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
168 fprintf(stderr, "Counld not found usable version of Winsock.\n");
169 WSACleanup();
170 return -1;
171 }
172 err = WSAStartup(MAKEWORD(2, 2), &wsaData);
173 if (err != 0) {
174 fprintf(stderr, "WSAStartup failed with error %d.", err);
175 WSACleanup();
176 return -1;
177 }
178 if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
179 fprintf(stderr, "Counld not found usable version of Winsock.\n");
180 WSACleanup();
181 WSACleanup();
182 return -1;
183 }
184#endif
185
186 struct params par{};
187 fd_t sock = open_socket(&par.port);
188 if (sock == INVALID_SOCKET) {
189 perror("open_socket");
190 return system_pipe(p);
191 }
192
193 DECLARE_TIMEOUT(timeout, 1);
194 DECLARE_TIMEOUT(old_timeout, 0);
195 socklen_t old_timeout_len = sizeof old_timeout;
196 if (getsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<sockopt_t>(&old_timeout), &old_timeout_len) != 0) {
197 socket_error("pipe getsockopt");
198 }
199 if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<sockopt_t>(&timeout), sizeof timeout) != 0) {
200 socket_error("pipe setsockopt");
201 }
202
203 thread thr(worker, &par);
204
205 p[0] = accept(sock, NULL, NULL);
206 if (p[0] == INVALID_SOCKET) {
207 perror("pipe accept");
208 thr.join();
209 CLOSESOCKET(sock);
210 return system_pipe(p);
211 }
212 thr.join();
213 if (setsockopt(p[0], SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<sockopt_t>(&old_timeout), old_timeout_len) != 0) {
214 socket_error("pipe setsockopt");
215 }
216 p[1] = par.sock;
217 if (p[1] == INVALID_SOCKET) {
218 CLOSESOCKET(sock);
219 perror("accept");
220 return system_pipe(p);
221 }
222 CLOSESOCKET(sock);
223
224 return 0;
225}
int fd_t
#define INVALID_SOCKET
#define DECLARE_TIMEOUT(name, init_val_s)
#define socket_error(...)
void * sockopt_t
static fd_t open_socket(int *port)
static void * worker(void *args)
static int system_pipe(fd_t p[2])
return regular pipe if available if our implementation fails

References CLOSESOCKET, DECLARE_TIMEOUT, INVALID_SOCKET, open_socket(), params::port, params::sock, socket_error, system_pipe(), and worker().

Referenced by state_root::state_root(), control_start(), udp_init_if(), and ug_control_connection_init().

Here is the call graph for this function: