1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | |
---|
5 | #include <unistd.h> |
---|
6 | |
---|
7 | extern "C" { |
---|
8 | #include <sys/types.h> |
---|
9 | #include <sys/socket.h> |
---|
10 | #include <netinet/in.h> |
---|
11 | } |
---|
12 | |
---|
13 | #include <time.hfa> |
---|
14 | #include <thread.hfa> |
---|
15 | #include <concurrency/iofwd.hfa> |
---|
16 | |
---|
17 | //---------- |
---|
18 | monitor Printer {}; |
---|
19 | |
---|
20 | void heartbeat( Printer & mutex ) { |
---|
21 | fprintf(stderr, "."); |
---|
22 | } |
---|
23 | |
---|
24 | void message( Printer & mutex, char * _msg, size_t len ) { |
---|
25 | fprintf(stderr, "'%.*s'", len, _msg); |
---|
26 | } |
---|
27 | |
---|
28 | void status( Printer & mutex, const char * st ) { |
---|
29 | fprintf(stderr, "%s\n", st); |
---|
30 | } |
---|
31 | |
---|
32 | void error( Printer & mutex, const char * _msg, int error) { |
---|
33 | fprintf(stderr, "%s - %s\n", _msg, strerror(error)); |
---|
34 | } |
---|
35 | |
---|
36 | Printer printer; |
---|
37 | |
---|
38 | //---------- |
---|
39 | thread HeartBeat {}; |
---|
40 | |
---|
41 | void ^?{}( HeartBeat & mutex ) {} |
---|
42 | |
---|
43 | void main( HeartBeat & this ) { |
---|
44 | while(true) { |
---|
45 | waitfor( ^?{} : this ) { break; } |
---|
46 | or else{ |
---|
47 | sleep( 5`s ); |
---|
48 | heartbeat( printer ); |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | //---------- |
---|
54 | thread Server { int port; }; |
---|
55 | void main( Server & this ) { |
---|
56 | char data[256]; |
---|
57 | struct iovec iov = { data, 256 }; |
---|
58 | struct msghdr msg = { "", 0, &iov, 1, NULL, 0, 0 }; |
---|
59 | |
---|
60 | int sock = socket(AF_INET, SOCK_STREAM, 0); |
---|
61 | if(sock < 0) { |
---|
62 | error( printer, "socket", -sock); |
---|
63 | exit( EXIT_FAILURE ); |
---|
64 | } |
---|
65 | |
---|
66 | status( printer, "Socket created" ); |
---|
67 | |
---|
68 | struct sockaddr_in serv_addr; |
---|
69 | memset(&serv_addr, 0, sizeof(serv_addr)); |
---|
70 | serv_addr.sin_family = AF_INET; |
---|
71 | serv_addr.sin_addr.s_addr = INADDR_ANY; |
---|
72 | serv_addr.sin_port = htons(this.port); |
---|
73 | |
---|
74 | int ret = bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); |
---|
75 | if(ret < 0) { |
---|
76 | error( printer, "bind", -ret); |
---|
77 | exit( EXIT_FAILURE ); |
---|
78 | } |
---|
79 | |
---|
80 | status( printer, "Socket bound" ); |
---|
81 | |
---|
82 | listen(sock,1); |
---|
83 | |
---|
84 | struct sockaddr_in cli_addr; |
---|
85 | __socklen_t clilen = sizeof(cli_addr); |
---|
86 | int newsock = cfa_accept4(sock, (struct sockaddr *) &cli_addr, &clilen, 0, 0, -1`s, 0p, 0p); |
---|
87 | if (newsock < 0) { |
---|
88 | error( printer, "accept", -newsock); |
---|
89 | exit( EXIT_FAILURE ); |
---|
90 | } |
---|
91 | |
---|
92 | status( printer, "Socket accepted, looping" ); |
---|
93 | |
---|
94 | while(1) { |
---|
95 | int res = cfa_recvmsg(newsock, &msg, 0, 0, -1`s, 0p, 0p); |
---|
96 | if(res == 0) break; |
---|
97 | if(res < 0) { |
---|
98 | error( printer, "recvmsg", -res); |
---|
99 | exit( EXIT_FAILURE ); |
---|
100 | } |
---|
101 | |
---|
102 | message(printer, data, res); |
---|
103 | } |
---|
104 | |
---|
105 | ret = cfa_close(newsock, 0, -1`s, 0p, 0p); |
---|
106 | if(ret < 0) { |
---|
107 | error( printer, "close new", -ret); |
---|
108 | exit( EXIT_FAILURE ); |
---|
109 | } |
---|
110 | |
---|
111 | ret = cfa_close(sock, 0, -1`s, 0p, 0p); |
---|
112 | if(ret < 0) { |
---|
113 | error( printer, "close old", -ret); |
---|
114 | exit( EXIT_FAILURE ); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | //---------- |
---|
119 | int main(int argc, char * argv []) { |
---|
120 | if(argc != 2) { |
---|
121 | printf("usage: %s portnumber\n", argv[0]); |
---|
122 | exit( EXIT_FAILURE ); |
---|
123 | } |
---|
124 | int port = atoi(argv[1]); |
---|
125 | if(port < 1) { |
---|
126 | printf("Invalid port : %d (from %s)\n", port, argv[1]); |
---|
127 | exit( EXIT_FAILURE ); |
---|
128 | } |
---|
129 | |
---|
130 | HeartBeat heartbeat; |
---|
131 | Server server = { port }; |
---|
132 | // while(true); |
---|
133 | } |
---|