Example #1
0
        public void Send(BasePacket packet, PacketResponseEvent responseEvent = null)
        {
            string Json = JsonConvert.SerializeObject(packet);

            System.Console.WriteLine("Sent packet " + packet.ID + " (reply to " + packet.RequestID + ").");

            try
            {
                byte[] Buffer       = System.Text.Encoding.UTF8.GetBytes(Json);
                byte[] LengthBuffer = BitConverter.GetBytes(Buffer.Length);
                m_socket.Send(LengthBuffer);
                m_socket.Send(Buffer);
            }
            catch (SocketException)
            {
                return;
            }

            if (responseEvent != null)
            {
                QueuedPacketResponseEvent evt = new QueuedPacketResponseEvent();
                evt.RequestID = packet.ID;
                evt.Event     = responseEvent;
                m_queuedResponseEvents.Add(evt);
            }

            //System.Console.Write("Sent:\n" + Json + "\n\n");
        }
Example #2
0
        void Recieve(byte[] Buffer)
        {
            string Json = System.Text.Encoding.UTF8.GetString(Buffer);
            // System.Console.Write("Recv:\n" + Json + "\n\n");

            BasePacket Base       = JsonConvert.DeserializeObject <BasePacket>(Json);
            Type       PacketType = Type.GetType("GameMonitor.Connection." + Base.ClassName);
            BasePacket Full       = JsonConvert.DeserializeObject(Json, PacketType) as BasePacket;

            if (Full != null)
            {
                System.Console.WriteLine("Recieved packet " + Full.ID + " (reply to " + Full.RequestID + ").");

                Full.Process(this);

                // Any async events awaiting?
                foreach (QueuedPacketResponseEvent evt in m_queuedResponseEvents)
                {
                    if (evt.RequestID == Full.RequestID)
                    {
                        System.Console.WriteLine("Invoking async event for packet retrieval.");

                        evt.Event(this, Full);
                        m_queuedResponseEvents.Remove(evt);
                        break;
                    }
                }
            }
        }