forked from bbadaboom/lg.srv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listen.c
94 lines (72 loc) · 1.45 KB
/
listen.c
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
#include <stdio.h>
#include <stdlib.h>
#ifndef _WIN32_WCE
#include <signal.h>
#endif //_WIN32_WCE
#include <stdarg.h>
#include "NetDrv.h"
static void li_close( SkLine *h )
{
}
static void _add_client( SkLine *h, int type, void *own, void *sys )
{
SkLine *l;
l = skAcceptFd( h->fd );
if ( !l )
return;
l->intid = SK_ID_HTTP;
l->other_v = 1;
if ( l->tid_sendwatch )
skRemoveTimer( l->tid_sendwatch );
l->tid_sendwatch = 0;
_clientConnected( l, 0, 0, 0 );
}
static int __listen( SkLine *h )
{
struct sockaddr_in insock;
int close_on_exec = 1;
h->fd = socket( AF_INET, SOCK_STREAM, 0 );
if ( h->fd == -1 )
return -1;
insock.sin_family = AF_INET;
insock.sin_port = htons(h->port);
insock.sin_addr.s_addr = htonl( INADDR_ANY );
#ifdef SO_REUSEADDR
{
int xxx = 1;
setsockopt(h->fd, SOL_SOCKET, SO_REUSEADDR, (char*)&xxx, sizeof(int));
}
#endif
if ( bind(h->fd, (struct sockaddr*)&insock, sizeof(insock)) )
{
close( h->fd );
h->fd = -1;
return( -1 );
}
if ( listen( h->fd, 5 ) )
{
close( h->fd );
h->fd = -1;
return( -1 );
}
skAddHandler( h, SK_H_READABLE, _add_client, 0 );
#if !defined(_WINDOWS) && !defined(_WIN32_WCE)
fcntl( h->fd, F_SETFD, &close_on_exec );
#endif
return( 0 );
}
SkLine *_lgListen( unsigned short port )
{
SkLine *l;
l = skNewLine();
l->port = port;
l->intid = SK_ID_HTTP;
l->close = li_close;
if ( __listen( l ) == -1 )
{
_skDelLine( l );
_xskDelLine( l );
l=0;
}
return l;
}