File: server.c

package info (click to toggle)
vdr-plugin-osdserver 0.1.3-23
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: ansic: 3,165; perl: 923; sh: 180; makefile: 46
file content (216 lines) | stat: -rw-r--r-- 4,201 bytes parent folder | download | duplicates (9)
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


#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <ctype.h>

#include <vdr/config.h>

#include "server.h"



cTCPConnection::cTCPConnection() {
    fd=0;
    Server=NULL;
    buffer=NULL;
    bufferLength=0;
    bufferFill=0;
    parsed=0;
    HasLine=false;
}

cTCPConnection::~cTCPConnection() {
    Close();
    if (buffer) free(buffer);
}


bool cTCPConnection::Open(int filedesc, cServer *server) {
    if (IsConnected()) return false;
    fd=filedesc;
    Server=server;

    // make fd non-blocking:
    int oldflags = fcntl(fd, F_GETFL, 0);
    if (oldflags < 0) {
        LOG_ERROR;
        Close();
        return false;
    }
    oldflags |= O_NONBLOCK;
    if (fcntl(fd, F_SETFL, oldflags) < 0) {
        LOG_ERROR;
        Close();
        return false;
    }

    buffer=(char*)malloc(InitialBufferLength);
    if (!buffer) {
        Close();
        return false;
    }
    bufferLength=InitialBufferLength;

    return true;
}

void cTCPConnection::Close() {
    if (fd>0) close(fd);
    fd=0;
}

bool cTCPConnection::Send(const char *s, int length) {
    if (!IsConnected()) return false;
    if (length < 0)
        length = strlen(s);
    if (safe_write(fd, s, length) < 0) {
        LOG_ERROR;
        return false;
    }
    return true;
}

/*

Reply-Codes:

  1xx - Debugging
  2xx - End of reply
  3xx - Status messages
  4xx - Error codes
  5xx - Data return, quoted
  6xx - Data return, multi-line

*/




char* cTCPConnection::ReadLine() {
    if (HasLine) return buffer;

    while (parsed<bufferFill) {
        if (buffer[parsed]=='\n') {
            buffer[parsed]=0;
            HasLine=true;
            return buffer;
        }
        parsed++;
    }

    // need more data
    if (bufferLength==bufferFill) {
        bufferLength+=256;
        buffer=(char*)realloc(buffer,bufferLength);
    }
    int len=read(fd,buffer+bufferFill,bufferLength-bufferFill);
    if (len==0) {
        Close();
        return NULL;
    }
    if (len<0 && errno!=EAGAIN && errno!=EINTR) {
        LOG_ERROR;
        Close();
        return NULL;
    }
    if (len>0) bufferFill+=len;

    while (parsed<bufferFill) {
        if (buffer[parsed]=='\n') {
            buffer[parsed]=0;
            HasLine=true;
            return buffer;
        }
        parsed++;
    }

    // Still need more data
    return NULL;
}

void cTCPConnection::AddSelect(cSelect &Selector) {
    Selector.SetRead(fd);
}


void cTCPConnection::DelLine() {
    if (!HasLine) return;

    memmove(buffer,buffer+parsed+1,bufferFill-parsed-1);
    bufferFill=bufferFill-parsed-1;
    parsed=0;
    HasLine=false;
}



cServer::cServer(int Port, const char *Hosts)
    : cThread("osdserver"),
      socket(this, Port,3) {

    HasOSDServerHosts = (Hosts != NULL);
    
    if (HasOSDServerHosts) {
        OSDServerHosts.Load(Hosts, true, true);
    }
}

bool cServer::cServerSocket::Accepted(in_addr_t Address)
{
    if (!server) return false;
    if (server->HasOSDServerHosts)
        return server->OSDServerHosts.Acceptable(Address);
    else   
        return SVDRPhosts.Acceptable(Address);
}

void cServer::Action() {
    if (!socket.Open()) return;

    while (Running()) {
        cSelect Selector;

        Selector.SetTimeoutMs(1000);
        Selector.SetRead(socket);

        int fd=socket.Accept();
        if (fd>0) {
            cTCPConnection *c=new cTCPConnection();
            if (c && !c->Open(fd,this)) {
                delete c;
                c=NULL;
            }
            if (c) {
                cCommandProcessor *p=new cCommandProcessor(c);
                if (p) {
                    connections.Add(p);
                } else {
                    delete c;
                }
            }
        }

        for (cCommandProcessor *p = connections.First(); p; ) {
            cCommandProcessor *next=connections.Next(p);

            if (!p->Action(Selector)) connections.Del(p);

            p=next;
        }

        int cnt=Selector.Select();
        if (cnt < 0) {
            if (errno!=EINTR) LOG_ERROR;
        }
    }

    socket.Close();
}

cServer::~cServer() {
}