daemon-pipetest.c 2.89 KB
Newer Older
Sandrine Avakian's avatar
Sandrine Avakian committed
1 2
/*
daemon-pipetest.c
3
Copyright (C) 2016 Belledonne Communications, Grenoble, France
Sandrine Avakian's avatar
Sandrine Avakian committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at
your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
*/

Yann Diorcet's avatar
Yann Diorcet committed
20 21
#define _GNU_SOURCE
#include <fcntl.h>
22
#include <stdio.h>
Yann Diorcet's avatar
Yann Diorcet committed
23

24
#ifndef _WIN32
Yann Diorcet's avatar
Yann Diorcet committed
25
#include <poll.h>
26
#endif
Yann Diorcet's avatar
Yann Diorcet committed
27 28 29 30 31 32 33 34



#include "ortp/ortp.h"

static int running=1;

int main(int argc, char *argv[]){
35 36
	char buf[32768];
	ortp_pipe_t fd;
37

38 39 40 41 42 43
	/* handle args */
	if (argc < 2) {
		ortp_error("Usage: %s pipename", argv[0]);
		return 1;
	}

Yann Diorcet's avatar
Yann Diorcet committed
44
	ortp_init();
45 46 47 48
	ortp_set_log_level_mask(NULL, ORTP_MESSAGE | ORTP_WARNING | ORTP_ERROR | ORTP_FATAL);

	fd=ortp_client_pipe_connect(argv[1]);
	if (fd==(ortp_pipe_t)-1){
Yann Diorcet's avatar
Yann Diorcet committed
49 50 51
		ortp_error("Could not connect to control pipe: %s",strerror(errno));
		return -1;
	}
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

#ifdef _WIN32
	DWORD fdwMode, fdwOldMode;
	HANDLE hin = GetStdHandle(STD_INPUT_HANDLE);
	HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
	GetConsoleMode(hin, &fdwOldMode);
	fdwMode = fdwOldMode ^ ENABLE_MOUSE_INPUT ^ ENABLE_WINDOW_INPUT;
	SetConsoleMode(hin, fdwMode);
	FlushConsoleInputBuffer(hin);
	while (running) {
		DWORD read = 0;
		DWORD written = 0;
		ReadFile(hin, buf, sizeof(buf), &read, NULL);
		if (read > 2) {
			buf[read - 2] = '\0'; // Remove ending '\r\n'
Ghislain MARY's avatar
Ghislain MARY committed
67
			if (ortp_pipe_write(fd, (uint8_t *)buf, (int)strlen(buf)) < 0) {
68 69 70 71 72 73 74 75 76 77 78 79 80 81
				running = 0;
			} else {
				read = ortp_pipe_read(fd, (uint8_t *)buf, sizeof(buf));
				if (read > 0) {
					WriteFile(hout, buf, read, &written, NULL);
				} else {
					running = 0;
				}
			}
		}
	}
	SetConsoleMode(hin, fdwOldMode);
#else
	struct pollfd pfds[2] = { { 0 } };
82
	ssize_t bytes;
Yann Diorcet's avatar
Yann Diorcet committed
83 84 85 86 87 88 89 90 91 92 93
	pfds[0].fd=fd;
	pfds[0].events=POLLIN;
	pfds[1].fd=1;
	pfds[1].events=POLLIN;
	while (running){
		int err;
		err=poll(pfds,2,-1);
		if (err>0){
			/*splice to stdout*/
			if (pfds[0].revents & POLLIN){
				if ((bytes=read(pfds[0].fd,buf,sizeof(buf)))>0){
94
					if (write(0,buf,(size_t)bytes)==-1){
Yann Diorcet's avatar
Yann Diorcet committed
95 96 97
						ortp_error("Fail to write to stdout?");
						break;
					}
98
					fprintf(stdout,"\n");
Yann Diorcet's avatar
Yann Diorcet committed
99 100 101 102 103 104 105
				}else if (bytes==0){
					break;
				}
			}
			/*splice from stdin to pipe */
			if (pfds[1].revents & POLLIN){
				if ((bytes=read(pfds[1].fd,buf,sizeof(buf)))>0){
106
					if (write(pfds[0].fd,buf,(size_t)bytes)==-1){
Yann Diorcet's avatar
Yann Diorcet committed
107 108
						ortp_error("Fail to write to unix socket");
						break;
109
					}
Yann Diorcet's avatar
Yann Diorcet committed
110 111 112 113 114 115
				}else if (bytes==0){
					break;
				}
			}
		}
	}
116 117
#endif

Yann Diorcet's avatar
Yann Diorcet committed
118 119
	return 0;
}