Register for your free account! | Forgot your password?

You last visited: Today at 00:26

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Release] PacketThreader

Discussion on [Release] PacketThreader within the CO2 PServer Guides & Releases forum part of the CO2 Private Server category.

Reply
 
Old   #1
 
t_dubble_uu's Avatar
 
elite*gold: 0
Join Date: Jan 2009
Posts: 267
Received Thanks: 181
[Release] PacketThreader

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();
t_dubble_uu is offline  
Thanks
5 Users
Old   #2
 
Arcо's Avatar
 
elite*gold: 0
Join Date: Oct 2009
Posts: 8,784
Received Thanks: 5,304
Very very impressive.
Keep up the good work.
Arcо is offline  
Old   #3
 
t_dubble_uu's Avatar
 
elite*gold: 0
Join Date: Jan 2009
Posts: 267
Received Thanks: 181
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
t_dubble_uu is offline  
Old   #4
 
elite*gold: 0
Join Date: May 2006
Posts: 297
Received Thanks: 58
Nice!

Some Usefull Release
pintser is offline  
Old   #5


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,126
Received Thanks: 2,518
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
Korvacs is offline  
Old   #6
 
elite*gold: 0
Join Date: Sep 2008
Posts: 1,683
Received Thanks: 506
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.
Basser is offline  
Old   #7
 
elite*gold: 0
Join Date: Mar 2010
Posts: 126
Received Thanks: 14
Aww... the icing off my oreo slipped off =[
Oh, right- Good job =P
-Spirits- is offline  
Thanks
1 User
Reply




All times are GMT +2. The time now is 00:26.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.