예제 #1
0
        public static void ReceiveEventData(ref TcpClient client, ref List <VREvent> inputEvents)
        {
            // Debug.Log("WaitForAndReceiveInputEvents");

            // 1. receive 1-byte message header
            ReceiveOneByteMessage(ref client, VRNet.INPUT_EVENTS_MSG);

            // 2. receive event data
            try {
                int    dataSize = ReadInt32(ref client);
                byte[] bytes    = new byte[dataSize];
                int    status   = ReceiveAll(ref client, ref bytes, dataSize);
                if (status == -1)
                {
                    Console.WriteLine("ReceiveEventData error reading data");
                    return;
                }

                using (MemoryStream ms = new MemoryStream(bytes)) {
                    using (BinaryReader br = new BinaryReader(ms)) {
                        int nEvents = br.ReadInt32();
                        for (int i = 0; i < nEvents; i++)
                        {
                            inputEvents.Add(VREvent.FromBinary(br));
                        }
                    }
                }
            }
            catch (Exception e) {
                Debug.Log("Exception: " + e);
                Console.WriteLine("Exception: {0}", e);
                BrokenConnectionError();
            }


            /**
             * // 2. receive int that tells us the size of the data portion of the message in bytes
             * Int32 dataSize = ReadInt32(ref client);
             *
             * // 3. receive dataSize bytes, then decode these as InputEvents
             * byte[] buf2 = new byte[dataSize + 1];
             * int status = ReceiveAll(ref client, ref buf2, dataSize);
             * if (status == -1) {
             *  Console.WriteLine("ReceiveEventData error reading data");
             *  return;
             * }
             * buf2[dataSize] = 0;
             *
             * // buf2 is the XML string that contains all the events.
             * string serializedQueue = System.Text.Encoding.UTF8.GetString(buf2);
             * //Debug.Log("Queue = " + serializedQueue);
             *
             * // Extract the VRDataQueue object
             * Dictionary<string, string> queueProps = new Dictionary<string, string>();
             * string queueContent = string.Empty;
             * string queueLeftover = string.Empty;
             * bool queueSuccess = XMLUtils.GetXMLField(serializedQueue, "VRDataQueue", ref queueProps, ref queueContent, ref queueLeftover);
             * if (!queueSuccess) {
             *  Debug.Log("Error decoding VRDataQueue");
             *  return;
             * }
             *
             * // The queue contents are VRDataItems, extract each one
             * int nItems = Convert.ToInt32(queueProps["num"]);
             *
             * //Debug.Log("Num = " + nItems);
             * //Debug.Log(queueContent);
             *
             * for (int i = 0; i < nItems; i++) {
             *  Dictionary<string, string> itemProps = new Dictionary<string, string>();
             *  string itemContent = string.Empty;
             *  string itemLeftover = string.Empty;
             *  bool itemSuccess = XMLUtils.GetXMLField(queueContent, "VRDataQueueItem", ref itemProps, ref itemContent, ref itemLeftover);
             *  if (!itemSuccess) {
             *      Debug.Log("Error decoding VRDataQueueItem #" + i);
             *      return;
             *  }
             *
             *  // Create a new VREvent from the content of this item
             *  //Debug.Log("Item Content = " + itemContent);
             *  VREvent e = new VREvent(ref itemContent);
             *  inputEvents.Add(e);
             *
             *  // Update the content to point to the next item if there is one
             *  queueContent = itemLeftover;
             * }
             **/
        }