Example #1
0
 public void Send(Packet P)
 {
     string args = "";
     foreach(string S in P.Arguments)
     {
         args += S + "|";
     }
     args = args.Remove(args.Length-1);
     OnSend(P.Command + " - " + args);
 }
Example #2
0
        public Packet(byte[] Vals)
        {
            try
            {
                List<byte> Vals1 = new List<byte>();
                Vals1.AddRange(Vals);
                //int index = Vals1.IndexOf(0);
                /*for (int x = Vals1.Count-1; x > 0; x--)
                {
                    if (Vals1[x] == 0)
                        Vals1.RemoveAt(x);
                    else
                        break;
                }*/
                //if (index != -1)
                //    Vals1.RemoveRange(index, Vals1.Count - index);
                //byte[] all = this.ToBytes();
                string all = encoder.GetString(Vals1.ToArray());

                if (!all.StartsWith(PrefixS) || !all.EndsWith(EndS)) //invalid packet
                {

                }

                string[] Dirty = Regex.Split(all, EndS);
                for (int x = 0; x < Dirty.Length; x++)
                {
                    string Line = Dirty[x];
                    if (Line == "")
                        continue;
                    if (x == 0)
                    {
                        string[] args = Regex.Split(Line, SplitterS);

                        Bytes.Clear();

                        foreach (string Arg in args)
                        {
                            //if (Arg == "")
                            //    continue;
                            Bytes.Add(encoder.GetBytes(Arg.Replace(PrefixS, "")));
                        }
                        if (Encrypted)
                        {
                            Decrypt();
                        }
                        else
                        {
                            throw new Exception("Received a non-encrypted packet.");
                        }
                        if (Bytes[Bytes.Count - 1].Length == 0)
                            Bytes.RemoveAt(Bytes.Count - 1);
                    }
                    else
                    {
                        Packet Extra = new Packet(encoder.GetBytes(Line));

                        string[] args = Regex.Split(Line, SplitterS);

                        //Bytes.Clear();
                        foreach (string Arg in args)
                        {
                            if (Arg == "")
                                continue;
                            Bytes.Add(encoder.GetBytes(Arg.Replace(PrefixS, "")));
                        }
                    }
                }
            }
            catch(Exception E)
            {

            }
        }
Example #3
0
 void C_OnRecv(object sender, Packet P)
 {
     Cmd.Process(sender, P.Command, P);
 }
Example #4
0
        void WS_ReceivedEvent(object sender, Packet P)
        {
            if (OnRecv == null)
                return;
               // CS.SysLog("Received Packet " + P.Command + " on thread " + Thread.CurrentThread.ManagedThreadId);
            if (Automate) //if automation is enabled and an internal packet is detected
            {//it will not raise the event on the external program
                if (Cmd.Process(sender, P.Command, P.Arguments))
                    return;
            }

            OnRecv(sender, P);
        }
Example #5
0
 public void Send(Packet Packet)
 {
     try
     {
         WS.Send(Packet);
     }
     catch(Exception E)
     {
         CS.SysLog("Exception during Sending: " + E.Message);
     }
 }
Example #6
0
        //final send
        private void Send(byte[] Bytes)
        {
            Stats.packetssent++;

            Packet P = new Packet(Bytes);
            try
            {
                if (Settings.Protocol == Wo1fSocketProtocol.UDP)
                {
                    //Udp.Connect(_IP,_Port);
                    Udp.Client.Send(Bytes);

                }
                else if (Settings.Protocol == Wo1fSocketProtocol.TCP)
                {
                    Tcp.Client.Send(Bytes);
                    // CS.SysLog("Sending packet " + P.Command);

                }
                if (CS.PLogger != null)
                    CS.PLogger.Send(P);
                OnPacketSend(this, P);
            }
            catch(Exception E)
            {
                Stats.LostPackets++;
                if (Stats.LostPackets >= 10)
                {
                    OnDC(this);
                }
            }
        }
Example #7
0
        private void BeginRecvCallback(IAsyncResult Res)
        {
            //CS.SysLog("BeginRecvCallback();");
            if (Res.IsCompleted)
            {

               // CS.SysLog("Completed Callback");
                try
                {
                    Tcp.Client.EndReceive(Res);
                    Packet P = new Packet(RecvBuffer);

                    if (OnRecv != null)
                        OnRecv(this, P);
                    Stats.LastPacketRecv = DateTime.Now;
                    Stats.packetsrecv++;
                    if (P.ExtraPacket)
                        Stats.packetsrecv += P.Extras.Count;
                    BeginReceive();
                }
                catch
                {
                    OnDC(this);
                }

            }
        }
Example #8
0
        private void AsyncSend(Packet P)
        {
            if (asyncbusy)
            {
                SQueue.Add(P);
                return;
            }

            if (SQueue.Count > 0)
            {
                int x = 0;
                P.ExtraPacket = true;
                while (x < 10) //x packets at a time or until full(todo: add code to check if full)
                {
                    if (SQueue.Count == 0)
                        break;
                    P.Extras.Add(SQueue[0]);
                    SQueue.RemoveAt(0);
                    x++;
                }
                CS.SysLog("Send queue backed up.  Sending " + P.Extras.Count + " extra packets.");
            }
            asyncbusy = true;
            byte[] info = P.ToBytes();
            try
            {
                Tcp.Client.BeginSend(info, 0, info.Length, SocketFlags.None, new AsyncCallback(BeginSendCallback), this);
            }
            catch(Exception E)
            {
                //OnSendFail

                CS.SysLog("Packet sending failed");
            }
            asyncbusy = false;
            this.Stats.packetssent++;
            if (P.ExtraPacket)
                this.Stats.packetssent += P.Extras.Count;
        }
Example #9
0
 protected virtual void OnPacketSend(object sender, Packet P)
 {
     if (OnSend != null)
         OnSend(sender, P);
 }
Example #10
0
        public void Send(Packet P)
        {
            if(Encryption)
                P.Encrypt();

            switch (Settings.ThreadMode)
            {
                case Wo1fSocketThreadMode.Async:
                    AsyncSend(P);
                    break;
                case Wo1fSocketThreadMode.SingleThreaded:
                    Send(P.ToBytes());
                    break;
                case Wo1fSocketThreadMode.MultiThreadedEach: //internal tick
                    SQueue.Add(P);
                    break;
                case Wo1fSocketThreadMode.MultiThreadCount: //wait for external tick
                    SQueue.Add(P);
                    break;
            }
        }
Example #11
0
 void C_OnRecv(object sender, Packet P)
 {
     if (!Cmd.Process(sender, P.Command.ToLower(), P))
     {
         CS.SysLog("Unhandled Cmd: " + P.Command.Trim());
     }
 }
 public void SendToAll(Packet P)
 {
     foreach (Client C in Clients)
     {
         C.Send(P);
     }
 }
Example #13
0
 static void WS_OnSend(object sender, Packet P)
 {
     throw new NotImplementedException();
 }