IPStack - Bugfixing (and testing IRC client)
[tpg/acess2.git] / Usermode / Applications / irc_src / main.c
index 4fce928..b4892e7 100644 (file)
@@ -52,11 +52,17 @@ int main(int argc, const char *argv[], const char *envp[])
        }\r
        \r
        printf("Connection opened\n");\r
+       ProcessIncoming(&srv);\r
        \r
        writef(srv.FD, "USER %s %s %s : %s\n", gsUsername, gsHostname, gsRemoteAddress, gsRealName);\r
-       writef(srv.FD, "NICK %s", gsNickname);\r
+       writef(srv.FD, "NICK %s\n", gsNickname);\r
        \r
-       ProcessIncoming(&srv);\r
+       printf("Processing\n");\r
+       \r
+       for( ;; )\r
+       {\r
+               ProcessIncoming(&srv);\r
+       }\r
        \r
        close(srv.FD);\r
        return 0;\r
@@ -86,6 +92,8 @@ char *GetValue(char *Src, int *Ofs)
        char    *ret = Src + pos;\r
        char    *end;\r
        \r
+       if( !Src )      return NULL;\r
+       \r
        while( *ret == ' ' )    ret ++;\r
        \r
        end = strchr(ret, ' ');\r
@@ -104,7 +112,7 @@ char *GetValue(char *Src, int *Ofs)
  */\r
 void ParseServerLine(tServer *Server, char *Line)\r
 {\r
-        int    pos;\r
+        int    pos = 0;\r
        char    *ident, *cmd;\r
        if( *Line == ':' ) {\r
                // Message\r
@@ -143,10 +151,12 @@ void ProcessIncoming(tServer *Server)
        // process it line by line\r
        // ioctl#8 on a TCP client gets the number of bytes in the recieve buffer\r
        // - Used to avoid blocking\r
-       while( ioctl(Server->FD, 8, NULL) )\r
+       #if NON_BLOCK_READ\r
+       while( (len = ioctl(Server->FD, 8, NULL)) > 0 )\r
        {\r
+       #endif\r
                // Read data\r
-               len = read(Server->FD, BUFSIZ - Server->ReadPos, Server->InBuf + Server->ReadPos);\r
+               len = read(Server->FD, BUFSIZ - Server->ReadPos, &Server->InBuf[Server->ReadPos]);\r
                Server->InBuf[Server->ReadPos + len] = '\0';\r
                \r
                // Break into lines\r
@@ -161,13 +171,19 @@ void ProcessIncoming(tServer *Server)
                \r
                // Handle incomplete lines\r
                if( ptr - Server->InBuf < len + Server->ReadPos ) {\r
-                       Server->ReadPos = ptr - Server->InBuf;\r
+                       // Update the read position\r
+                       // InBuf ReadPos    ptr          ReadPos+len\r
+                       // | old | new used | new unused |\r
+                       Server->ReadPos = len + Server->ReadPos - (ptr - Server->InBuf);\r
+                       // Copy stuff back (moving "new unused" to the start of the buffer)\r
                        memcpy(Server->InBuf, ptr, Server->ReadPos);\r
                }\r
                else {\r
                        Server->ReadPos = 0;\r
                }\r
+       #if NON_BLOCK_READ\r
        }\r
+       #endif\r
 }\r
 \r
 /**\r

UCC git Repository :: git.ucc.asn.au