Beispiel #1
0
        private void SetDefaults()
        {
            CancelationToken = new CancellationTokenSource();

            Connections          = new ConcurrentDictionary <EndPoint, NetConnection>();
            NetConnectionFactory = new NetConnectionFactory();
        }
Beispiel #2
0
        private void ConnectionCallback(IAsyncResult ar)
        {
            Socket socket = null;

            try
            {
                socket = ListenerSocket.EndAccept(ar);
            }
            catch
            {
                Log.Warn("Failed to accept connection!");
            }

            ListenerSocket.BeginAccept(ConnectionCallback, null);

            if (socket == null)
            {
                return;
            }

            NetConnection conn = NetConnectionFactory.CreateConnection(Direction.Client, socket, ConfirmdAction);

            if (Connections.TryAdd(socket.RemoteEndPoint, conn))
            {
                conn.OnConnectionClosed += (sender, args) =>
                {
                    NetConnection nc;
                    if (Connections.TryRemove(args.Connection.RemoteEndPoint, out nc))
                    {
                        Log.Info("Client disconnected!");
                    }
                };
                conn.Initialize();
            }
            else
            {
                Log.WarnFormat("Could not create new active connection!");
            }
        }