2 * Acess2 Networking Test Suite (NetTest)
3 * - By John Hodge (thePowersGang)
12 #include "tcpserver.h"
16 struct sNetTest_TCPServer
22 } Clients[MAX_CLIENTS];
26 tNetTest_TCPServer *NetTest_TCPServer_Create(int Port)
28 tNetTest_TCPServer *ret;
29 ret = calloc(sizeof(*ret), 1);
30 ret->ServerFD = VFS_Open("/Devices/ip/1/tcps", VFS_OPENFLAG_READ);
31 ASSERT(ret->ServerFD >= 0);
32 VFS_IOCtl(ret->ServerFD, 4, &Port);
36 void NetTest_TCPServer_Close(tNetTest_TCPServer *Srv)
38 VFS_Close(Srv->ServerFD);
39 for( int i = 0; i < Srv->nClients; i ++ )
41 struct sClient *client = &Srv->Clients[i];
42 VFS_Close(client->FD);
47 int NetTest_TCPServer_FillSelect(tNetTest_TCPServer *Srv, fd_set *fds)
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)
67 if( FD_ISSET(Srv->ServerFD, rfds) )
70 ASSERT(Srv->nClients != MAX_CLIENTS);
71 struct sClient *client = &Srv->Clients[Srv->nClients++];
73 client->FD = VFS_OpenChild(Srv->ServerFD, "", VFS_OPENFLAG_READ|VFS_OPENFLAG_WRITE);
74 LOG("client->FD = %i", client->FD);
76 if( FD_ISSET(Srv->ServerFD, efds) )
78 LOG("Oops, error on server");
79 VFS_Close(Srv->ServerFD);
83 for( int i = 0; i < Srv->nClients; i ++ )
85 struct sClient *client = &Srv->Clients[i];
86 if( FD_ISSET(client->FD, rfds) )
88 // RX'd data on client
89 // TODO: Cache until '.' is seen, and send all data before+incl that character
91 size_t len = VFS_Read(client->FD, sizeof(buf), buf);
92 Debug_HexDump("TCP Srv Rx", buf, len);
93 VFS_Write(client->FD, len, buf);
96 if( FD_ISSET(client->FD, efds) )
99 VFS_Close(client->FD);
100 memmove(client, client+1, (Srv->nClients-i-1) * sizeof(*client));
102 i --; // counteract i++