6 |
|
|
7 |
|
#include "Daemon.hpp" |
8 |
|
|
9 |
< |
Daemon::Daemon() |
9 |
> |
void Daemon::serve(int port, bool fork, Daemon* self) |
10 |
|
{ |
11 |
< |
// |
11 |
> |
api::TcpSocket server; |
12 |
> |
|
13 |
> |
server.Create(); |
14 |
> |
server.SetAddress(api::InternetAddress(api::InternetAddress::Any, port)); |
15 |
> |
|
16 |
> |
if (fork) |
17 |
> |
{ |
18 |
> |
switch (::fork()) |
19 |
> |
{ |
20 |
> |
case -1: |
21 |
> |
cerr << program << ": fork()\n"; |
22 |
> |
|
23 |
> |
exit(1); |
24 |
> |
case 0: |
25 |
> |
break; |
26 |
> |
default: |
27 |
> |
return; |
28 |
> |
} |
29 |
> |
} |
30 |
> |
|
31 |
> |
server.Listen(50); |
32 |
> |
|
33 |
> |
while (true) |
34 |
> |
{ |
35 |
> |
api::TcpSocket* client(new api::TcpSocket()); |
36 |
> |
|
37 |
> |
server.Accept(*client); |
38 |
> |
|
39 |
> |
api::Thread thread(etl::BindAll(&Daemon::handle, self, client)); |
40 |
> |
} |
41 |
|
} |
42 |
|
|
43 |
< |
Daemon::~Daemon() |
43 |
> |
int Daemon::handle(api::TcpSocket* client) |
44 |
|
{ |
45 |
+ |
ios::InputOutputStreamBufAdapter adapter(*client); |
46 |
+ |
iostream socket(&adapter); |
47 |
+ |
|
48 |
|
// |
49 |
+ |
|
50 |
+ |
delete client; |
51 |
|
} |