| 1 |
/* |
| 2 |
* Component of the D-ITG 2.8.1 (r1023) platform (http://traffic.comics.unina.it/software/ITG) |
| 3 |
* |
| 4 |
* Copyright : (C) 2004-2013 by Alessio Botta, Walter de Donato, Alberto Dainotti, |
| 5 |
* Stefano Avallone, Antonio Pescape' (PI) |
| 6 |
* COMICS (COMputer for Interaction and CommunicationS) Group |
| 7 |
* Department of Electrical Engineering and Information Technologies |
| 8 |
* University of Napoli "Federico II". |
| 9 |
* email : [email protected], [email protected], [email protected], |
| 10 |
* [email protected], [email protected] |
| 11 |
* |
| 12 |
* This program is free software: you can redistribute it and/or modify |
| 13 |
* it under the terms of the GNU General Public License as published by |
| 14 |
* the Free Software Foundation, either version 3 of the License, or |
| 15 |
* (at your option) any later version. |
| 16 |
* |
| 17 |
* This program is distributed in the hope that it will be useful, |
| 18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
* GNU General Public License for more details. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU General Public License |
| 23 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 |
* |
| 25 |
* For commercial use please refer to D-ITG Professional. |
| 26 |
*/ |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#include "../common/ITG.h" |
| 31 |
#include "../common/debug.h" |
| 32 |
#include <unistd.h> |
| 33 |
#include "pipes.h" |
| 34 |
|
| 35 |
#ifdef WIN32 |
| 36 |
int numPipes = 0; |
| 37 |
HANDLE mutex_numPipes; |
| 38 |
#endif |
| 39 |
|
| 40 |
#ifdef UNIX |
| 41 |
int sendPipeMsg(int fd[2], pipeMsg *msg) |
| 42 |
{ |
| 43 |
write(fd[1], msg, sizeof(pipeMsg)); |
| 44 |
return(0); |
| 45 |
} |
| 46 |
|
| 47 |
int recvPipeMsg(int fd[2], pipeMsg *msg) |
| 48 |
{ |
| 49 |
read(fd[0], msg, sizeof(pipeMsg)); |
| 50 |
return(0); |
| 51 |
} |
| 52 |
|
| 53 |
int createNewPipe(int fd[2]) |
| 54 |
{ |
| 55 |
return(pipe(fd)); |
| 56 |
} |
| 57 |
|
| 58 |
int closePipe(int fd[2]) |
| 59 |
{ |
| 60 |
close(fd[1]); |
| 61 |
close(fd[0]); |
| 62 |
return(0); |
| 63 |
|
| 64 |
} |
| 65 |
#endif |
| 66 |
|
| 67 |
#ifdef WIN32 |
| 68 |
int sendPipeMsg(HANDLE pipe[3], pipeMsg *msg) |
| 69 |
{ |
| 70 |
DWORD written = 0; |
| 71 |
DWORD err; |
| 72 |
HANDLE fd = pipe[0]; |
| 73 |
HANDLE event = pipe[1]; |
| 74 |
|
| 75 |
if (WriteFile(fd, msg, sizeof(pipeMsg), &written, NULL) == 0) { |
| 76 |
printf("Error writing in pipe, with written = %ld\n",written); |
| 77 |
err = GetLastError(); |
| 78 |
printf("GetLastError returned %ld\n", err); |
| 79 |
return(-1); |
| 80 |
} |
| 81 |
if (written != sizeof(pipeMsg)) { |
| 82 |
printf("Error writing in pipe: written != sizeof(pipeMsg)\n"); |
| 83 |
return(-1); |
| 84 |
} |
| 85 |
|
| 86 |
PRINTD(1,"sendPipeMsg: written: %d\n", (int) written); |
| 87 |
SetEvent(event); |
| 88 |
return(0); |
| 89 |
} |
| 90 |
|
| 91 |
int recvPipeMsg(HANDLE pipe[3], pipeMsg *msg) |
| 92 |
{ |
| 93 |
DWORD read = 0; |
| 94 |
DWORD err; |
| 95 |
|
| 96 |
|
| 97 |
if (ReadFile(pipe[2], msg, sizeof(pipeMsg) , &read , NULL) == 0) { |
| 98 |
printf("Error reading from pipe, with read = %ld\n", read); |
| 99 |
err = GetLastError(); |
| 100 |
printf("GetLastError returned %ld\n", err); |
| 101 |
return(-1); |
| 102 |
} |
| 103 |
if (read != sizeof(pipeMsg)) { |
| 104 |
printf("Error reading from pipe: read != sizeof(pipeMsg)\n"); |
| 105 |
return(-1); |
| 106 |
} |
| 107 |
|
| 108 |
return(0); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
int createNewPipe(HANDLE pipe[3]) |
| 113 |
{ |
| 114 |
int num; |
| 115 |
char pipename[30]; |
| 116 |
HANDLE hpipe, namedPipe; |
| 117 |
WSAEVENT event; |
| 118 |
char stringa[30]; |
| 119 |
|
| 120 |
MUTEX_THREAD_LOCK(mutex_numPipes); |
| 121 |
num = numPipes++; |
| 122 |
MUTEX_THREAD_UNLOCK(mutex_numPipes); |
| 123 |
|
| 124 |
|
| 125 |
strcpy(pipename,"\\\\.\\pipe\\"); |
| 126 |
strcat(pipename, nameProgram); |
| 127 |
sprintf(stringa, "%d", num); |
| 128 |
strcat(pipename, stringa); |
| 129 |
|
| 130 |
PRINTD(1,"createNewPipe: pipename: %s\n", pipename); |
| 131 |
|
| 132 |
|
| 133 |
namedPipe = CreateNamedPipe( |
| 134 |
(LPTSTR)pipename, |
| 135 |
PIPE_ACCESS_DUPLEX, |
| 136 |
PIPE_TYPE_BYTE | |
| 137 |
PIPE_READMODE_BYTE | |
| 138 |
PIPE_WAIT, |
| 139 |
PIPE_UNLIMITED_INSTANCES, |
| 140 |
PIPE_BUFSIZE, |
| 141 |
PIPE_BUFSIZE, |
| 142 |
PIPE_TIMEOUT, |
| 143 |
NULL |
| 144 |
); |
| 145 |
if (namedPipe == INVALID_HANDLE_VALUE) { |
| 146 |
printf("Error in CreateNamedPipe()\n"); |
| 147 |
return (-1); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
hpipe = CreateFile(pipename, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0); |
| 152 |
if (hpipe == INVALID_HANDLE_VALUE) { |
| 153 |
printf("Error in CreateFile()\n"); |
| 154 |
return (-1); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
event = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 159 |
ResetEvent(event); |
| 160 |
|
| 161 |
pipe[0] = hpipe; |
| 162 |
pipe[1] = event; |
| 163 |
pipe[2] = namedPipe; |
| 164 |
return(0); |
| 165 |
} |
| 166 |
|
| 167 |
int closePipe(HANDLE pipe[3]) |
| 168 |
{ |
| 169 |
CloseHandle(pipe[2]); |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
#endif |
| 174 |
|
| 175 |
|