Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / openchess / src / net.c
blob2741baa2c4ca40397a722ed29077d36b2efe351d
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "config.h"
22 #include "client.h"
23 #include "proto.h"
24 #include "net.h"
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <sys/socket.h>
32 #include <errno.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <sys/types.h>
37 int sock; // main socket
38 char buf[257]; // receive buffer
39 unsigned addrlen; // length of clientInfo
42 struct sockaddr_in sockName; // name of socket
43 struct sockaddr_in clientInfo; // connected client information
45 extern int errno;
47 extern client_t client_list;
49 /** Network Send function */
50 int net_send (int sock, char *buf, unsigned len)
52 return send (sock, buf, len, 0);
55 /** Network Init function */
56 int init_net ()
58 // Create mainsocket
59 if ((sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
60 printf ("> ERROR -> I can't create main socket");
61 return 0;
64 /* Set to nonblocking socket mode */
65 int oldFlag = fcntl (sock, F_GETFL, 0);
66 if (fcntl (sock, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
67 printf ("> ERROR -> Problem with set socket mode");
68 return 0;
71 #ifdef __linux__
72 /* Unblock re-use of previously binded port, when server die or something else */
73 unsigned long yes = 1;
74 setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof (yes));
75 #endif
77 // Put data to structure sockaddr_in
78 // 1) Family of protocol
79 sockName.sin_family = AF_INET;
80 // 2) Number of listen port
81 sockName.sin_port = htons (DEFAULT_NET_PORT);
82 // 3) Set local ip address. You can connect in with whatever IP
83 sockName.sin_addr.s_addr = INADDR_ANY;
85 /* Bind port */
86 if (bind (sock, (struct sockaddr *) &sockName, sizeof (sockName)) == -1) {
87 printf ("> ERROR -> Port is used or danied");
88 return 0;
91 // Crete front (max 100 sessions in one time)
92 if (listen (sock, 100) == -1) {
93 printf ("> ERROR -> I can't create front");
94 return 0;
97 addrlen = sizeof (clientInfo);
99 return 1;
102 /** Network Loop function */
103 int net_loop ()
105 int newsock = accept (sock, (struct sockaddr *) &clientInfo, &addrlen);
107 /* new client is connected */
108 #ifdef __linux__
109 if (errno == EAGAIN && newsock != -1) {
110 #else
111 if (newsock > 0) {
112 #endif
113 client_new (newsock);
115 /* Set to nonblocking socket mode */
116 int oldFlag = fcntl (newsock, F_GETFL, 0);
117 if (fcntl (newsock, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
118 printf ("> ERROR -> Problem with set socket mode\n");
119 return 0;
122 printf ("-> New client was connected\n");
125 /* HACK: this is pretty needed for less cpu load */
126 usleep (60);
128 client_t *c;
129 for (c = client_list.next; c != &client_list; c = c->next) {
130 int ret = recv (c->sock, buf, 256, 0);
132 /* new data are available */
133 if (ret > 0) {
134 buf[ret] = '\0';
136 /* handle received data */
137 proto_handler (c, buf, ret);
140 /* client go out */
141 #ifdef __linux__
142 if (!ret) {
143 #else
144 if (ret == -1) {
145 #endif
146 close (c->sock);
148 client_quit (c);
150 printf ("-> Client was disconnected\n");
152 break;
156 return 1;