コード例 #1
0
ファイル: UAVBase.cs プロジェクト: wangbo121/UAV-NET
 /// <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());
     }
 }
コード例 #2
0
        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);
                    }
                }
            }
        }
コード例 #3
0
ファイル: UAVBase.cs プロジェクト: wangbo121/UAV-NET
 /// <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);
             }
         }
     }
 }
コード例 #4
0
 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);
             }
         }
     }
 }
コード例 #5
0
ファイル: UAVBase.cs プロジェクト: wangbo121/UAV-NET
        /// <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);
            }
        }
コード例 #6
0
ファイル: TcpCommandClient.cs プロジェクト: wangbo121/UAV-NET
        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();
            }
        }
コード例 #7
0
 /// <summary>
 /// Sendet ein Kommando an alle offenen Verbindungen dieses Endpunkts
 /// </summary>
 /// <param name="cmd"></param>
 public abstract void SendCommand(Commands.BaseCommand cmd);
コード例 #8
0
 /// <summary>
 /// No Commands via UDP for now
 /// </summary>
 /// <param name="cmd"></param>
 public override void SendCommand(Commands.BaseCommand cmd)
 {
 }
コード例 #9
0
 public void Send(BaseCommand cmd)
 {
     lock (SendSync)
     {
         if (client.Connected)
         {
             if (clientStream != null)
             {
                 BinaryFormatter formatter = new BinaryFormatter();
                 formatter.Serialize(clientStream, cmd);
                 clientStream.Flush();
             }
         }
     }
 }