[Release] PacketThreader

04/30/2010 00:24 t_dubble_uu#1
This is a Theaded Packet Queue'er for the coemu source. You will need to modify it a little bit if you have another source.

First create a new file and name it PacketThreading.cs

Copy and paste this code
Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using WorldServer.Networking; //EDIT change this to the folder your ClientSocket.cs is in

namespace WorldServer.Packets //EDIT change this to namespace of your project
{
    public class PacketThreading
    {
        protected static int Counter = 0;
        private static Hashtable Data = new Hashtable();
        private static Hashtable Data1 = new Hashtable();

        private static byte t1 = 0, t2 = 0, t = 0;
        public static void TurnOn()
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(Thread1_Execute));
            ThreadPool.QueueUserWorkItem(new WaitCallback(Thread2_Execute));
        }
        public static void Thread1_Execute(object state)
        {
            while (true)
            {
                foreach (PacketData packet in Data.Values)
                {
                   TryExecute(packet.data, packet.CSocket);
                }
                Data.Clear();
                t1 = 0;
                Thread.Sleep(10);
            }
        }
        public static void Thread2_Execute(object state)
        {
            while (true)
            {
                foreach (PacketData packet in Data1.Values)
                {
                    TryExecute(packet.data, packet.CSocket);
                }
                Data1.Clear();
                t2 = 0;
                Thread.Sleep(10);
            }
        }
        private static void TryExecute(byte[] data, ClientSocket CSocket)
        {
            try
            {
                PacketProcessor.ProcessPacket(data, CSocket);
            }
            catch (Exception e) { Program.WriteLine(e.ToString()); }          
        }
        public static void Add(PacketData Packet)
        {
            int Count = Counter + 1;
            switch (t)
            {
                case 0:
                {
                    Data.Add(Count, Packet);
                    Counter = Count;
                    t = 0;
                }
                break;
                case 1:
                {
                    Data1.Add(Count, Packet);
                    Counter = Count;
                    t = 1;
                }
                break;
            }
        }
    }
    public class PacketData
    {
        protected byte[] _data;
        public byte[] data
        {
            get { return _data; }
            set { _data = value; }
        }

        protected ClientSocket _CSocket;
        public ClientSocket CSocket
        {
            get { return _CSocket; }
            set { _CSocket = value; }
        }

        public void Execute()
        {
            PacketThreading.Add(this);
        }
    }
}
Then in ClientSocket.cs preplace this:
Code:
PacketProcessor.ProcessPacket(Data, this);
with:
Code:
PacketData P = new PacketData();
P.data = Data;
P.CSocket = this;
P.Execute();
And somewhere where your server is initializing add this:
Code:
PacketThreading.TurnOn();
04/30/2010 00:32 Arcо#2
Very very impressive.
Keep up the good work.
04/30/2010 00:37 t_dubble_uu#3
Quote:
Originally Posted by .Arco View Post
Very very impressive.
Keep up the good work.
Thank you.

This can also be used for many other things that you need to pass data/vairables into a running thread which can get complicated.

Doing it this way will queue each packet in order they are recieved instead of craming each of the hundreds of packets sent with a high player load, down the packetprocessors throat.

Inturn adding to the performance of your server
04/30/2010 02:02 pintser#4
Nice!:)

Some Usefull Release :)
05/01/2010 11:46 Korvacs#5
A good start, however you need to be careful when using this, since your now performing the same tasks on more than one thread you need to make sure that you have the correct locks in place to avoid conflicts across the threads.

Personally i use a thread-pool, which goes one step further than this and creates new threads when they are required, you should look into it :)
05/01/2010 18:50 Basser#6
Quote:
Originally Posted by Korvacs View Post
A good start, however you need to be careful when using this, since your now performing the same tasks on more than one thread you need to make sure that you have the correct locks in place to avoid conflicts across the threads.

Personally i use a thread-pool, which goes one step further than this and creates new threads when they are required, you should look into it :)
A thread pool would be better indeed, however it also more difficult to create one.
05/01/2010 22:54 -Spirits-#7
Aww... the icing off my oreo slipped off =[
Oh, right- Good job =P