18 void Server_NewClient(int FD);
19 void HandleServerBoundData(tClient *Client);
20 void HandleClientBoundData(tClient *Client);
23 // --- Configuration ---
24 int giConfig_MaxClients = 5;
31 int main(int argc, char *argv[])
36 gaClients = calloc(giConfig_MaxClients, sizeof(tClient));
39 giServerFD = Net_OpenSocket(0, NULL, "tcps");
52 void FD_SET_MAX(fd_set *set, int fd, int *maxfd)
55 if(*maxfd < fd) *maxfd = fd;
62 FD_SET_MAX(&fds, giServerFD, &maxfd);
63 for( int i = 0; i < giConfig_MaxClients; i ++ )
65 FD_SET_MAX(&fds, gaClients[i].Socket, &maxfd);
66 FD_SET_MAX(&fds, gaClients[i].stdout, &maxfd);
70 select( maxfd+1, &fds, NULL, NULL, NULL );
73 if( FD_ISSET(giServerFD, &fds) )
75 Server_NewClient(giServerFD);
77 for( int i = 0; i < giConfig_MaxClients; i ++ )
79 if( FD_ISSET(gaClients[i].Socket, &fds) )
82 HandleServerBoundData(&gaClients[i]);
84 if( FD_ISSET(gaClients[i].stdout, &fds) )
86 // Handle output from terminal
87 HandleClientBoundData(&gaClients[i]);
93 void Server_NewClient(int FD)
97 // TODO: Is this done in the IPStack?
98 if( giNumClients == giConfig_MaxClients )
101 close( _SysOpenChild(FD, "", O_RDWR) );
105 // Allocate client structure and open socket
106 for( int i = 0; i < giConfig_MaxClients; i ++ )
108 if( gaClients[i].Socket == 0 ) {
113 // Accept the connection
114 clt->Socket = _SysOpenChild(FD, "", O_RDWR);
117 // Create stdin/stdout
118 clt->stdin = open("/Devices/fifo", O_RDWR);
119 clt->stdout = open("/Devices/fifo", O_RDWR);
121 // TODO: Arguments and envp
123 int fds[3] = {clt->stdin, clt->stdout, clt->stdout};
124 _SysSpawn("/Acess/SBin/login", NULL, NULL, 3, fds);
128 void HandleServerBoundData(tClient *Client)
133 len = read(Client->Socket, buf, BUFSIZ);
134 write(Client->stdin, buf, len);
137 void HandleClientBoundData(tClient *Client)
142 len = read(Client->stdout, buf, BUFSIZ);
143 write(Client->Socket, buf, len);