Пример #1
0
        /// <summary>
        /// Sends a package to a specified client.
        /// </summary>
        /// <param name="package">The Package.</param>
        /// <param name="connection">The Connection</param>
        private void SendTo(IBasePackage package, IPAddress connection)
        {
            LocalConnection localConnection = GetConnection(connection);

            if (localConnection != null)
            {
                PackageSerializer.Serialize(package, localConnection.Client.GetStream());
            }
        }
Пример #2
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;
            LocalConnection connection = GetConnection(pingPackage.Receiver);

            connection.Latency = (float)dif.TotalMilliseconds;

            //Kick the client if the latency is to high
            if (!(connection.Latency > TimeOutLatency))
            {
                return;
            }
            SendNotificationPackage(NotificationMode.TimeOut,
                                    new IConnection[] { SerializableConnection.FromIConnection(connection) });
            connection.Client.Close();
            _connections.Remove(connection);
        }
Пример #3
0
 /// <summary>
 /// Accepts clients if available.
 /// </summary>
 private void BeginAcceptConnections()
 {
     while (IsActive)
     {
         TcpClient tcpClient = _localListener.AcceptTcpClient();
         //Reset idle
         _idleTimeout = 0;
         _currentIdle = 0;
         var localConnection = new LocalConnection(tcpClient);
         _connections.Add(localConnection);
         //Handle connection.
         var pts          = new ParameterizedThreadStart(HandleClient);
         var handleClient = new Thread(pts)
         {
             IsBackground = true
         };
         SendNotificationPackage(NotificationMode.ClientJoined,
                                 new IConnection[] { SerializableConnection.FromIConnection(localConnection) });
         IConnection[] connectionList = SerializableConnection.FromIConnection(_connections.ToArray());
         SendNotificationPackage(NotificationMode.ClientList, connectionList);
         handleClient.Start(localConnection);
     }
 }
Пример #4
0
 /// <summary>
 /// Accepts clients if available.
 /// </summary>
 private void BeginAcceptConnections()
 {
     while (IsActive)
     {
         TcpClient tcpClient = _localListener.AcceptTcpClient();
         //Reset idle
         _idleTimeout = 0;
         _currentIdle = 0;
         var localConnection = new LocalConnection(tcpClient);
         _connections.Add(localConnection);
         //Handle connection.
         var pts = new ParameterizedThreadStart(HandleClient);
         var handleClient = new Thread(pts) {IsBackground = true};
         SendNotificationPackage(NotificationMode.ClientJoined,
             new IConnection[] {SerializableConnection.FromIConnection(localConnection)});
         IConnection[] connectionList = SerializableConnection.FromIConnection(_connections.ToArray());
         SendNotificationPackage(NotificationMode.ClientList, connectionList);
         handleClient.Start(localConnection);
     }
 }