/// <summary> /// Ein neuer Kommand ist über eine Kommunikationsschnittstelle angekommen /// kümmern wir uns darum! /// </summary> /// <param name="client"></param> /// <param name="Command"></param> public virtual void CommandRecieved(System.Net.Sockets.TcpClient client, Commands.BaseCommand recievedCommand) { Console.WriteLine(recievedCommand.GetType().ToString()); try{ if (recievedCommand.completed) { recievedCommand.ProcessResults(this); if (!recievedCommand.isAsync) { lock (SynclockCommands) { SyncCommands.Add(recievedCommand.guid, recievedCommand); } } } else { Console.WriteLine("RemoteExec"); recievedCommand.RemoteExecute(this); recievedCommand.completed = true; BinaryFormatter formatter = new BinaryFormatter(); MemoryStream memstr = new MemoryStream(); formatter.Serialize(memstr, recievedCommand); var outputstream = client.GetStream(); outputstream.Write(memstr.ToArray(), 0, Convert.ToInt32(memstr.Length)); outputstream.Flush(); } }catch (Exception ex) { Console.WriteLine(ex.Message + " " + ex.StackTrace.ToString()); } }
public void SendCommand(BaseCommand cmd) { TcpClient lastclient = null; lock (lockclients) { try { foreach (TcpClient myclient in clients) { lastclient = myclient; if (myclient.Connected) { NetworkStream clientStream = myclient.GetStream(); if (clientStream != null) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(clientStream, cmd); clientStream.Flush(); } } } } catch (Exception ex) { lock (lockclients) { clients.Remove(lastclient); } } } }
/// <summary> /// Send a Command but does not wait for it to succeed and come back /// </summary> /// <param name="cmd"></param> public void SendCommandAsync(Commands.BaseCommand cmd) { cmd.Send(this); foreach (CommunicationEndpoint mycomm in knownEndpoints) { if (mycomm.Connected()) { if (mycomm.commType == CommunicationEndpoint.Communicationtype.Command) { mycomm.SendCommand(cmd); } } } }
public override void SendCommand(Commands.BaseCommand cmd) { if (this.Connected()) { if (this.commType == Communicationtype.Command) { if (listen) { ((TCPServer.CommandTcpServer)server).SendCommand(cmd); } else { ((TcpCommandClient)client).Send(cmd); } } } }
/// <summary> /// Sends a Command and waits until it comes back with the Results /// </summary> /// <param name="cmd">An Command object to be sent, containing code and values</param> /// <returns>The resulting Command object which came back</returns> public Commands.BaseCommand SendCommand(Commands.BaseCommand cmd) { //Sende Kommando an alle Endpunkte bool sent = false; cmd.Send(this); foreach (CommunicationEndpoint mycomm in knownEndpoints) { if (mycomm.Connected()) { if (mycomm.commType == CommunicationEndpoint.Communicationtype.Command) { sent = true; mycomm.SendCommand(cmd); } } } if (!sent) { return(cmd); } // Warte auf ankunft des Ergebnisses bool CommandRecieved = false; do { lock (SynclockCommands) { if (SyncCommands.ContainsKey(cmd.guid)) { CommandRecieved = true; } } Thread.Sleep(5); } while (!CommandRecieved); ///Remove recieved Command from store lock (SynclockCommands) { Commands.BaseCommand recievedCmd = SyncCommands[cmd.guid]; SyncCommands.Remove(cmd.guid); return(recievedCmd); } }
public void Recieve() { TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); string line = ""; byte[] message = new byte[4096]; int bytesRead; try { while (true) { Thread.Sleep(0); bytesRead = 0; clientStream.Flush(); try { //blocks until a client sends a message UTF8Encoding encoder = new UTF8Encoding(); BinaryFormatter formatter = new BinaryFormatter(); Commands.BaseCommand recievedCommand = (Commands.BaseCommand)formatter.Deserialize(clientStream); // System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead)); cmdHandler.CommandRecieved(tcpClient, recievedCommand); } catch (Exception ex) { Console.WriteLine("Error on Recieve Command" + ex.Message); //a socket error has occured break; } } } catch (Exception ex) { } finally { tcpClient.Close(); //Close the current Connection Disconnect(); } }
/// <summary> /// Sendet ein Kommando an alle offenen Verbindungen dieses Endpunkts /// </summary> /// <param name="cmd"></param> public abstract void SendCommand(Commands.BaseCommand cmd);
/// <summary> /// No Commands via UDP for now /// </summary> /// <param name="cmd"></param> public override void SendCommand(Commands.BaseCommand cmd) { }
public void Send(BaseCommand cmd) { lock (SendSync) { if (client.Connected) { if (clientStream != null) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(clientStream, cmd); clientStream.Flush(); } } } }