private void ProcessIncomingData(object obj) { try { lock (m_ClientLock) { UDPReceiveEvent Packet = (UDPReceiveEvent)obj; FireReceiveMessage(ref Packet); #if DEBUG Log.LogMessage("UDPServer : Requête entrante de " + (Packet.Client.Address.ToString() + " : " + Outils.HexToString(Packet.Data, Packet.Data.Length)), Color.Blue, 2); #endif } } catch (SocketException Ex) { Log.LogMessage("UDPServer : Problème Socket : " + Ex.Message, Color.DarkBlue, 0); } }
protected virtual void FireReceiveMessage(ref UDPReceiveEvent e) { OnReceiveMessage?.Invoke(this, e); }
/// <summary> /// Réception d'un datagramme entrant sur le socket udp /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnReceiveMessage(object sender, UDPReceiveEvent evt) { // Si l'adresse du peer est nulle cela signifie que le wazzhup update a détecté un PEER local if (m_EP == null) { // Si le peer à une addresse locale qui correspond // Il s'agit d'un client sur le même réseau local // On prend alors cette IP locale comme référence if (m_InternalIP.Contains(evt.Client.Address)) { m_EP = new IPEndPoint(evt.Client.Address, m_Port); } else { return; } } else if ((!evt.Client.Address.Equals(m_EP.Address)) || (evt.Client.Port != m_Port)) { return; } BinaryReader reader = new BinaryReader(new MemoryStream(evt.Data)); try { switch ((Protocol)reader.ReadByte()) { case Protocol.PING: { SendPong(evt.Time); break; } case Protocol.PONG: { m_Latence = (evt.Time - m_LastPing) / 2; m_Counter = 0; long TimePong = reader.ReadInt64(); if (m_MiniPing > m_Latence) { m_Decalage = evt.Time - m_Latence - TimePong; m_MiniPing = m_Latence; } if (!m_OnLine) { #if DEBUG Log.LogMessage("Peer [" + CallSign + "] Passage en état OnLine", Color.DarkBlue, 1); #endif m_OnLine = true; m_Distance = -1; m_Counter_In = 0; m_Counter_Out = 0; } if (m_Version == 0) { RequestVersion(); } P2P.UpdateListItem(m_CallSign); break; } case Protocol.CHAT: { string Message = reader.ReadString(); P2P.AddLineChat(CallSign, Message); m_SC.SendScrollingText(CallSign + " : " + Message); break; } case Protocol.DATA: { if (m_Version == PROTO_VERSION) { if (m_bBlockData) { return; } m_Mutex.WaitOne(); try { byte CounterIn = reader.ReadByte(); // Si le compteur reçu est supérieur nous avons une donnée récente if (((CounterIn - m_Counter_In) > 0) || (m_Counter_In == 255)) { m_OldData.Clone(m_Data); int Len = reader.ReadInt32(); reader.BaseStream.Seek(2, 0); m_Data = (AirData)Serializer.Deserialize <AirData>(reader.BaseStream); m_Data.TimeStamp += m_Decalage; if (m_Data.TimeStamp <= m_OldData.TimeStamp) { #if DEBUG Log.LogMessage("Peer[" + CallSign + "] Donées en retard ignorées", Color.DarkBlue, 1); #endif return; } if ((m_Spawned >= 4) && (m_Spawned < 5)) { m_Spawned++; } m_RefreshRate = evt.Time - m_LastData; m_LastData = evt.Time; m_RemoteRefreshRate = m_Data.TimeStamp - m_OldData.TimeStamp; m_Distance = Outils.distance(m_Data.Latitude, m_Data.Longitude, m_SendData.Latitude, m_SendData.Longitude, 'N'); #if DEBUG if ((CounterIn - m_Counter_In) > 1) { Log.LogMessage("Peer [" + CallSign + "] Paquets Udp Manquants =" + (CounterIn - m_Counter_In - 1).ToString(), Color.DarkViolet, 1); } #endif if (m_Spawned >= 5) { RefreshData(); } } #if DEBUG else { Log.LogMessage("Peer [" + CallSign + "] Paquets Udp ignoré en retard de " + (m_Counter_In - CounterIn).ToString(), Color.DarkViolet, 1); } #endif m_Counter_In = CounterIn; } finally { m_Mutex.ReleaseMutex(); } } break; } case Protocol.VERSION: { m_Version = reader.ReadByte(); if (m_Version == PROTO_VERSION) { m_Data.Title = reader.ReadString(); if (m_Spawned >= 3) { Spawn_AI(false); } m_Data.Type = reader.ReadString(); m_Data.Model = reader.ReadString(); m_Data.Category = reader.ReadString(); } P2P.UpdateListItem(m_CallSign); #if DEBUG Log.LogMessage("Peer [" + CallSign + "] reçu numéro de version = " + m_Version.ToString(), Color.DarkBlue, 1); #endif break; } case Protocol.REQ_VERSION: { SendVersion(); break; } } } catch (Exception e) { Log.LogMessage("Peer [" + CallSign + "] Erreur d'analyse du datagramme : " + e.Message, Color.DarkViolet, 0); } }