Exemplo n.º 1
0
        public SpeedDateClient()
        {
            _handlers = new Dictionary <ushort, IPacketHandler>();
            _kernel   = new SpeedDateKernel();
            _listener = new SpeedDateNetListener();

            _listener.NetworkErrorEvent += (point, code) =>
            {
                Logs.Error($"NetworkError: ({code}): {point}");
            };
            _listener.PeerConnectedEvent += peer =>
            {
                _netPeer.MessageReceived += HandleMessage;

                _logger.Info("Connected");
                Started?.Invoke();
            };
            _listener.NetworkReceiveEvent += (peer, reader, method) =>
            {
                _netPeer.HandleDataReceived(reader.Data);
            };
            _listener.PeerDisconnectedEvent += (peer, info) =>
            {
                _manager.Stop();

                AppUpdater.Instance.Remove(this);

                _logger.Info("Disconnected");
                Stopped?.Invoke();
            };

            _manager = new NetManager(_listener);
        }
Exemplo n.º 2
0
        public SpeedDateServer()
        {
            _kernel   = new SpeedDateKernel();
            _handlers = new Dictionary <ushort, IPacketHandler>();

            _listener = new SpeedDateNetListener();
            _listener.ConnectionRequestEvent += request =>
            {
                request.AcceptIfKey(_config.Network.Key);
            };

            _listener.PeerConnectedEvent += peer =>
            {
                // Listen to messages
                peer.MessageReceived += OnMessageReceived;

                // Create the security extension
                var extension = peer.AddExtension(new PeerSecurityExtension());

                // Set default permission level
                extension.PermissionLevel = 0;

                _logger.Info($"Client {peer.ConnectId} connected.");
            };

            _listener.NetworkErrorEvent += (point, code) =>
            {
                Logs.Error($"NetworkError: ({code}): {point}");
            };

            _listener.NetworkReceiveEvent += (peer, reader, method) =>
            {
                peer.HandleDataReceived(reader.Data);
            };

            _listener.PeerDisconnectedEvent += (peer, info) =>
            {
                _logger.Info($"Client {peer.ConnectId} disconnected.");

                // Remove listener to messages
                peer.MessageReceived -= OnMessageReceived;

                peer.NotifyDisconnected();
            };

            _manager = new NetManager(_listener)
            {
                ReuseAddress = true
            };
        }