void ConnectCallback(IAsyncResult ar) { try { _tcp.EndConnect(ar); _stream = _tcp.GetStream(); Debug.Log($"NS CON: Socket connected to {_tcp.Client.RemoteEndPoint.ToString()}"); ThreadDispatcher.RunOnMainThread(() => { Message(this, new StateChangedEventArgs(State.CONNECTED, "connected, checking version...")); }); CheckVersion(); ThreadDispatcher.RunOnMainThread(() => { Sync(); }); } catch (Exception ex) { if (_tcp != null) { _tcp.Close(); _tcp = null; } Debug.LogError($"NS CON: {ex.Message}"); ThreadDispatcher.RunOnMainThread(() => { Message(this, new StateChangedEventArgs(State.FAILED_TO_CONNECT, $"failed to connect: {ex.Message}")); }); } }
// internal methods void CheckVersion() { if (!IsConnected) { return; } byte result = SndAndRcv("QMAC-"); if (result == 'F') { var message = "version cannot be checked"; ThreadDispatcher.RunOnMainThread(() => { Message(this, new StateChangedEventArgs(State.FAILED_TO_CHECK_ECI_VERSION, message)); }); throw new Exception(message); } else if (result == 'I') { byte[] buffer = new byte[256]; _stream.Read(buffer, 0, 1); var version = buffer[0]; if (version != 1 && version != '1') { var message = $"unknown version: {(char)version} (\\x{version})"; ThreadDispatcher.RunOnMainThread(() => { Message(this, new StateChangedEventArgs(State.UNSUPPORTED_ECI_VERIONS, message)); }); throw new Exception(message); } } }