private void EndReceiveCallback(System.IAsyncResult ar) { UdpClient c = (UdpClient)ar.AsyncState; IPEndPoint temp = new IPEndPoint(0, 0); byte[] bytes = null; try { bytes = c.EndReceive(ar, ref temp); } catch (SocketException e) { Debug.Log("Socket error: " + e.ToString()); } if (bytes != null) { UDPToolkit.Packet packet = UDPToolkit.Packet.PacketFromBytes(bytes); if (m_connectionData.Receive(packet)) { m_connected = true; Distribute(packet); } } c.BeginReceive(EndReceiveCallback, c); }
private void Distribute(UDPToolkit.Packet packet) { for (int i = 0; i < m_receivers.Count; i++) { m_receivers[i].ReceivePacket(packet); } }
private void OnReceive(UDPToolkit.Packet packet, int playerID) { // TODO (maybe) : give up ticks and use only packet sequence number? for (int i = 0; i < m_receivers.Count; i++) { m_receivers[i].Receive(packet, playerID); } }
public void ReceivePacket(UDPToolkit.Packet packet) { ServerSuccessfulConnectMessage serverSuccessPing = common.serialization.IConvertible.CreateFromBytes <ServerSuccessfulConnectMessage>(packet.Data.ArraySegment()); if (serverSuccessPing != null) { m_waitingOnUDPResponse = false; #if DEBUG_LOG Debug.Log("Received UDP connection confirmation."); #endif // DEBUG_LOG } }
public void Receive(UDPToolkit.Packet packet, int playerID) { IdentificationMessage identification = Serializable.CreateFromBytes <common.data.IdentificationMessage>(packet.Data.ArraySegment()); if (identification != null) { #if DEBUG_LOG Debug.Log("Received UDP confirmation from player (ID " + playerID + ")"); #endif // DEBUG_LOG ServerSuccessfulConnectMessage serverSuccessPing = new ServerSuccessfulConnectMessage(); m_UDPServer.Send(serverSuccessPing.GetBytes(), playerID); } }
public void ReceivePacket(UDPToolkit.Packet packet) { if (!Initialized && !ConnectedToServer) { return; } // TODO remove tick from ClientSTate and add it to custom server state packet? // client doesnt need its own client state ticks lock (m_lock) { ClientState state = common.serialization.IConvertible.CreateFromBytes <ClientState>(packet.Data.ArraySegment()); if (state != null) { state.PlayerGUID = PlayerID.Value; m_lastServerState = state; #if DEBUG_LOG Debug.Log("Received server state tick " + state.Tick.Value); #endif //DEBUG_LOG m_remoteTick = state.Tick.Value; if (m_localTick < m_remoteTick) { #if DEBUG_LOG Debug.Log("Client has fallen behind by " + (m_remoteTick - m_localTick) + ". Fast-forwarding."); #endif //DEBUG_LOG m_localTick = m_remoteTick + (uint)m_simulationBuffer; } // PATCH FOR JITTER (too many phy simulate calls) // TODO: investigate (si le temps le permet) // ClientCorrection() if (m_localTick > m_remoteTick + (uint)m_simulationBuffer) { m_localTick = m_remoteTick; } } } }
/// <summary> /// Tries to send a packet with a data payload. /// if too many packets are sent, the packet is dropped. /// </summary> /// <param name="data"></param> public void Send(byte[] data, int playerID) { if (m_currentTime - m_lastPacketSentTime < 1.0f / m_maximumPacketsPerSecond) { return; } try { UDPToolkit.Packet packet = m_connectionData.Send(data, playerID); uint seq = packet.Sequence; m_sequencesSendTime.Add(seq, m_currentTime); byte[] bytes = packet.RawBytes; m_client.BeginSend(bytes, bytes.Length, m_server, EndSendCallback, m_client); m_lastPacketSentTime = m_currentTime; } catch (SocketException e) { Debug.Log("Client socket exception: " + e); } }
private void EndReceiveCallback(System.IAsyncResult ar) { IPEndPoint clientEndPoint = new IPEndPoint(0, 0); UdpClient server = (UdpClient)ar.AsyncState; byte[] bytes = server.EndReceive(ar, ref clientEndPoint); UDPToolkit.Packet packet = UDPToolkit.Packet.PacketFromBytes(bytes); int playerID = packet.PlayerID; if (packet.HasValidProtocolID()) { if (!m_clients.ContainsKey(playerID)) { if (AcceptNewClients) { #if DEBUG_LOG Debug.Log("Received data from unregistered client(" + playerID.ToString() + "). Adding to clients."); #endif // DEBUG_LOG AddNewClient(playerID, clientEndPoint); } else { #if DEBUG_LOG Debug.Log("Received data from unregistered client(" + playerID.ToString() + "). Not accepting new connections."); #endif // DEBUG_LOG } } if (m_clients.ContainsKey(playerID)) { if (!m_endpointsSet.Contains(clientEndPoint)) { // this means the player client doesnt have the same UDP endpoint (probably because he DC'd) // we delete the endpoint previously paired with the playerID (if any) if (m_playerEndpoints.ContainsKey(playerID)) { if (m_playerEndpoints[playerID] != null) { m_endpointsSet.Remove(m_playerEndpoints[playerID]); } } m_playerEndpoints[playerID] = clientEndPoint; m_endpointsSet.Add(clientEndPoint); m_clients[playerID].Close(); m_clients[playerID].Connect(clientEndPoint); } if (m_clientConnections[playerID].Receive(packet)) { OnReceive(packet, playerID); } } } else { #if DEBUG_LOG Debug.Log("Received invalid network protocol ID packet. Rejecting."); #endif //DEBUG_LOG } server.BeginReceive(EndReceiveCallback, server); }