X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2Firc_src%2Fmain.c;h=b4892e7975f665209d0b07ef8b9ced834a72f4a2;hb=4dcdedff0ff1494189773c2d724f76a0291ca155;hp=30c579c05890c1eba1e3d07fd30cee0298460b78;hpb=41769c02317835472d7678d3531ecfc23df8e17a;p=tpg%2Facess2.git diff --git a/Usermode/Applications/irc_src/main.c b/Usermode/Applications/irc_src/main.c index 30c579c0..b4892e79 100644 --- a/Usermode/Applications/irc_src/main.c +++ b/Usermode/Applications/irc_src/main.c @@ -38,10 +38,13 @@ int main(int argc, const char *argv[], const char *envp[]) memset(&srv, 0, sizeof(srv)); + // Parse Command line + // - Sets the server configuration globals if( (tmp = ParseArguments(argc, argv)) ) { return tmp; } + // Connect to the remove server srv.FD = OpenTCP( gsRemoteAddress, giRemotePort ); if( srv.FD == -1 ) { fprintf(stderr, "Unable to create socket\n"); @@ -49,11 +52,17 @@ int main(int argc, const char *argv[], const char *envp[]) } printf("Connection opened\n"); + ProcessIncoming(&srv); writef(srv.FD, "USER %s %s %s : %s\n", gsUsername, gsHostname, gsRemoteAddress, gsRealName); - writef(srv.FD, "NICK %s", gsNickname); + writef(srv.FD, "NICK %s\n", gsNickname); - ProcessIncoming(&srv); + printf("Processing\n"); + + for( ;; ) + { + ProcessIncoming(&srv); + } close(srv.FD); return 0; @@ -83,6 +92,8 @@ char *GetValue(char *Src, int *Ofs) char *ret = Src + pos; char *end; + if( !Src ) return NULL; + while( *ret == ' ' ) ret ++; end = strchr(ret, ' '); @@ -101,7 +112,7 @@ char *GetValue(char *Src, int *Ofs) */ void ParseServerLine(tServer *Server, char *Line) { - int pos; + int pos = 0; char *ident, *cmd; if( *Line == ':' ) { // Message @@ -140,10 +151,12 @@ void ProcessIncoming(tServer *Server) // process it line by line // ioctl#8 on a TCP client gets the number of bytes in the recieve buffer // - Used to avoid blocking - while( ioctl(Server->FD, 8, NULL) ) + #if NON_BLOCK_READ + while( (len = ioctl(Server->FD, 8, NULL)) > 0 ) { + #endif // Read data - len = read(Server->FD, BUFSIZ - Server->ReadPos, Server->InBuf + Server->ReadPos); + len = read(Server->FD, BUFSIZ - Server->ReadPos, &Server->InBuf[Server->ReadPos]); Server->InBuf[Server->ReadPos + len] = '\0'; // Break into lines @@ -158,13 +171,19 @@ void ProcessIncoming(tServer *Server) // Handle incomplete lines if( ptr - Server->InBuf < len + Server->ReadPos ) { - Server->ReadPos = ptr - Server->InBuf; + // Update the read position + // InBuf ReadPos ptr ReadPos+len + // | old | new used | new unused | + Server->ReadPos = len + Server->ReadPos - (ptr - Server->InBuf); + // Copy stuff back (moving "new unused" to the start of the buffer) memcpy(Server->InBuf, ptr, Server->ReadPos); } else { Server->ReadPos = 0; } + #if NON_BLOCK_READ } + #endif } /** @@ -232,12 +251,12 @@ int OpenTCP(const char *AddressString, short PortNumber) } // Set remote port and address - printf("Setting port and remote address"); + printf("Setting port and remote address\n"); ioctl(fd, 5, &PortNumber); ioctl(fd, 6, addrBuffer); // Connect - printf("Initiating connection"); + printf("Initiating connection\n"); if( ioctl(fd, 7, NULL) == 0 ) { // Shouldn't happen :( fprintf(stderr, "Unable to start connection\n");