2 * Acess2 Networking Test Suite (NetTest)
3 * - By John Hodge (thePowersGang)
11 #include "tcpserver.h"
15 struct sNetTest_TCPServer
21 } Clients[MAX_CLIENTS];
25 tNetTest_TCPServer *NetTest_TCPServer_Create(int Port)
27 tNetTest_TCPServer *ret;
28 ret = calloc(sizeof(*ret), 1);
29 ret->ServerFD = VFS_Open("/Devices/ip/1/tcps", VFS_OPENFLAG_READ);
30 ASSERT(ret->ServerFD >= 0);
31 VFS_IOCtl(ret->ServerFD, 4, &Port);
35 void NetTest_TCPServer_Close(tNetTest_TCPServer *Srv)
37 VFS_Close(Srv->ServerFD);
38 for( int i = 0; i < Srv->nClients; i ++ )
40 struct sClient *client = &Srv->Clients[i];
41 VFS_Close(client->FD);
46 int NetTest_TCPServer_FillSelect(tNetTest_TCPServer *Srv, fd_set *fds)
48 ASSERT(Srv->ServerFD >= 0);
51 if( Srv->nClients == MAX_CLIENTS ) {
53 FD_SET(Srv->ServerFD, fds);
55 for( int i = 0; i < Srv->nClients; i ++ )
57 int fd = Srv->Clients[i].FD;
58 if( fd > max ) max = fd;
64 void NetTest_TCPServer_HandleSelect(tNetTest_TCPServer *Srv, const fd_set *rfds, const fd_set *wfds, const fd_set *efds)
66 if( FD_ISSET(Srv->ServerFD, rfds) )
69 ASSERT(Srv->nClients != MAX_CLIENTS);
70 struct sClient *client = &Srv->Clients[Srv->nClients++];
71 client->FD = VFS_OpenChild(Srv->ServerFD, "", VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE);
74 for( int i = 0; i < Srv->nClients; i ++ )
76 struct sClient *client = &Srv->Clients[i];
77 if( FD_ISSET(client->FD, rfds) )
79 // RX'd data on client
82 if( FD_ISSET(client->FD, efds) )
85 VFS_Close(client->FD);
86 memmove(client, client+1, (Srv->nClients-i-1) * sizeof(*client));
88 i --; // counteract i++