]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/extra/m_ziplink.cpp
MetaData rework
[user/henk/code/inspircd.git] / src / modules / extra / m_ziplink.cpp
index cb19157fa59af45b0c1c288f80bd3160500f33f0..efff1364458b026d3b5639fec27718e9d9ff0c6a 100644 (file)
@@ -2,8 +2,8 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
- * See: http://www.inspircd.org/wiki/index.php/Credits
+ *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
+ * See: http://wiki.inspircd.org/Credits
  *
  * This program is free but copyrighted software; see
  *            the file COPYING for details.
 
 #include "inspircd.h"
 #include <zlib.h>
-#include "users.h"
-#include "channels.h"
-#include "modules.h"
-#include "socket.h"
-#include "hashcomp.h"
 #include "transport.h"
-
 #include <iostream>
 
 /* $ModDesc: Provides zlib link support for servers */
@@ -35,9 +29,6 @@
 /* Status of a connection */
 enum izip_status { IZIP_CLOSED = 0, IZIP_OPEN };
 
-/* Maximum transfer size per read operation */
-const unsigned int CHUNK = 128 * 1024;
-
 /** Represents an zipped connections extra data
  */
 class izip_session : public classbase
@@ -60,16 +51,13 @@ class ModuleZLib : public Module
        float total_out_uncompressed;
        float total_in_uncompressed;
 
-       /* Used for reading data from the wire.
-        * We need a smaller buffer than CHUNK, else we could read more data
-        * from the wire than we can pass to the socket code.
-        */
-       char *read_buffer;
-       unsigned int read_buffer_size;
+       /* Used for reading data from the wire and compressing data to. */
+       char *net_buffer;
+       unsigned int net_buffer_size;
  public:
 
        ModuleZLib(InspIRCd* Me)
-               : Module::Module(Me)
+               : Module(Me)
        {
                ServerInstance->Modules->PublishInterface("BufferedSocketHook", this);
 
@@ -82,15 +70,16 @@ class ModuleZLib : public Module
                Implementation eventlist[] = { I_OnRawSocketConnect, I_OnRawSocketAccept, I_OnRawSocketClose, I_OnRawSocketRead, I_OnRawSocketWrite, I_OnStats, I_OnRequest };
                ServerInstance->Modules->Attach(eventlist, this, 7);
 
-               read_buffer_size = ServerInstance->Config->NetBufferSize / 4;
-               read_buffer = new char[read_buffer_size];
+               // Allocate a buffer which is used for reading and writing data
+               net_buffer_size = ServerInstance->Config->NetBufferSize;
+               net_buffer = new char[net_buffer_size];
        }
 
        virtual ~ModuleZLib()
        {
                ServerInstance->Modules->UnpublishInterface("BufferedSocketHook", this);
                delete[] sessions;
-               delete[] read_buffer;
+               delete[] net_buffer;
        }
 
        virtual Version GetVersion()
@@ -114,7 +103,7 @@ class ModuleZLib : public Module
                        const char* ret = "OK";
                        try
                        {
-                               ret = ServerInstance->Config->AddIOHook((Module*)this, (BufferedSocket*)ISR->Sock) ? "OK" : NULL;
+                               ret = ISR->Sock->AddIOHook((Module*)this) ? "OK" : NULL;
                        }
                        catch (ModuleException& e)
                        {
@@ -125,7 +114,7 @@ class ModuleZLib : public Module
                else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
                {
                        /* Detach from an inspsocket */
-                       return ServerInstance->Config->DelIOHook((BufferedSocket*)ISR->Sock) ? "OK" : NULL;
+                       return ISR->Sock->DelIOHook() ? "OK" : NULL;
                }
                else if (strcmp("IS_HSDONE", request->GetId()) == 0)
                {
@@ -176,9 +165,9 @@ class ModuleZLib : public Module
                        results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_compressed    = "+ConvToStr(total_in_compressed));
                        results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_uncompressed = "+ConvToStr(total_out_uncompressed));
                        results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_uncompressed  = "+ConvToStr(total_in_uncompressed));
-                       results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS outbound_ratio        = "+outbound_ratio);
-                       results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS inbound_ratio         = "+inbound_ratio);
-                       results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS combined_ratio        = "+combined_ratio);
+                       results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS percentage_of_original_outbound_traffic        = "+outbound_ratio);
+                       results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS percentage_of_orignal_inbound_traffic         = "+inbound_ratio);
+                       results.push_back(sn+" 304 "+user->nick+" :ZIPSTATS total_size_of_original_traffic        = "+combined_ratio);
                        return 0;
                }
 
@@ -192,9 +181,6 @@ class ModuleZLib : public Module
 
                izip_session* session = &sessions[fd];
 
-               /* allocate state and buffers */
-               session->status = IZIP_OPEN;
-
                /* Just in case... */
                session->outbuf.clear();
 
@@ -220,9 +206,12 @@ class ModuleZLib : public Module
                        session->status = IZIP_CLOSED;
                        return;
                }
+
+               /* Just in case, do this last */
+               session->status = IZIP_OPEN;
        }
 
-       virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
+       virtual void OnRawSocketAccept(int fd, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*)
        {
                /* Nothing special needs doing here compared to connect() */
                OnRawSocketConnect(fd);
@@ -241,19 +230,33 @@ class ModuleZLib : public Module
                if (session->status == IZIP_CLOSED)
                        return 0;
 
-               /* Read read_buffer_size bytes at a time to the buffer (usually 128k) */
-               readresult = read(fd, read_buffer, read_buffer_size);
+               if (session->inbuf.length())
+               {
+                       /* Our input buffer is filling up. This is *BAD*.
+                        * We can't return more data than fits into buffer
+                        * (count bytes), so we will generate another read
+                        * event on purpose by *NOT* reading from 'fd' at all
+                        * for now.
+                        */
+                       readresult = 0;
+               }
+               else
+               {
+                       /* Read read_buffer_size bytes at a time to the buffer (usually 2.5k) */
+                       readresult = read(fd, net_buffer, net_buffer_size);
 
-               /* Did we get anything? */
-               if (readresult <= 0)
-                       return 0;
+                       total_in_compressed += readresult;
 
-               total_in_compressed += readresult;
+                       /* Copy the compressed data into our input buffer */
+                       session->inbuf.append(net_buffer, readresult);
+               }
 
-               /* Copy the compressed data into out input buffer */
-               session->inbuf.append(read_buffer, readresult);
                size_t in_len = session->inbuf.length();
 
+               /* Do we have anything to do? */
+               if (in_len <= 0)
+                       return 0;
+
                /* Prepare decompression */
                session->d_stream.next_in = (Bytef *)session->inbuf.c_str();
                session->d_stream.avail_in = in_len;
@@ -331,7 +334,6 @@ class ModuleZLib : public Module
                        /* Seriously, wtf? */
                        return 0;
 
-               unsigned char compr[CHUNK];
                int ret;
 
                /* This loop is really only supposed to run once, but in case 'compr'
@@ -344,8 +346,8 @@ class ModuleZLib : public Module
                        session->c_stream.next_in = (Bytef*)buffer + offset;
                        session->c_stream.avail_in = count - offset;
 
-                       session->c_stream.next_out = compr;
-                       session->c_stream.avail_out = CHUNK;
+                       session->c_stream.next_out = (Bytef*)net_buffer;
+                       session->c_stream.avail_out = net_buffer_size;
 
                        /* Compress the text */
                        ret = deflate(&session->c_stream, Z_SYNC_FLUSH);
@@ -375,7 +377,7 @@ class ModuleZLib : public Module
                                return 0;
 
                        /* Space before - space after stuff was added to this */
-                       unsigned int compressed = CHUNK - session->c_stream.avail_out;
+                       unsigned int compressed = net_buffer_size - session->c_stream.avail_out;
                        unsigned int uncompressed = count - session->c_stream.avail_in;
 
                        /* Make it skip the data which was compressed already */
@@ -386,7 +388,7 @@ class ModuleZLib : public Module
                        total_out_compressed += compressed;
 
                        /* Add compressed to the output buffer */
-                       session->outbuf.append((const char*)compr, compressed);
+                       session->outbuf.append((const char*)net_buffer, compressed);
                } while (session->c_stream.avail_in != 0);
 
                /* Lets see how much we can send out */
@@ -402,7 +404,6 @@ class ModuleZLib : public Module
                        else
                        {
                                session->outbuf.clear();
-                               /* TODO pass errors down? */
                                return 0;
                        }
                }