/* * Turns BSONObj into an array of bytes ready to transport. * --------------------------------------------------------- * | a | b | c | d | * --------------------------------------------------------- * d => compressed data * c => length of uncompressed data * b => length of c + compressed data (aka compressed data + 4) * a => 1 byte header * */ public void Send(Kernys.Bson.BSONObject bsonObj) { byte[] raw = Kernys.Bson.SimpleBSON.Dump(bsonObj); byte[] compressed = ZlibStream.CompressBuffer(raw); List <byte> c = intToByteString(raw.Length); c.AddRange(compressed); List <byte> b = intToByteString(c.Count); b.AddRange(c); List <byte> a = new List <byte>(); a.Add(3); a.AddRange(b); // string deb = ""; // foreach (byte oneB in a) { // deb += oneB + " "; // } // Debug.Log (deb); tcpSender.AddMessage(a.ToArray()); }
public void ParseBSON(Kernys.Bson.BSONObject bsonObj) { try { if (bsonObj["ipad"]["type"].stringValue == "up") { if (lastMessage == messageType.down) { //its a tap! if (lastFingers == 1) { messageQueue.Enqueue(new iPadTapInput()); } else { //double tap here } } lastMessage = messageType.up; this.lastFingers = 0; } else if (bsonObj["ipad"]["type"].stringValue == "down") { this.lastFingers = bsonObj["ipad"]["n"].int32Value; lastMessage = messageType.down; } else { this.lastMessage = messageType.move; } } catch (KeyNotFoundException) { //unhandled keynotfoundexception. e.g. recieving letter inputs. } //TODO: Parse other types of messages, e.g. swipe, fingerdown, finger up... }
// Update is called once per frame void Update() { //byte[] msg; bsonObj = null; /* Get the first incoming message */ if (bsonListener != null) { bsonObj = bsonListener.Receive(); } while (bsonObj != null) { this.bsonParser.ParseBSON(bsonObj); try { //Debug.Log (bsonObj["ipad"]["type"].stringValue); if (bsonObj["ipad"]["type"].stringValue == "up") { gameObject.renderer.material.color = Color.red; } else if (bsonObj["ipad"]["type"].stringValue == "down") { gameObject.renderer.material.color = Color.green; } else if (bsonObj["ipad"]["type"].stringValue == "move") { //Debug.Log ("MOVE " + bsonObj["ipad"]["dist"][0]); float dx = (float)bsonObj["ipad"]["dist"][0]; float dy = (float)bsonObj["ipad"]["dist"][1] * -1.0f; float newX; float newY; newX = gameObject.transform.position.x + (dx * xSensitivity); newY = gameObject.transform.position.y + (dy * ySensitivity); if (constrainX) { if (wrapX) { newX = nfmod(newX, 1.0f); } else { newX = Mathf.Clamp(newX, 0.0f, 1.0f); } } if (constrainY) { if (wrapY) { newY = nfmod(newY, 1.0f); } else { newY = Mathf.Clamp(newY, 0.0f, 1.0f); } } gameObject.transform.position = new Vector3(newX, newY, 0); } } catch (KeyNotFoundException) { //unimplemented message. } /* Get the next message */ bsonObj = bsonListener.Receive(); } #if NO_CLUSTER while (this.bsonParser.messageQueue.Count > 0) { IiPadInput ipi = this.bsonParser.messageQueue.Dequeue(); if (ipi.inputType == InputType.Tap && this.eh != null) { this.eh.handleTap(); } } #else if (icCluster.isMaster()) { this.iPadTapped.SetValue(false); //always set to false at end of frame. } if (icCluster.isMaster()) { while (this.bsonParser.messageQueue.Count > 0) { IiPadInput ipi = this.bsonParser.messageQueue.Dequeue(); if (ipi.inputType == InputType.Tap && this.eh != null) { this.iPadTapped.SetValue(true); } } } bool tapped = this.iPadTapped.GetValue(); if (tapped) { this.eh.handleTap(); } #endif }
/* * Grabs a byte[] from the listener and turns it into a BSONObject * * Returns null if no messages * * --------------------------------------------------------- * | a | b | c | d | * --------------------------------------------------------- * d => compressed data * c => length of uncompressed data * b => length of c + compressed data (aka compressed data + 4) * a => 1 byte header * */ public Kernys.Bson.BSONObject Receive() { if (mTcpListener.GetMessageCount() == 0) { return(null); } else { try { byte a; int b; //int c; byte [] compressedData; byte [] uncompressedData; int offset; if (!mPartwayThroughMessage) { mMsgOffset = 0; mMsg = mTcpListener.GetMessage(); } //Debug.Log ("Got message"); a = mMsg[mMsgOffset]; b = getInt(mMsg, mMsgOffset + 1); //c = getInt(msg, 5); if ((a & HEADER_BIT_COMPRESS) != 0) { //Debug.Log("Compressed " + b); compressedData = new byte [b - 4]; Array.Copy(mMsg, mMsgOffset + 9, compressedData, 0, b - 4); uncompressedData = ZlibStream.UncompressBuffer(compressedData); } else { //Debug.Log("Not Compressed"); uncompressedData = new byte [b]; Array.Copy(mMsg, mMsgOffset + 5, uncompressedData, 0, b); //Debug.Log("msg len : " + b + ", " + mMsg.Length + ", " + mMsgOffset); } Kernys.Bson.BSONObject obj = Kernys.Bson.SimpleBSON.Load(uncompressedData); /* The current message is (b + 5) bytes long */ if ((mMsgOffset + b + 5) < mMsg.Length) { mPartwayThroughMessage = true; mMsgOffset += b + 5; } else { mPartwayThroughMessage = false; mMsgOffset = 0; } return(obj); } catch { Debug.Log("ERROR parsing BSON packets - will skip this packet"); mPartwayThroughMessage = false; mMsgOffset = 0; return(null); } } }
// Update is called once per frame void Update() { //byte[] msg; bsonObj = null; /* Get the first incoming message */ if (bsonListener != null) bsonObj = bsonListener.Receive(); while (bsonObj != null) { this.bsonParser.ParseBSON(bsonObj); try { //Debug.Log (bsonObj["ipad"]["type"].stringValue); if (bsonObj["ipad"]["type"].stringValue == "up") gameObject.renderer.material.color = Color.red; else if (bsonObj["ipad"]["type"].stringValue == "down") gameObject.renderer.material.color = Color.green; else if (bsonObj["ipad"]["type"].stringValue == "move") { //Debug.Log ("MOVE " + bsonObj["ipad"]["dist"][0]); float dx = (float)bsonObj["ipad"]["dist"][0]; float dy = (float)bsonObj["ipad"]["dist"][1] * -1.0f; float newX; float newY; newX = gameObject.transform.position.x + (dx * xSensitivity); newY = gameObject.transform.position.y + (dy * ySensitivity); if (constrainX) { if (wrapX) newX = nfmod(newX, 1.0f); else newX = Mathf.Clamp(newX, 0.0f, 1.0f); } if (constrainY) { if (wrapY) newY = nfmod(newY, 1.0f); else newY = Mathf.Clamp(newY, 0.0f, 1.0f); } gameObject.transform.position = new Vector3(newX, newY, 0); } } catch (KeyNotFoundException) { //unimplemented message. } /* Get the next message */ bsonObj = bsonListener.Receive(); } #if NO_CLUSTER while (this.bsonParser.messageQueue.Count > 0) { IiPadInput ipi = this.bsonParser.messageQueue.Dequeue(); if (ipi.inputType == InputType.Tap && this.eh != null) { this.eh.handleTap(); } } #else if (icCluster.isMaster()) { this.iPadTapped.SetValue(false); //always set to false at end of frame. } if (icCluster.isMaster()) { while (this.bsonParser.messageQueue.Count > 0) { IiPadInput ipi = this.bsonParser.messageQueue.Dequeue(); if (ipi.inputType == InputType.Tap && this.eh != null) { this.iPadTapped.SetValue(true); } } } bool tapped = this.iPadTapped.GetValue(); if (tapped) { this.eh.handleTap(); } #endif }