bool SendPacket(UDPPacket packet)
 {
     packet.LastSent = DateTime.Now;
     var bytes = packet.ToBytes();
     if (Sender.Send(bytes, bytes.Length, DestinationEP) == 0) {
         return false;
     } else {
         return true;
     }
 }
 public void AddPacket(UDPPacket message)
 {
     Sent[message.SequenceNo] = message;
 }
 /// <summary>
 /// Determine if a given UDP packet has been seen before (this is a resend)
 /// </summary>
 bool DuplicatePacket(UDPPacket p)
 {
     return Received.Contains(p.SequenceNo);
 }
 /// <summary>
 /// Notes that we have received this packet, keeping track via sequence number
 /// </summary>
 void RegisterPacket(UDPPacket p)
 {
     Received.Add(p.SequenceNo);
 }
 /// <summary>
 /// Adds a packet that has been received to the next ack that will be sent
 /// </summary>
 /// <param name="p"></param>
 void AddAck(UDPPacket p)
 {
     LastReceivedTicks = System.DateTime.Now.Ticks;
     if (p.SequenceNo < RemoteSeqNo - 32) {
         PrepareSpecialAck(p.SequenceNo);
     }
     // evil bit-field hacking. TODO: explain?
     if (p.SequenceNo > RemoteSeqNo) {
         _AckField <<= (int)(p.SequenceNo - RemoteSeqNo);
     }
     _AckField |= (UInt32)(1 << (int)(RemoteSeqNo - p.SequenceNo));
     if (p.SequenceNo >= RemoteSeqNo) {
         _RemoteSeqNo = p.SequenceNo;
     }
 }
 public void Send()
 {
     PonykartPacket TopPacket;
     try {
         TopPacket = Owner.TopMessage.ToPKPacket(Owner);
     } catch (Exception e) { return; }
     var message = new UDPPacket(TopPacket,this);
     if (!TopPacket.Volatile) {
         AddPacket(message);
     }
     SendPacket(message);
 }
 public void OnPacket(UDPPacket p)
 {
     if (p.Contents.Type != Commands.NoMessage && p.Contents.Type != Commands.SendPositions) {
         Launch.Log(String.Format("[Networking] Got packet of type {0}", p.Contents.Type));
     }
 }
 /// <summary>
 /// Called whenever we receive a packet.
 /// </summary>
 public void Handle(UDPPacket p)
 {
     AddAck(p);
     ProcessAcks(p.Ack, p.AckField);
     if (!DuplicatePacket(p)) {
         OnPacketRecv(p);
     }
 }
예제 #9
0
        /// <summary>
        /// Called every time we receive a new packet.
        /// </summary>
        public void OnPacket(byte[] packet)
        {
            // Launch.Log("Processing packet.");
            UDPPacket p = new UDPPacket(packet);
            //        Launch.Log(string.Format("Protocol string: {0}. Ours: {1}", System.Text.ASCIIEncoding.ASCII.GetString(p.Protocol),
            //                                                                   System.Text.ASCIIEncoding.ASCII.GetString(Protocol)));
            if (p.Protocol.SequenceEqual(Protocol)) {
               // Launch.Log("Packet of our protocol.");
                if (NetworkType == NetworkTypes.Client) {
                  //  Launch.Log("Received packet as client");
                    if (p.CID == SingleConnection.UDPConnection.ConnectionID) {
                        SingleConnection.UDPConnection.Handle(p);
                    }
                }
                else if (NetworkType == NetworkTypes.Host) {
                  //  Launch.Log("Received packet as host");
                    UInt32 id = p.CID;
                    if (Connections.ContainsKey(id)) {
                        Connections[id].UDPConnection.Handle(p);
                    } else if (Connections.Keys.Count < MaxConnections) {
                        Launch.Log("Allowing new connection");
                        Connections.Add(id, new Connection(Listener, new IPEndPoint(ListenEP.Address, ListenEP.Port), id));
                        Connections[id].UDPConnection.Handle(p);
                    }

                }

            }
        }