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> /// Sends a command to the module /// </summary> /// <param name="command">Response to send</param> /// <returns>true if the command has been sent, false otherwise</returns> public bool Send(Command command) { // Check if this module is the destination module for the command if (this != command.Destination) { throw new Exception("Command marked to be sent through other module"); } // Simulation mode #region Simulation Mode if (simOptions != SimulationOptions.SimulationDisabled) { //Response r = Response.CreateFromCommand(command, true); dataReceived.Enqueue( System.Text.ASCIIEncoding.Default.GetBytes(Response.CreateFromCommand(command, true).ToString()) ); return(true); } #endregion // Check is connection has been stablished. if (!server.Started) { return(false); } // Check if module is not busy and command is not a priority command if (Busy && !command.Prototype.HasPriority) { return(false); } // Send the command try { server.SendToAll(command.StringToSend + "\0"); if (command.Prototype.ResponseRequired) { Lock(command); } } catch { return(false); } return(true); }
/// <summary> /// Sends data through server socket /// </summary> /// <param name="s">String to send</param> /// <returns>true if data was sent successfully, false otherwise</returns> protected bool serverSend(string s) { int count; if (tcpServer.ClientsConnected < 1) { return(false); } if (tcpServer.Started) { lock (tcpServer) { count = tcpServer.SendToAll(s.Trim()); } //Console("Sent to " + count.ToString() + " clients on server: " + s); return(true); } else { //Console("Can`t Send through server [Client TCP]: " + (s.Length > 100 ? s.Substring(0, 100) : s)); return(false); } }
private void btnServerSend_Click(object sender, EventArgs e) { if (txtServerInput.Text.Length > 0) { #if BENCHMARK stopwatch.Reset(); stopwatch.Start(); #endif server.SendToAll(txtServerInput.Text); textForServerConsole = "[Sent: " + txtServerInput.Text.Substring(0, Math.Min(txtServerInput.Text.Length, 50)) + "]"; this.Invoke(updateServerConsoleEH); if (!txtServerInput.AutoCompleteCustomSource.Contains(txtServerInput.Text)) { txtServerInput.AutoCompleteCustomSource.Add(txtServerInput.Text); } txtServerInput.Text = ""; } txtServerInput.Focus(); }