/// <summary> /// Listens for incoming messages on the <see cref="NetworkStream" />. /// Fires a <see cref="MessageReceived" /> event when a new <see cref="IMessage" /> has been received. /// </summary> /// <param name="clientUserId">The Id of the user the NetworkStream is linked to.</param> /// <param name="tcpClient">The stream between the Client and the Server.</param> public void ReceiveMessages(int clientUserId, TcpClient tcpClient) { using (NetworkStream networkStream = tcpClient.GetStream()) { try { while (true) { MessageIdentifier messageIdentifier = MessageIdentifierSerialiser.DeserialiseMessageIdentifier(networkStream); IMessageSerialiser serialiser = SerialiserFactory.GetSerialiser(messageIdentifier); IMessage message = serialiser.Deserialise(networkStream); OnMessageReceived(new MessageEventArgs(message)); } } catch (IOException) { Log.Info("Detected client disconnection, notifying Server of ClientDisconnection."); IMessage message = new ClientDisconnection(clientUserId); OnMessageReceived(new MessageEventArgs(message)); } } }
public ServerActions(ServerInfo serverInfo, Game game, ClientDisconnection disconnectionHandler, ServerDataPackageProvider _dataPackageProvider) { _protocolActions = new Dictionary <ProtocolActionEnum, Action <ICommunication, DataPackage> >(); _disconnectionHandler = disconnectionHandler; _serverInfo = serverInfo; _game = game; this._dataPackageProvider = _dataPackageProvider; }
public Core() { _game = new Game(); _serverInfo = new ServerInfo(); _dataPackageProvider = new ServerDataPackageProvider(_serverInfo, _game); _udpserver = new UdpBroadcast(_serverInfo); _connectionHandler = new ClientConnection(_serverInfo, _dataPackageProvider); _disconnectionHandler = new ClientDisconnection(_game, _serverInfo, _dataPackageProvider); _actionsHandler = new ServerActions(_serverInfo, _game, _disconnectionHandler, _dataPackageProvider); _stateMachine = new StateMachine(_serverInfo, _actionsHandler, _game); _validationSystem = new ValidationSystem(_serverInfo, _disconnectionHandler, _connectionHandler, _dataPackageProvider, _actionsHandler); _server = new Server(_actionsHandler, _serverInfo, _stateMachine, _validationSystem, _disconnectionHandler); }
public Server(ServerActions actionHandler, ServerInfo serverInfo, StateMachine stateMachine, ValidationSystem validationSystem, ClientDisconnection disconnectionHandler) { _stateMachine = stateMachine; var bwStateMachine = new BackgroundWorker(); bwStateMachine.DoWork += (obj, ea) => _stateMachine.Start(); bwStateMachine.RunWorkerAsync(); _validationSystem = validationSystem; var bwValidationSystem = new BackgroundWorker(); bwValidationSystem.DoWork += (obj, ea) => _validationSystem.Start(); bwValidationSystem.RunWorkerAsync(); _serverInfo = serverInfo; _actionsHandler = actionHandler; _disconnectionHandler = disconnectionHandler; _queue = new PackageQueue(); _process = new PackageProcessing(_queue, _actionsHandler); _listener = new TcpListener(_serverInfo.ServerIPAdress, 8080); }
//断线提醒事件定义 #region protected virtual void clientdisconnection() { ClientDisconnection?.Invoke(); /* 事件被触发 */ }
public override void BeforeEachTest() { base.BeforeEachTest(); clientDisconnection = new ClientDisconnection(DefaultUser.Id); }