Example #1
0
        public static Packet Decode(byte[] message, int length)
        {
            Packet lp = new Packet();
            if (length == 0) return lp;
            try
            {
                lp.From = BitConverter.ToInt32(message, (int)PacketOffset.source);
                lp.To = BitConverter.ToInt32(message, (int)PacketOffset.dest);
                lp.Intention = (MessageTypes)BitConverter.ToInt32(message, (int)PacketOffset.type);
            }
            catch (Exception) { lp.Intention = MessageTypes.UNKNOWN_MESSAGE; return lp; }
            ASCIIEncoding encoder = new ASCIIEncoding();
            lp.Data = encoder.GetString(message, (int)PacketOffset.message, length - (int)PacketOffset.message);
            return lp;

        }
Example #2
0
        void WaitSocket()
        {
            try
            {
                if (packetWorker == null)
                {
                    packetWorker = new AsyncCallback(OnData);
                }
                Packet lp = new Packet();

                // Start listening to the data asynchronously
                socket.BeginReceive(lp.EncodedData,
                                                        0, lp.EncodedData.Length,
                                                        SocketFlags.None,
                                                        packetWorker,
                                                        lp);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debugger.Log(0, "1", e.Message);
            }

        }
Example #3
0
        void WaitSocket(int numclient)
        {
            try
            {
                if (packetWorker == null)
                {
                    // Specify the call back function which is to be 
                    // invoked when there is any write activity by the 
                    // connected client
                    packetWorker = new AsyncCallback(OnData);
                }
                Packet lp = new Packet(numclient);
                Socket soc = client[numclient];

                soc.BeginReceive(lp.EncodedData, 0,
                    lp.EncodedData.Length,
                    SocketFlags.None,
                    packetWorker,
                    lp);
            }
            catch (SocketException se)
            {
                System.Diagnostics.Debugger.Log(0, "1", se.Message);
            }
        }
Example #4
0
 public void SendPacket(Packet lp)
 {
     Socket sock = client[lp.To];
     if ((sock == null) || !sock.Connected) return;
     sock.Send(Packet.Encode(lp));
 }
Example #5
0
 public void SendPacket(Packet lp)
 {
     if ((socket == null) || !socket.Connected) return;
     try
     {
         socket.Send(Packet.Encode(lp),lp.EncodedData.Length, SocketFlags.None);
     }
     catch (Exception e)
     {
         System.Diagnostics.Debugger.Log(0, "1", e.Message);
     }
 }
Example #6
0
 public static byte[] Encode(Packet lp)
 {
     return Encode(lp.From, lp.To, lp.Intention, lp.Data);
 }
Example #7
0
 public static Packet Decode(Packet lp, int length)
 {
     Packet np = Decode(lp.EncodedData, length);
     return np;
 }
Example #8
0
 public virtual void OnPacket(Packet lp) { }