private void OnGameClientConnectError(object sender, SocketErrorEventArgs e) { log.WarnFormat("TestClient({1}): Failed to connect to game server: error = {0}", e.SocketError, userId); //TODO connection failed state? or set an error? this.connectionState = TestClientConnectionState.Disconnected; }
// public void StartWithMaster(string address, string gameName, string token) // { // var master = new TestMasterClient(); // master.Start("selfMonitoring", 0, token, this); // } public void Start(string ip, int port, string userId, string gameName, string token, int interval) { if (log.IsDebugEnabled) { log.DebugFormat("TestClient({2}): Connecting to game server at {0}:{1}", ip, port, userId); } var ipaddress = IPAddress.Parse(ip); this.userId = userId; this.gameId = gameName; this.token = token; this.interval = interval; endPoint = new IPEndPoint(ipaddress, port); this.gameServerClient = new TcpClient(); this.gameServerClient.ConnectError += this.OnGameClientConnectError; this.gameServerClient.ConnectCompleted += this.OnGameClientConnectCompleted; this.gameServerClient.OperationResponse += this.OnGameClientOperationResponse; this.gameServerClient.Event += OnGameClientEvent; this.gameServerClient.Disconnected += OnGameClientDisconnected; this.connectionState = TestClientConnectionState.Connecting; this.gameServerClient.Connect(endPoint, "Game"); }
public void Stop() { connectionState = TestClientConnectionState.Stopped; if (this.gameServerClient != null && this.gameServerClient.Connected) { this.gameServerClient.Disconnect(); } }
private void OnGameClientConnectCompleted(object sender, EventArgs e) { if (log.IsDebugEnabled) { log.DebugFormat("TestClient({0}): Successfully connected to game server.", userId); } this.connectionState = TestClientConnectionState.Connected; this.Authenticate(); }
private void OnGameClientDisconnected(object sender, SocketErrorEventArgs e) { if (log.IsDebugEnabled) { log.DebugFormat("TestClient({0}): disconnected from game server.", userId); } if (this.connectionState != TestClientConnectionState.Stopped) { this.connectionState = TestClientConnectionState.Disconnected; } }
private void Rejoin() { if (this.connectionState == TestClientConnectionState.Stopped) { return; } this.connectionState = TestClientConnectionState.Connecting; gameServerClient.Connect(endPoint, "Game"); if (log.IsDebugEnabled) { log.DebugFormat("TestClient({0}): trying to rejoin game '{1}'", userId, gameId); } }
private void OnGameClientOperationResponse(object sender, OperationResponseEventArgs e) { if (e.OperationResponse.ReturnCode != 0) { log.WarnFormat( "TestClient({3}): Received error response: code={0}, result={1}, msg={2}", e.OperationResponse.OperationCode, e.OperationResponse.ReturnCode, e.OperationResponse.DebugMessage, userId); return; } switch (e.OperationResponse.OperationCode) { case (byte)OperationCode.Authenticate: { this.connectionState = TestClientConnectionState.Authenticated; if (log.IsDebugEnabled) { log.DebugFormat("TestClient({0}): Successfully authenticated", userId); } JoinOrCreateGameOnGameServer(); break; } case (byte)Operations.OperationCode.JoinGame: { this.connectionState = TestClientConnectionState.InGame; if (log.IsDebugEnabled) { log.DebugFormat("TestClient({0}): Successfully joined/created game '{1}'", userId, gameId); } break; } default: { log.WarnFormat("TestClient({1}): received response for unexpected operation: {0}", e.OperationResponse.OperationCode, userId); return; } } }