public override void Connect(ServerInfo serverInfo) { if (Connected) { throw new ProtocolException("Connection error: Already connected to server."); } foreach (var modAPI in ModAPIs) { modAPI.OnConnect(serverInfo); } // -- Connect to server. Stream.Connect(serverInfo.Address.IP, serverInfo.Address.Port); // -- Begin data reading. if (ReadThread != null && ReadThread.IsRunning) { throw new ProtocolException("Connection error: Thread already running."); } else { ReadThread = ThreadWrapper.Create(ReadCycle); ReadThread.IsBackground = true; ReadThread.Name = "PacketReaderThread"; ReadThread.Start(); } }
// null-safe & exception-safe: 2019-1-8 private bool StartListen() { if (ReadThread == null) { Debugger.Log("ReadThread is null.", Debugger.LogLevel.WARN); return(false); } if (Listening) { Debugger.Log("ReadThread already running.", Debugger.LogLevel.INFO); return(false); } try { ReadThread.Start(); Debugger.Log("Successfully started listening.", Debugger.LogLevel.INFO); Listening = true; return(true); } catch (Exception e) { Debugger.Log("Exception while starting ReadThread: \n" + e.ToString(), Debugger.LogLevel.WARN); return(false); } }
protected void RestartWorkingThread() { readThread = new ReadThread(this); readThread.Start(); while (!readThread.IsAlive) { } // Busy waiting }
/// <summary> /// Connects this WebSocketClient to the WebSocket Server. /// </summary> /// <returns>Task.</returns> /// <exception cref="Exception">If the WebSocket Connection is already Connected or Connecting.</exception> public async Task ConnectAsync() { if (Status == WebSocketState.Open || Status == WebSocketState.Connecting) { throw new Exception("Websocket already Connected or Connecting"); } await ClientWebSocket.ConnectAsync(Uri, CancellationToken); Exit = false; Open?.Invoke(this, EventArgs.Empty); ReadThread.Start(); }
/* * Restart workingThread if it has been killed before */ protected void RestartWorkingThread() { if (mr1Version && workerThread == null) { workerThread = new WorkerThread(this); workerThread.Start(); workerThread.SetCallback(readCallback); while (!workerThread.IsAlive) { } // Busy waiting Log.Info("UsbSerialDevice", "Read Thread started."); } else if (!mr1Version && readThread == null) { readThread = new ReadThread(this); readThread.Start(); readThread.SetCallback(readCallback); while (!readThread.IsAlive) { } // Busy waiting Log.Info("UsbSerialDevice", "Read Thread started."); } }
private void FinishConnect(IAsyncResult result) { try { var beginClient = (TcpClient)result.AsyncState; if (beginClient == null) { Close(); return; } beginClient.EndConnect(result); if (beginClient.Connected) { // 启动读取和写入线程。 _readThread = new ReadThread(this, beginClient, _msgQueue); _readThread.Start(); _writeThread = new WriteThread(this, beginClient, _msgQueue); _writeThread.Start(); // 已经连接上。 Status = SocketStatus.Connected; _client = beginClient; } else { // 未连接成功,关闭当前连接。 Close(); } } catch (Exception e) { MyLog.ErrorAsync(Tag, string.Format("end connect error: {0}\n{1}", e.Message, e.StackTrace)); Close(); } }
public bool Connect(string ScannerModel)//Connect to Paired BT Device containing { try { BluetoothAdapter.DefaultAdapter.Enable(); BluetoothAdapter.DefaultAdapter.CancelDiscovery(); } catch (System.Exception) { } ScannerModel = ScannerModel.ToLower(); foreach (BluetoothDevice device in BluetoothAdapter.DefaultAdapter.BondedDevices) { Log.Info(TAG, "found: " + device.Name); MainActivity.onStatusChanged("found: " + device.Name); if (device.Name.ToLower().Contains(ScannerModel)) { if (device != null && Connected == false) { string SPP_UUID = "00001101-0000-1000-8000-00805f9b34fb"; bluetoothSocket = device.CreateInsecureRfcommSocketToServiceRecord(Java.Util.UUID.FromString(SPP_UUID)); try { bluetoothSocket.Connect(); } catch (Java.Lang.Exception Ex) { Toast.MakeText(m_context, Ex.Message.ToString(), ToastLength.Long).Show(); MainActivity.onStatusChanged(Ex.Message); try { System.Threading.Thread.Sleep(1000); bluetoothSocket.Connect(); } catch (Java.Lang.Exception Ex2) { Connected = false; Toast.MakeText(m_context, Ex2.Message.ToString(), ToastLength.Long).Show(); MainActivity.onStatusChanged(Ex2.Message); m_state = eStatus.STATE_NONE; } } if (bluetoothSocket != null && bluetoothSocket.IsConnected) { Toast.MakeText(m_context, "Bluetooth Scanner Connected", ToastLength.Long).Show(); Connected = true; // Start the thread to manage the connection and perform transmissions readThread = new ReadThread(bluetoothSocket); readThread.Start(); return(true); } } else { Toast.MakeText(m_context, "DEVICE NOT FOUND : " + ScannerModel, ToastLength.Long).Show(); Connected = false; m_state = eStatus.STATE_NONE; } } } return(false); }
private void Start() { _helloRequester = new ReadThread(); Debug.Log("start thread"); _helloRequester.Start(); }