public void Start(string server, string port) { try { session = new GameSession(); var tcpClient = new SimpleTcpClient().Connect(server, int.Parse(port)); Log("tcp client connected to server"); var handshakeCommand = new HandshakeCommand { me = $"player{new Random().Next(1000)} {punter.GetType().Name}" }; Log($"Begin handshake as {handshakeCommand.me}"); var reply = tcpClient.WriteAndGetReply(serializer.Serialize(handshakeCommand), TimeSpan.MaxValue); session.Status = GameStatus.Setup; tcpClient.DataReceived += TcpClient_DataReceived; var handshakeMessage = serializer.Deserialize <HandshakeMessage>(reply.MessageString); if (handshakeMessage.you != handshakeCommand.me) { throw new Exception($"me: {handshakeCommand.me}, you: {handshakeMessage.you}"); } } catch (Exception e) { Log(e.ToString()); } }
public async Task ConnectAsync(string hostName) { Close(); _socket = _socketFactory.Create(); _socket.OnMessage += OnMessage; _socket.Connect($"ws://{hostName}:3000"); if (!_socket.IsAlive) { throw new ConnectionException($"Unable to conenct to television at {hostName}."); } _hostName = hostName; var key = await _keyStore.GetKeyAsync(hostName); var handshakeCommand = new HandshakeCommand(key); var handshakeResponse = await SendCommandAsync <HandshakeResponse>(handshakeCommand); await _keyStore.StoreKeyAsync(hostName, handshakeResponse.Key); var mouseCommand = new MouseGetCommand(); var mouseGetResponse = await SendCommandAsync <MouseGetResponse>(mouseCommand); _mouseSocket = _socketFactory.Create(); _mouseSocket.Connect(mouseGetResponse.SocketPath); if (!_mouseSocket.IsAlive) { throw new ConnectionException($"Unable to conenct to television mouse service at {hostName}."); } }
internal static async Task Handshake(ClientConnection connection, TestLoggerBackend logger, Handshake handshake, CancellationToken cancellationToken) { Connection.Debug("Client Handshake: {0}", handshake); handshake.EventSink = new EventSinkServant(connection, logger); var handshakeCommand = new HandshakeCommand(); await handshakeCommand.Send(connection, handshake, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); Connection.Debug("Client Handshake done"); }