Example #1
0
 /// <summary>
 /// Publishes a received packet to all listeners.
 /// </summary>
 /// <param name="opcode">The opcode of the packet.</param>
 /// <param name="data">The payload of the packet.</param>
 private void OnPacketReceived(short opcode, byte[] data)
 {
     if (PacketReceived != null)
     {
         var invocationList = PacketReceived.GetInvocationList();
         foreach (var del in invocationList)
         {
             try
             {
                 ((DataReceivedHandler)del)(opcode, data);
             }
             catch (Exception ex)
             {
                 log.Error("Unhandled Exception in delegate", ex);
             }
         }
     }
 }
Example #2
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            if (NetworkStream != null)
            {
                try {
                    int iBytesRead = NetworkStream.EndRead(ar);
                    iBytesRead = ServeCrossDomainPolicy(iBytesRead);

                    if (iBytesRead > 0)
                    {
                        // Create or resize our packet stream to hold the new data.
                        if (PacketStream == null)
                        {
                            PacketStream = new byte[iBytesRead];
                        }
                        else
                        {
                            Array.Resize(ref PacketStream, PacketStream.Length + iBytesRead);
                        }

                        Array.Copy(ReceivedBuffer, 0, PacketStream, PacketStream.Length - iBytesRead, iBytesRead);

                        UInt32 packetSize = Packet.DecodePacketSize(PacketStream);

                        while (this.PacketStream != null && PacketStream.Length >= packetSize && PacketStream.Length > Packet.PacketHeaderSize)
                        {
                            // Copy the complete packet from the beginning of the stream.
                            var completePacket = new byte[packetSize];
                            Array.Copy(PacketStream, completePacket, packetSize);

                            var deserializedPacket = new Packet(completePacket);
                            SequenceNumber = Math.Max(SequenceNumber, deserializedPacket.SequenceNumber);

                            LastPacketReceived = deserializedPacket;

                            // Dispatch the completed packet.
                            if (PacketReceived != null)
                            {
                                FrostbiteConnection.RaiseEvent(PacketReceived.GetInvocationList(), this, deserializedPacket);
                            }
                            //this.DispatchPacket(cpCompletePacket);

                            // Now remove the completed packet from the beginning of the stream
                            var updatedSteam = new byte[PacketStream.Length - packetSize];
                            Array.Copy(PacketStream, packetSize, updatedSteam, 0, PacketStream.Length - packetSize);
                            PacketStream = updatedSteam;

                            packetSize = Packet.DecodePacketSize(PacketStream);
                        }

                        // If we've recieved 16 kb's and still don't have a full command then shutdown the connection.
                        if (ReceivedBuffer.Length >= MaxGarbageBytes)
                        {
                            ReceivedBuffer = null;
                            Shutdown();
                        }

                        if (this.NetworkStream != null)
                        {
                            this.NetworkStream.BeginRead(this.ReceivedBuffer, 0, this.ReceivedBuffer.Length, this.ReceiveCallback, this);
                        }
                    }
                    else
                    {
                        Shutdown();
                    }
                }
                catch (Exception) {
                    Shutdown();
                }
            }
            else
            {
                Shutdown();
            }
        }