summaryrefslogtreecommitdiffstats
path: root/src/dabOutput/dabOutputTcp.cpp
blob: a3d7a5a0cb75d3ee668c09a45c10fa3b9f923c41 (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
#include <cstring>
#include <cstdio>
#include <signal.h>
#include <limits.h>
#include "dabOutput.h"
#include "TcpServer.h"

#ifdef _WIN32
#   include <io.h>
#   ifdef __MINGW32__
#       define FS_DECLARE_CFG_ARRAYS
#       include <winioctl.h>
#   endif
#   include <sdci.h>
#else
#   include <unistd.h>
#   include <sys/time.h>
#   ifndef O_BINARY
#       define O_BINARY 0
#   endif // O_BINARY
#endif

void* tcpThread(void* param)
{
    TcpSocket* client;

    DabOutputTcp* tcp = (DabOutputTcp*)param;

    while ((client = tcp->server->accept()) != NULL) {
        etiLog.print(TcpLog::INFO, "TCP server got a new client.\n");
        if (tcp->client != NULL) {
            delete tcp->client;
        }
        tcp->client = client;
    }
    etiLog.print(TcpLog::ERR, "TCP thread can't accept new client (%s)\n",
            inetErrDesc, inetErrMsg);

    return NULL;
}

int DabOutputTcp::Open(const char* name)
{
    char* hostport = strdup(name); // the name is actually an tuple host:port

    char* address;
    long port;
    address = strchr((char*)hostport, ':');
    if (address == NULL) {
        etiLog.printHeader(TcpLog::ERR,
                "\"%s\" is an invalid format for tcp address: "
                "should be [address]:port - > aborting\n",
                hostport);
        goto tcp_open_fail;
    }

    // terminate string hostport after the host, and advance address to the port number
    *(address++) = 0;

    port = strtol(address, (char **)NULL, 10);
    if ((port == LONG_MIN) || (port == LONG_MAX)) {
        etiLog.printHeader(TcpLog::ERR,
                "can't convert port number in tcp address %s\n", address);
        goto tcp_open_fail;
    }
    if (port == 0) {
        etiLog.printHeader(TcpLog::ERR,
                "can't use port number 0 in tcp address\n");
        goto tcp_open_fail;
    }
    address = hostport;
    if (strlen(address) > 0) {
        if (this->server->create(port, address) == -1) {
            etiLog.printHeader(TcpLog::ERR, "Can't create Tcp server on %s:%i "
                    "(%s: %s) -> aborting\n",
                    address, port, inetErrDesc, inetErrMsg);
            goto tcp_open_fail;
        }
    } else {
        if (this->server->create(port) == -1) {
            etiLog.printHeader(TcpLog::ERR, "Can't create Tcp server on :%i "
                    "(%s: %s) -> aborting\n",
                    port, inetErrDesc, inetErrMsg);
            goto tcp_open_fail;
        }
    }

    //sprintf(name, "%s:%i", this->packet_->getAddress().getHostAddress(),
    //        this->packet_->getAddress().getPort());

    if (this->server->listen() == -1) {
        etiLog.printHeader(TcpLog::ERR, "Can't listen on Tcp socket (%s: %s)\n",
                inetErrDesc, inetErrMsg);
        goto tcp_open_fail;
    }
#ifdef _WIN32
    this->thread_ = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)tcpThread, this, 0, NULL);
    if (this->thread_ == NULL) {
        fprintf(stderr, "Can't create TCP child");
        goto tcp_open_fail;
    }
#else
    if (pthread_create(&this->thread_, NULL, tcpThread, this)) {
        perror("Can't create TCP child");
        goto tcp_open_fail;
    }
#endif

    return 0;

tcp_open_fail:
    free(hostport);
    return -1;
}


int DabOutputTcp::Write(void* buffer, int size)
{

    if (this->client != NULL) {
        if (this->client->write(&size, 2) == 2) {
            if (this->client->write(buffer, size) != size) {
                return size;
            }
        }
        else {
            etiLog.print(TcpLog::INFO, "TCP server client disconnected.\n");
            delete this->client;
            this->client = NULL;
        }
    }
    return size;
}


int DabOutputTcp::Close()
{
    this->server->close();
    if( this->client != NULL )
        this->client->close();
#ifdef WIN32
    DWORD status;
    for (int i = 0; i < 5; ++i) {
        if (GetExitCodeThread(this->thread_, &status)) {
            break;
        }
        Sleep(100);
    }
    TerminateThread(this->thread_, 1);
#else
    pthread_kill(this->thread_, SIGPIPE);
#endif
    return 0;
}