/// <summary> /// Starts the TCP server asynchronously /// </summary> protected void BidirectionalConnectionTask() { if (!running || !autoReconnect) { return; } if (tcpServer == null) { ConfigureSockets(); } if (tcpServer.Port != portIn) { ConfigureSockets(); } while (!tcpServer.Started) { try { tcpServer.Port = portIn; tcpServer.BufferSize = DEFAULT_BUFFER_SIZE; tcpServer.Start(); //Console("TCP Server Started"); OnStatusChanged(); } catch { //Console("Can not start Tcp Server"); Thread.Sleep(500); } } }
public void Run() { /* * Se inicia el SocketTcpServer. */ server.Start(); // Espero a que conecte while (server.ClientsConnected == 0) { Thread.Sleep(10); } //Inicia timer sw.Start(); /* * Se crea la variable mediante * create_var "variable|0" */ server.SendToAll("create_var \"odometryPos|0\""); /* * Realizo la suscripcion. Para el ejemplo no me interesa verificar lo * que responde el Blackboard, pero todo llega a * server_DataReceived(...) * incluso la basura, y se imprime en pantalla. * * Nota que hay un error y dice "suscribe" en lugar de "subscribe". * Esto cambiara en la siguiente version, por lo que puedes programar * con "subscribe". */ server.SendToAll("suscribe_var \"localizerPos suscribe=writeany report=content\""); /* * La suscripcion es para la variable 'variable' * reportando cuando cualquier modulo escriba y reportando todo * el contenido de la variable. * * Ahora realizo 100 escrituras a intervalo de 100ms aprox. * Se reporta el tiempo de demora. */ for (int i = 0; i < 100000; ++i) { // Se obtiene el valor a escribir en la variable en hex string byte[] iData = BitConverter.GetBytes(i); string sData = String.Empty; for (int j = 0; j < iData.Length; ++j) { sData += iData[j].ToString("X2"); } server.SendToAll("write_var \"odometryPos|0x" + sData + "\""); Thread.Sleep(100); } server.Stop(); sw.Stop(); }
/// <summary> /// Starts the Blackboard /// </summary> /// <returns></returns> public void Start() { foreach (ModuleClient module in modules) { module.Start(); } server.Start(); }
private void btnServerStart_Click(object sender, EventArgs e) { if (!server.Started) { try { int port; if (!Int32.TryParse(txtServerPort.Text, out port)) { MessageBox.Show("Invalid port", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } server.Port = port; server.Start(); } catch { return; } txtServerPort.AutoCompleteCustomSource.Add(txtServerPort.Text); btnServerStart.Text = "Stop"; txtServerPort.Enabled = false; txtServerInput.Enabled = true; txtServerConsole.Enabled = true; btnServerSend.Enabled = true; } else { try { server.Stop(); } catch { return; } btnServerStart.Text = "Start"; txtServerPort.Enabled = true; txtServerInput.Enabled = false; txtServerConsole.Enabled = false; btnServerSend.Enabled = false; } }
/// <summary> /// Thread for start the Tcp Server and perform Module tasks /// </summary> private void MainThreadTask() { DateTime sendNextAliveTime = DateTime.Now; Thread thisThread; int i; stopMainThread = false; restartRequested = false; restartTestRequested = false; running = true; Busy = true; ExecuteStartupActions(); Busy = false; #region First time connection // This section is to allow to send the startup actions on first time connection. while (running && !stopMainThread) { if (!server.Started) { if (stopMainThread) { break; } server.Start(); Thread.Sleep(100); continue; } else { IsAlive = true; break; } } #region Send startup commands for (i = 0; (i < startupActions.Count) && !stopMainThread; ++i) { if (startupActions[i].Type != ActionType.Send) { continue; } bool result = startupActions[i].Execute(); if (ActionExecuted != null) { ActionExecuted(this, startupActions[i], result); } } #endregion #endregion while (running && !stopMainThread) { // Autoconnect (if disconnected) #region Autoconnect if (!server.Started) { if (stopMainThread) { break; } server.Start(); Thread.Sleep(10); continue; } else { IsAlive = true; } #endregion // Parse received data ParsePendingData(); // Check if module is alive and responding #region Check alive CheckAlive(ref sendNextAliveTime); #endregion // Check if module is busy and has not hang #region Check Busy CheckBusy(); #endregion // Performs additional actions if (restartTestRequested) { DoRestartTest(); } if (restartRequested) { DoRestartModule(); continue; } if (stopMainThread) { break; } Thread.Sleep(1); } Busy = true; ExecuteStopActions(); if ((server != null) && server.Started) { try { server.Stop(); } catch { } } running = false; Busy = false; if (Stopped != null) { Stopped(this); } if (StatusChanged != null) { StatusChanged(this); } #region Stop and Clear Thread thisThread = mainThread; mainThread = null; thisThread.Abort(); thisThread.Join(); #endregion }