Пример #1
0
        private void EndRead(IAsyncResult ar)
        {
            UdpReader reader = null;
            Exception err    = null;
            Action <Exception, UdpReader> copy = _readCompleted;

            _readCompleted = null;

            lock (_sync)
            {
                try
                {
                    if (_disposed)
                    {
                        throw new ObjectDisposedException(typeof(UdpClient).FullName);
                    }

                    IPEndPoint ep       = new IPEndPoint(IPAddress.Any, 0);
                    byte[]     datagram = _client.EndReceive(ar, ref ep);
                    Log.Trace(Toolkit.DumpToLog, "received datagram: {0}", datagram);
                    reader = new UdpReader(datagram);
                }
                catch (Exception ex)
                {
                    err = ex;
                }
            }

            copy(err, reader);
        }
Пример #2
0
        public TResp Request <TReq, TResp>(call_body callBody, TReq reqArgs)
        {
            UdpReader mr      = new UdpReader(_receivedArray);
            Reader    r       = Toolkit.CreateReader(mr);
            rpc_msg   respMsg = r.Read <rpc_msg>();


            rpc_msg reqHeader = new rpc_msg()
            {
                xid  = respMsg.xid,                // use xid in _receivedArray
                body = new body()
                {
                    mtype = msg_type.CALL,
                    cbody = callBody
                }
            };

            UdpWriter dtg = new UdpWriter();
            Writer    w   = Toolkit.CreateWriter(dtg);

            w.Write(reqHeader);
            w.Write(reqArgs);

            byte[] outBuff = dtg.Build();
            Assert.AreEqual(_expectedSendArray, outBuff, "send dump is difference");

            Toolkit.ReplyMessageValidate(respMsg);
            TResp respArgs = r.Read <TResp>();

            mr.CheckEmpty();

            return(respArgs);
        }
Пример #3
0
 public MessageReciever(int port)
 {
     this.port = port;
     reader    = new UdpReader(port);
     oscDispatcher.AddNetReader(reader);
     oscDispatcher.Bundle += OnRecieve;
     StartRecieving();
 }
Пример #4
0
 //Constructors
 public MessageReciever()
 {
     port   = 4445;
     reader = new UdpReader(port);
     oscDispatcher.AddNetReader(reader);
     oscDispatcher.Bundle += OnRecieve;
     StartRecieving();
 }
 private void listen()
 {
     while (connected)
     {
         try
         {
             if (oscDataType == OscDataType.OscMessage)
             {
                 OscMessage packet = receiver.Receive();
                 if (packet != null)
                 {
                     lock (processQueue)
                     {
                         //Debug.Log( "adding  packets " + processQueue.Count );
                         processQueue.Add((OscElement)packet);
                     }
                 }
                 else
                 {
                     Console.WriteLine("null packet");
                 }
             }
             else
             {
                 OscBundle packet = receiver.ReceiveBundle();
                 if (packet != null)
                 {
                     lock (processQueue)
                     {
                         foreach (OscElement element in packet.Elements)
                         {
                             //Debug.Log( "adding  packets " + processQueue.Count );
                             processQueue.Add(element);
                         }
                     }
                 }
                 else
                 {
                     Console.WriteLine("null packet");
                 }
             }
         }
         catch (Exception e)
         {
             Debug.Log(e.Message);
             Console.WriteLine(e.Message);
         }
     }
     if (receiver != null)
     {
         receiver.Dispose();
         receiver = null;
     }
 }
    // Use this for initialization
    void Start()
    {
        this.oscReceiver = null;
        this.receivedMessages = null;
        this.receivedMessages = new List<OscElement> ();

        if (oscReceiver == null)
        {
            oscReceiver = new UdpReader(listeningPort);
        }
    }
Пример #7
0
 static void Main(string[] args)
 {
     // 1. Instantiate BT connection to NXT
        //    You can peek in the class file for more documentation
        NXTBluetooth bt = new NXTBluetooth(@"Tox NXT");
        // 2. Instantiate the OSC receiver
        NetReader stream = new UdpReader(8000);
        // this is the main handler when an OSC message is recieved, per 100ms to not clog the BT
        Timer events = new Timer(new TimerCallback(
             delegate(object state) {
                  object o;
                  o = stream.Receive(); // 3. receive the info
                  if (o != null) {
                       // 4. convert the information
                       OscElement element = (OscElement)o;
                       //Console.WriteLine(string.Format("Message Received [{0}]: {1}",
                       //     element.Address,
                       //     element.Args[0]));
                       object arg;
                       // get the value passed
                       if (element.Args != null &&
                            element.Args.Length > 0 &&
                            (arg = element.Args[0]) != null) {
                            // 5. check on which value it is then send the message to BT
                            int value = 0;
                            switch (element.Address) {
                                 case "/1/leftTrack":
                                      value = 10000 + (int)((float)element.Args[0] * 100);
                                      bt.SendString(value.ToString());
                                      break;
                                 case "/1/rightTrack":
                                      value = 20000 + (int)((float)element.Args[0] * 100);
                                      bt.SendString(value.ToString());
                                      break;
                                 case "/1/turret":
                                      value = 30000 + (int)((float)element.Args[0] * 100);
                                      bt.SendString(value.ToString());
                                      break;
                                 case "/1/stop":
                                      value = 40000;
                                      bt.SendString(value.ToString());
                                      break;
                                 default:
                                      // ignore
                                      break;
                            }
                            Console.WriteLine(value.ToString());
                       }
                  }
             }), null, 0, 1);
        // timer starts immediately, ticks every 1ms
        Console.ReadKey(); // press any key to quit
 }
Пример #8
0
        public void SetUp()
        {
            this.server = new UdpClient(0);

            int port = ((IPEndPoint)this.server.Client.LocalEndPoint).Port;

            this.remoteIpEndPoint = new IPEndPoint(IPAddress.Loopback, port);

            this.client = new UdpClient();

            this.reader = new UdpReader(this.server.Client, 100);
            this.writer = new UdpWriter(this.client.Client, 100);
        }
 public void connect()
 {
     try
     {
         //print("connecting.");
         connected = true;
         receiver  = new UdpReader(port);
         thread    = new Thread(new ThreadStart(listen));
         thread.Start();
     }
     catch (Exception e)
     {
         Debug.Log("failed to connect to port " + port);
         Debug.Log(e.Message);
     }
 }
Пример #10
0
        private void OnMessageReaded(Exception err, UdpReader udpReader)
        {
            if (err != null)
            {
                Log.Debug("No receiving UDP datagrams. Reason: {0}", err);
                OnException(err);
                return;
            }

            lock (_sync)
                _receivingInProgress = false;

            rpc_msg respMsg = null;
            Reader  r       = null;

            try
            {
                r       = Toolkit.CreateReader(udpReader);
                respMsg = r.Read <rpc_msg>();
            }
            catch (Exception ex)
            {
                Log.Info("Parse exception: {0}", ex);
                BeginReceive();
                return;
            }

            Log.Trace("Received response xid:{0}", respMsg.xid);
            ITicket ticket = EnqueueTicket(respMsg.xid);

            BeginReceive();
            if (ticket == null)
            {
                Log.Debug("No handler for xid:{0}", respMsg.xid);
            }
            else
            {
                ticket.ReadResult(udpReader, r, respMsg);
            }
        }
Пример #11
0
 void Start()
 {
     if (reader == null)
     {
         reader = new UdpReader(4000);
     }
     sphere     = new GameObject[21];
     skeleton   = new float[336];
     AvatarRoot = GameObject.FindGameObjectWithTag("AvatarRoot").transform;
     for (int i = 0; i < 21; i++)
     {
         if (i == 5 || i == 8 || i == 10 || i == 15 || i == 20)
         {
             sphere[i] = (GameObject)GameObject.Instantiate(AvatarSphereCollider, new Vector3(0f, 0f, 0f), Quaternion.identity);              //Sphären Instanzieren und Erstellen
         }
         else
         {
             sphere[i] = (GameObject)GameObject.Instantiate(AvatarSphereBody, new Vector3(0f, 0f, 0f), Quaternion.identity);              //Sphären Instanzieren und Erstellen
         }
         sphere[i].transform.parent = AvatarRoot;
     }
 }
Пример #12
0
    // Use this for initialization
    void Start()
    {
        playerRobot = GameObject.Find ("PlayerRobot");
        pcAccessor = playerRobot.GetComponent<PlayerController> ();
        //peAccessor = playerRobot.GetComponent<PlayerEnergy>();

        readGestures = new UdpReader (12346);
    }
    // Use this for initialization
    void Start()
    {
        this.lockObject = new System.Object ();
        this.oscReceiver = null;
        this.receivedMessages = null;
        this.receivedMessages = new List<OscElement> ();
        Debug.Log("Initializing Receiver " );
        if (oscReceiver == null)
        {
            oscReceiver = new UdpReader(listeningPort);
        }

        Debug.Log("Initializing thread " );

        //initializing thread
        receiveThread = new Thread(
            new ThreadStart(ReceiveData));
        receiveThread.IsBackground = true;
        receiveThread.Start();
    }
Пример #14
0
        static void Main(string[] args)
        {
            // 1. Instantiate BT connection to NXT
            //    You can peek in the class file for more documentation
            NXTBluetooth bt = new NXTBluetooth(@"Tox NXT");
            // 2. Instantiate the OSC receiver
            NetReader stream = new UdpReader(8000);
            // this is the main handler when an OSC message is recieved, per 100ms to not clog the BT
            Timer events = new Timer(new TimerCallback(
                                         delegate(object state) {
                object o;
                o = stream.Receive();          // 3. receive the info
                if (o != null)
                {
                    // 4. convert the information
                    OscElement element = (OscElement)o;
                    //Console.WriteLine(string.Format("Message Received [{0}]: {1}",
                    //     element.Address,
                    //     element.Args[0]));
                    object arg;
                    // get the value passed
                    if (element.Args != null &&
                        element.Args.Length > 0 &&
                        (arg = element.Args[0]) != null)
                    {
                        // 5. check on which value it is then send the message to BT
                        int value = 0;
                        switch (element.Address)
                        {
                        case "/1/leftTrack":
                            value = 10000 + (int)((float)element.Args[0] * 100);
                            bt.SendString(value.ToString());
                            break;

                        case "/1/rightTrack":
                            value = 20000 + (int)((float)element.Args[0] * 100);
                            bt.SendString(value.ToString());
                            break;

                        case "/1/turret":
                            value = 30000 + (int)((float)element.Args[0] * 100);
                            bt.SendString(value.ToString());
                            break;

                        case "/1/stop":
                            value = 40000;
                            bt.SendString(value.ToString());
                            break;

                        default:
                            // ignore
                            break;
                        }
                        Console.WriteLine(value.ToString());
                    }
                }
            }), null, 0, 1);

            // timer starts immediately, ticks every 1ms
            Console.ReadKey();    // press any key to quit
        }