Inheritance: IConnection
Beispiel #1
0
        /// <summary>
        /// Sets the latency of a connection.
        /// </summary>
        /// <param name="pingPackage">The PingPackage.</param>
        private void SetLatency(PingPackage pingPackage)
        {
            DateTime      timeNow    = DateTime.Now;
            TimeSpan      dif        = timeNow - pingPackage.TimeStamp;
            UdpConnection connection = GetConnection(pingPackage.Receiver);

            connection.Latency = (float)dif.TotalMilliseconds;

            //alert the connectionmanager
            _connectionManager.RemoveByIP(pingPackage.Receiver);

            //Kick the client if the latency is to high
            if (!(connection.Latency > TimeOutLatency))
            {
                return;
            }
            SendNotificationPackage(NotificationMode.TimeOut,
                                    new IConnection[] { SerializableConnection.FromIConnection(connection) });
            _connections.Remove(connection);
        }
Beispiel #2
0
        /// <summary>
        /// Accepts clients if available.
        /// </summary>
        private void BeginAcceptConnections()
        {
            var incommingConnection = new IPEndPoint(IPAddress.Any, 2565);

            while (IsActive)
            {
                try
                {
                    if (_listener.Available > 0)
                    {
                        byte[] receivedData = _listener.Receive(ref incommingConnection);
                        using (var mStream = new MemoryStream(receivedData))
                        {
                            IBasePackage package    = PackageSerializer.Deserialize(mStream);
                            var          udpPackage = package as UdpPackage;
                            if (udpPackage != null)
                            {
                                if (udpPackage.NotifyMode == UdpNotify.Hi)
                                {
                                    //notify
                                    SendNotificationPackage(NotificationMode.ClientJoined, _connections.ToArray());
                                    //add connection
                                    var udpConnection = new UdpConnection(incommingConnection.Address);
                                    _connections.Add(udpConnection);
                                    // publish the new server list, after a new connection.
                                    SendNotificationPackage(NotificationMode.ClientList, _connections.ToArray());
                                }
                                else //(UdpNotify.Bye)
                                {
                                    //notify
                                    SendNotificationPackage(NotificationMode.ClientExited, _connections.ToArray());
                                    //remove connection
                                    UdpConnection udpConnection = GetConnection(incommingConnection.Address);
                                    if (udpConnection != null)
                                    {
                                        _connections.Remove(udpConnection);
                                        // publish the new server list, after a lost connection.
                                        SendNotificationPackage(NotificationMode.ClientList, _connections.ToArray());
                                    }
                                }
                            }
                            else
                            {
                                //any other package handle in new void
                                HandlePackage(package);
                            }
                        }
                    }
                    else
                    {
                        //no data available

                        Idle();
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex.Message);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Accepts clients if available.
        /// </summary>
        private void BeginAcceptConnections()
        {
            var incommingConnection = new IPEndPoint(IPAddress.Any, 2565);

            while (IsActive)
            {
                try
                {
                    if (_listener.Available > 0)
                    {
                        byte[] receivedData = _listener.Receive(ref incommingConnection);
                        using (var mStream = new MemoryStream(receivedData))
                        {
                            IBasePackage package = PackageSerializer.Deserialize(mStream);
                            var udpPackage = package as UdpPackage;
                            if (udpPackage != null)
                            {
                                if (udpPackage.NotifyMode == UdpNotify.Hi)
                                {
                                    //notify
                                    SendNotificationPackage(NotificationMode.ClientJoined, _connections.ToArray());
                                    //add connection
                                    var udpConnection = new UdpConnection(incommingConnection.Address);
                                    _connections.Add(udpConnection);
                                    // publish the new server list, after a new connection.
                                    SendNotificationPackage(NotificationMode.ClientList, _connections.ToArray());
                                }
                                else //(UdpNotify.Bye)
                                {
                                    //notify
                                    SendNotificationPackage(NotificationMode.ClientExited, _connections.ToArray());
                                    //remove connection
                                    UdpConnection udpConnection = GetConnection(incommingConnection.Address);
                                    if (udpConnection != null)
                                    {
                                        _connections.Remove(udpConnection);
                                        // publish the new server list, after a lost connection.
                                        SendNotificationPackage(NotificationMode.ClientList, _connections.ToArray());
                                    }
                                }
                            }
                            else
                            {
                                //any other package handle in new void
                                HandlePackage(package);
                            }
                        }
                    }
                    else
                    {
                        //no data available

                        Idle();
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex.Message);
                }
            }
        }