private IUnityTask OnConnectionRequest(ConnectionRequest connectionRequest, IPEndPoint senderAddress) { if (IsPlayerConnected(senderAddress) || IsFull) { return(EmptyTask); } //TODO (Optimization) Send whole player data only once and then send only meditation value var newPlayer = new PlayerData { Meditation = 0f, Name = connectionRequest.PlayerName }; _connectedPlayers.Add(senderAddress, newPlayer); if (!IsFull) { var response = new ConnectionResponse(); var responseTask = new Common.SendMessageTask(_connection, response, senderAddress); return(responseTask); } var opponentData = GetAnotherPlayer(newPlayer); var connectionResponse = new ConnectionResponse(opponentData.Value); var sendToPlayer = new Common.SendMessageTask(_connection, connectionResponse, senderAddress); var sendToOpponent = new Common.SendMessageTask(_connection, newPlayer, opponentData.Key); var fireEvent = new FireEventTask(matchCreated); return(new BatchTask(sendToOpponent, sendToPlayer, fireEvent)); }
private IUnityTask OnPing(Ping ping, IPEndPoint senderAddress) { if (_isConnected || _pendingConnection != null) { return(EmptyTask); } _pendingConnection = senderAddress; var connectionMessage = new ConnectionRequest(_synchronizationData.myName); var sendMessage = new Common.SendMessageTask(_connection, connectionMessage, senderAddress); var debug = new DebugTask($"Started waiting for connection", _logger); return(new BatchTask(debug, sendMessage)); }