public void run()
        {
            // output needs to be the size of the data, remove excess 00 00?
            //byte[] buffer = new byte[2000]; // create a large buffer, should be able to hold all traffic
            ArrayList <Byte> aBuffer = new ArrayList <Byte>();
            ArrayList <Byte> aOutput = new ArrayList <Byte>();

            // create a large buffer, should be able to hold all traffic
            // byte decompressBuffer[] = new byte[2000];
            while (run)
            {
                System.Threading.Thread.Sleep(5);
                if (!client.isConnected())
                {
                    writeoutput("Disconnected");
                    packetOperator.processDisconnect();
                }

                try
                {
                    int size = myin.available();
                    if (size != 0)
                    {
                        byte[] incoming = new byte[myin.available()];
                        myin.read(incoming);
                        for (int i = 0; i < incoming.length; i++)
                        {
                            aBuffer.add(incoming[i]);
                        }         // fills our vecotor with the packet
                    }
                    while (!aBuffer.isEmpty())
                    {
                        if (bDecompress)
                        {
                            byte[] tempbuf = new byte[aBuffer.size()];             // create a buffer to hold list
                            for (int x = 0; x < aBuffer.size(); x++)
                            {
                                tempbuf[x] = aBuffer.get(x);
                            }                                                                                   // adds list to buffer
                            if (aBuffer.size() > 1000)
                            {
                                int zx = 55 + 5;
                            }
                            huffmanobject myhuf = new huffmanobject();
                            myhuf.buffer   = tempbuf;
                            myhuf.src_size = tempbuf.length;
                            myhuf.out_size = 0;
                            myhuf          = BinaryNode.drkDecompress(myhuf);
                            byte[] temp = myhuf.output;
                            if (temp.length < 1)
                            {
                                aBuffer.clear(); break;
                            }                                                           // if we are getting garbage wipe the buffer.
                            if (tempbuf.length > temp.length)
                            {
                                writeoutput("Packet: " + aBuffer.size() + "bytes consumed: " + myhuf.out_size);
                                int intx = 1;
                                for (int x = 0; x < temp.length; x++)
                                {
                                    aOutput.add(temp[x]);
                                }                                                                          // outputs the packet
                                for (int x = 0; x < myhuf.out_size + intx; x++)
                                {
                                    aBuffer.remove(0);
                                }                                                                                 // removes it from queue
                            }
                            else
                            {
                                for (int x = 0; x < temp.length; x++)
                                {
                                    aOutput.add(temp[x]);
                                }                                                                          // outputs the packet
                                if (aBuffer.isEmpty() == false)
                                {
                                    aBuffer.clear();
                                }
                            }
                        }

                        else
                        {
                            byte[] temp = new byte[aBuffer.size()];             // create a buffer to hold list
                            for (int x = 0; x < aBuffer.size(); x++)
                            {
                                temp[x] = aBuffer.get(x);
                            }                                                                                // adds list to buffer
                            if (aBuffer.isEmpty() == false)
                            {
                                aBuffer.clear();
                            }
                            for (int x = 0; x < temp.length; x++)
                            {
                                aOutput.add(temp[x]);
                            }
                        }
                        byte[] temp = new byte[aOutput.size()];
                        for (int x = 0; x < aOutput.size(); x++)
                        {
                            temp[x] = aOutput.get(x);
                        }
                        handlePacket(temp);
                        aOutput.clear();
                    }
                }        // end  of newdata IF

                // end of try
                catch (SocketException e)
                {
                    writeoutput("Socket Error: Most likely disconnect: " + e);
                    try { client.close(); } catch (IOException e2) {}
                    packetOperator.processDisconnect();
                }
                catch (IOException e)
                {
                    writeoutput("IOError: " + e);
                }
                catch (NegativeArraySizeException e)
                {
                }
                catch (Exception e)
                {
                    writeoutput("Some other error in the thread: " + e);
                    e.printStackTrace();
                }
            }
        }
Exemple #2
0
        public void PacketReceived(IAsyncResult result)
        {
            mysocket.EndReceive(result);
            StateObject newstate        = (StateObject)result.AsyncState;
            ArrayList   incomingpackets = new ArrayList();

            incomingpackets.AddRange(newstate.buffer);
            byte[] outbuffer;
            // store incoming packet in an arraylist
            // decompress it etc, then xfer to byte[] and remove from list
            // repeat
            while (incomingpackets.Count > 0)
            {
                if (bDecompress)
                {
                    HuffmanDecompression huff = new HuffmanDecompression();

                    outbuffer = new byte[incomingpackets.Count];
                    huffmanobject myhuf = new huffmanobject();
                    myhuf.buffer   = (byte[])incomingpackets.ToArray(typeof(byte));
                    myhuf.src_size = incomingpackets.Count;
                    myhuf.out_size = 0;

                    //myhuf.output = BinaryNode.Decompress(myhuf.buffer);
                    myhuf     = BinaryNode.drkDecompress(myhuf);
                    outbuffer = myhuf.output;
                    //myhuf.out_size = outbuffer.Length;

                    if (outbuffer.Length < 1)
                    {
                        incomingpackets.Clear(); break;
                    }
                    if (myhuf.buffer.Length > outbuffer.Length)
                    {
                        if (bDebug)
                        {
                            display("consuming: " + myhuf.out_size + " bytes");
                        }
                        for (int x = 0; x < (myhuf.out_size + 1); x++)
                        {
                            incomingpackets.RemoveAt(0);
                        }                                                                               // removes it from queue
                    }
                    else
                    {
                        incomingpackets.Clear();
                    }
                }
                else
                {
                    // Wipe the buffer as pre decompression packets dont stack up.
                    outbuffer = newstate.buffer;
                    incomingpackets.Clear();
                }

                if (bTwofishcrypt)
                {
                    Twofish.serverDecrypt(ref outbuffer, outbuffer.Length);
                    display("Decrypting incoming");
                }

                handlePackets(outbuffer);
            }
        }