aboutsummaryrefslogtreecommitdiffstats
path: root/src/dabInputSlip.cpp
blob: 7663d2cf80931e9eddf98fbc08878b2056d4ade5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
   Copyright (C) 2009 Her Majesty the Queen in Right of Canada (Communications
   Research Center Canada)
   */
/*
   This file is part of CRC-DabMux.

   CRC-DabMux is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   CRC-DabMux 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with CRC-DabMux.  If not, see <http://www.gnu.org/licenses/>.
   */

#include "dabInputSlip.h"
#include "dabInputFifo.h"
#include "TcpServer.h"
#include "UdpSocket.h"
#include "bridge.h"

#include <signal.h>
#include <string.h>
#include <limits.h>


#ifdef _WIN32
#   include <io.h>

#   define sem_t HANDLE
#   define O_NONBLOCK 0
#else
#   include <semaphore.h>
#   define O_BINARY 0
#endif


struct dabInputSlipData {
    TcpServer* server;
    UdpPacket** packets;
    UdpPacket* buffer;
    bridgeInfo* info;
    dabInputFifoStats stats;
    pthread_t thread;
    sem_t semWrite;
    sem_t semQueue;
    bool reading;
    volatile int nbPackets;
    volatile int queueSize;
    volatile int packetSize;
};


struct dabInputOperations dabInputSlipOperations = {
    dabInputSlipInit,
    dabInputSlipOpen,
    dabInputSlipSetbuf,
    dabInputSlipRead,
    NULL,
    NULL,
    dabInputSlipReadFrame,
    dabInputSetbitrate,
    dabInputSlipClose,
    dabInputSlipClean,
    NULL
};


int dabInputSlipInit(void** args)
{
    dabInputSlipData* data = new dabInputSlipData;
    memset(&data->stats, 0, sizeof(data->stats));
    data->stats.id = dabInputFifoData::nb++;
    data->server = new TcpServer();
    data->packetSize = 1500;
    data->queueSize = 10;
    data->packets = new UdpPacket*[data->queueSize];
    for (int i = 0; i < data->queueSize; ++i) {
        data->packets[i] = new UdpPacket(data->packetSize);
    }
    data->buffer = new UdpPacket(data->packetSize);
    data->nbPackets = 0;
    data->info = new bridgeInfo;
    data->thread = (pthread_t)NULL;
    bridgeInitInfo(data->info);

#ifdef _WIN32
    char semName[32];
    sprintf(semName, "semWrite%i", data->stats.id);
    data->semWrite = CreateSemaphore(NULL, 1, 1, semName);
    if (data->semWrite == NULL) {
        fprintf(stderr, "Can't init SLIP data write semaphore %s\n", semName);
        return -1;
    }
    sprintf(semName, "semQueue%i", data->stats.id);
    data->semQueue = CreateSemaphore(NULL, 1, 1, semName);
    if (data->semQueue == NULL) {
        fprintf(stderr, "Can't init SLIP data index semaphore %s\n", semName);
        return -1;
    }
#else
    if (sem_init(&data->semWrite, 0, data->queueSize) == -1) {
        perror("Can't init SLIP data write semaphore");
        return -1;
    }
    if (sem_init(&data->semQueue, 0, 1) == -1) {
        perror("Can't init SLIP data index semaphore");
        return -1;
    }
#endif
    data->reading = false;

    *args = data;
    return 0;
}


void* dabInputSlipThread(void* args)
{
    dabInputSlipData* data = (dabInputSlipData*)args;
    TcpSocket* client;

    while ((client = data->server->accept()) != NULL) {
        int size = 0;
        etiLog.print(TcpLog::INFO, "SLIP server got a new client.\n");

#ifdef _WIN32
        WaitForSingleObject(data->semWrite, INFINITE);
                WaitForSingleObject(data->semQueue, INFINITE);
#else
        sem_wait(&data->semWrite);
        sem_wait(&data->semQueue);
#endif
        UdpPacket* packet = data->packets[data->nbPackets];
#ifdef _WIN32
        ReleaseSemaphore(data->semQueue, 1, NULL);
#else
        sem_post(&data->semQueue);
#endif

        while ((size = client->read(packet->getData(), packet->getSize()))
                    > 0) {
            packet->setLength(size);
#ifdef _WIN32
            WaitForSingleObject(data->semQueue, INFINITE);
#else
            sem_wait(&data->semQueue);
#endif
            data->nbPackets++;
#ifdef _WIN32
            ReleaseSemaphore(data->semQueue, 1, NULL);
#else
            sem_post(&data->semQueue);
#endif
            
#ifdef _WIN32
            WaitForSingleObject(data->semWrite, INFINITE);
            WaitForSingleObject(data->semQueue, INFINITE);
#else
            sem_wait(&data->semWrite);
            sem_wait(&data->semQueue);
#endif
            packet = data->packets[data->nbPackets];
#ifdef _WIN32
            ReleaseSemaphore(data->semQueue, 1, NULL);
#else
            sem_post(&data->semQueue);
#endif
        }
        etiLog.print(TcpLog::INFO, "SLIP server client deconnected.\n");
        client->close();
    }
    etiLog.print(TcpLog::ERR, "SLIP thread can't accept new client (%s)\n",
            inetErrDesc, inetErrMsg);

    return NULL;
}


int dabInputSlipOpen(void* args, const char* inputName)
{
    const char* address;
    long port;
    address = strchr(inputName, ':');
    if (address == NULL) {
        etiLog.print(TcpLog::ERR, "\"%s\" SLIP address format is invalid: "
                "should be [address]:port - > aborting\n", inputName);
        return -1;
    }
    ++address;
    port = strtol(address, (char **)NULL, 10);
    if ((port == LONG_MIN) || (port == LONG_MAX)) {
        etiLog.print(TcpLog::ERR, "can't convert port number in SLIP address %s\n",
                address);
        return -1;
    }
    if (port == 0) {
        etiLog.print(TcpLog::ERR, "can't use port number 0 in SLIP address\n");
        return -1;
    }
    dabInputSlipData* data = (dabInputSlipData*)args;
    if (data->server->create(port) == -1) {
        etiLog.print(TcpLog::ERR, "can't set port %i on SLIP input (%s: %s)\n",
                port, inetErrDesc, inetErrMsg);
        return -1;
    }

    if (data->server->listen() == -1) {
        etiLog.print(TcpLog::ERR, "can't listen on SLIP socket(%s: %s)\n",
                inetErrDesc, inetErrMsg);
        return -1;
    }
#ifdef _WIN32
    data->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)dabInputSlipThread, data, 0, NULL);
    if (data->thread == NULL) {
        fprintf(stderr, "Can't create SLIP child");
        return -1;
    }
#else
    if (pthread_create(&data->thread, NULL, dabInputSlipThread, data)) {
        perror("Can't create SLIP child");
        return -1;
    }
#endif

    etiLog.print(TcpLog::DBG, "check return code of create\n");
    return 0;
}


int dabInputSlipSetbuf(void* args, int size)
{
    dabInputSlipData* data = (dabInputSlipData*)args;

    if (size <= 10) {
        return -1;
    }

    data->packetSize = size - 10;
    if (data->packets == NULL) {
        data->packets = new UdpPacket*[data->queueSize];
    }
    for (int i = 0; i < data->queueSize; ++i) {
        if (data->packets[i] == NULL) {
            data->packets[i] = new UdpPacket(data->packetSize);
        } else {
            data->packets[i]->setSize(data->packetSize);
        }
    }
    if (data->buffer == NULL) {
        data->buffer = new UdpPacket(data->packetSize);
    } else {
        data->buffer->setSize(data->packetSize);
    }

    return 0;
}


int dabInputSlipRead(void* args, void* buffer, int size)
{
    dabInputSlipData* data = (dabInputSlipData*)args;

    if (data->nbPackets > 0) {  // data ready
        UdpPacket* temp;
        temp = data->buffer;
        data->buffer = data->packets[0];

#ifdef _WIN32
        WaitForSingleObject(data->semQueue, INFINITE);
#else
        sem_wait(&data->semQueue);
#endif
        for (int i = 1; i < data->queueSize; ++i) {
            data->packets[i - 1] = data->packets[i];
        }
        data->packets[data->queueSize - 1] = temp;
        --data->nbPackets;
#ifdef _WIN32
        ReleaseSemaphore(data->semQueue, 1, NULL);
        ReleaseSemaphore(data->semWrite, 1, NULL);
#else
        sem_post(&data->semQueue);
        sem_post(&data->semWrite);
#endif
    } else {
        data->buffer->setLength(0);
    }

    return data->buffer->getLength();
}


int dabInputSlipReadFrame(dabInputOperations* ops, void* args, void* buffer, int size)
{
    int nbBytes = 0;
    dabInputSlipData* data = (dabInputSlipData*)args;
    dabInputFifoStats* stats = (dabInputFifoStats*)&data->stats;

#ifdef _WIN32
    WaitForSingleObject(data->semQueue, INFINITE);
#else
    sem_wait(&data->semQueue);
#endif
    stats->bufferRecords[stats->bufferCount].curSize = data->nbPackets;
    stats->bufferRecords[stats->bufferCount].maxSize = data->queueSize;
#ifdef _WIN32
    ReleaseSemaphore(data->semQueue, 1, NULL);
#else
    sem_post(&data->semQueue);
#endif
    if (++stats->bufferCount == NB_RECORDS) {
        etiLog.print(TcpLog::INFO, "SLIP buffer state: (%i)", stats->id);
        for (int i = 0; i < stats->bufferCount; ++i) {
            etiLog.print(TcpLog::INFO, " %i/%i",
                    stats->bufferRecords[i].curSize,
                    stats->bufferRecords[i].maxSize);
        }
        etiLog.print(TcpLog::INFO, "\n");

        stats->bufferCount = 0;
    }

    data->stats.frameRecords[data->stats.frameCount].curSize = 0;
    data->stats.frameRecords[data->stats.frameCount].maxSize = size;

    if (data->buffer->getLength() == 0) {
        ops->read(args, NULL, 0);
    }
    while ((nbBytes = writePacket(data->buffer->getData(),
                    data->buffer->getLength(), buffer, size, data->info))
            != 0) {
        data->stats.frameRecords[data->stats.frameCount].curSize = nbBytes;
        ops->read(args, NULL, 0);
    }

    if (data->buffer->getLength() != 0) {
        data->stats.frameRecords[data->stats.frameCount].curSize = size;
    }

    if (++stats->frameCount == NB_RECORDS) {
        etiLog.print(TcpLog::INFO, "Data subchannel usage: (%i)",
                stats->id);
        for (int i = 0; i < stats->frameCount; ++i) {
            etiLog.print(TcpLog::INFO, " %i/%i",
                    stats->frameRecords[i].curSize,
                    stats->frameRecords[i].maxSize);
        }
        etiLog.print(TcpLog::INFO, "\n");
        stats->frameCount = 0;
    }
    return size;
}


int dabInputSlipClose(void* args)
{
    dabInputSlipData* data = (dabInputSlipData*)args;
    data->server->close();
#ifdef WIN32
    DWORD status;
    for (int i = 0; i < 5; ++i) {
        if (GetExitCodeThread(data->thread, &status)) {
            break;
        }
        Sleep(100);
    }
    TerminateThread(data->thread, 1);
#else
    if (data->thread != (pthread_t)NULL) {
        pthread_kill(data->thread, SIGPIPE);
    }
#endif
    return 0;
}


int dabInputSlipClean(void** args)
{
    dabInputSlipData* data = (dabInputSlipData*)(*args);
#ifdef _WIN32
    CloseHandle(data->thread);
    CloseHandle(data->semWrite);
    CloseHandle(data->semQueue);
#else
    sem_destroy(&data->semWrite);
    sem_destroy(&data->semQueue);
#endif
    for (int i = 0; i < data->queueSize; ++i) {
        if (data->packets[i] != NULL) {
            delete data->packets[i];
        }
    }
    delete []data->packets;
    delete data->buffer;
    delete data->server;
    delete data->info;
    delete data;
    return 0;
}