Exemple #1
0
 private void SendInitialHeartbeat(IPEndPoint endPoint)
 {
     DarkUtils.WriteMagicHeader(initialHeartBeat, 0);
     DarkUtils.WriteInt32ToByteArray(-1, initialHeartBeat, 4);
     DarkUtils.WriteInt32ToByteArray(8, initialHeartBeat, 8);
     DarkUtils.WriteInt64ToByteArray(DateTime.UtcNow.Ticks, initialHeartBeat, 12);
     //Send 4 times.
     for (int i = 0; i < 4; i++)
     {
         socket.SendTo(initialHeartBeat, endPoint);
     }
 }
        internal void SendHeartbeat()
        {
            lock (connections)
            {
                long currentTime = DateTime.UtcNow.Ticks;
                foreach (KeyValuePair <Guid, Connection <T> > c in connections)
                {
                    if (currentTime > c.Value.lastReceiveTime + TimeSpan.TicksPerSecond * 20)
                    {
                        disconnectList.Add(c.Key);
                    }
                    if (currentTime > (c.Value.lastHeartbeatTime + TimeSpan.TicksPerSecond))
                    {
                        c.Value.lastHeartbeatTime = currentTime;
                        NetworkMessage nm = NetworkMessage.Create(-1, 8, NetworkMessageType.UNORDERED_UNRELIABLE);
                        DarkUtils.WriteInt64ToByteArray(DateTime.UtcNow.Ticks, nm.data.data, 0);
                        SendMessageWithHighPriority(nm, c.Value);
                    }
                    RateControl <T> .Update(c.Value);

                    c.Value.reliableMessageHandler.Send();
                }
                foreach (Guid disconnectConnectionGuid in disconnectList)
                {
                    Connection <T> disconnectConnection = connections[disconnectConnectionGuid];
                    if (disconnectCallback != null)
                    {
                        disconnectCallback(disconnectConnection);
                    }
                    disconnectConnection.reliableMessageHandler.ReleaseAllObjects();
                    network.ReleaseAllObjects(disconnectConnection);
                    connections.Remove(disconnectConnectionGuid);
                }
                disconnectList.Clear();
            }
        }