/// <summary>
        /// Retrieves an event from the js library, handles it internally and then adds it to a queue for delivery to the user.
        /// </summary>
        /// <param name="evt"> The new network event or an empty struct if none is found.</param>
        /// <returns>True if event found, false if no events queued.</returns>
        private bool DequeueInternal(out NetworkEvent evt)
        {
            int length = UnityWebRtcNetworkPeekEventDataLength(mReference);

            if (length == -1) //-1 == no event available
            {
                evt = new NetworkEvent();
                return(false);
            }
            else
            {
                ByteArrayBuffer buf        = ByteArrayBuffer.Get(length);
                bool            eventFound = UnityWebRtcNetworkDequeue(mReference, mTypeidBuffer, mConidBuffer, buf.array, 0, buf.array.Length, mDataWrittenLenBuffer);
                //set the write correctly
                buf.PositionWriteRelative = mDataWrittenLenBuffer[0];

                NetEventType type = (NetEventType)mTypeidBuffer[0];
                ConnectionId id;
                id.id = (short)mConidBuffer[0];
                object data = null;

                if (buf.PositionWriteRelative == 0 || buf.PositionWriteRelative == -1) //no data
                {
                    data = null;
                    //was an empty buffer -> release it and
                    buf.Dispose();
                }
                else if (type == NetEventType.ReliableMessageReceived || type == NetEventType.UnreliableMessageReceived)
                {
                    //store the data for the user to use
                    data = buf;
                }
                else
                {
                    //non data message with data attached -> can only be a string
                    string stringData = Encoding.ASCII.GetString(buf.array, 0, buf.PositionWriteRelative);
                    data = stringData;
                    buf.Dispose();
                }


                evt = new NetworkEvent(type, id, data);
                UnityEngine.Debug.Log("event" + type + " received");
                HandleEventInternally(ref evt);
                return(eventFound);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Retrieves an event from the js library, handles it internally and then adds it to a queue for delivery to the user.
        /// </summary>
        /// <param name="evt"> The new network event or an empty struct if none is found.</param>
        /// <returns>True if event found, false if no events queued.</returns>
        private bool DequeueInternal(out NetworkEvent evt)
        {
            int length = CAPI.Unity_WebRtcNetwork_PeekEventDataLength(mReference);

            if (length == -1) //-1 == no event available
            {
                evt = new NetworkEvent();
                return(false);
            }
            else
            {
                ByteArrayBuffer buf        = ByteArrayBuffer.Get(length);
                bool            eventFound = CAPI.Unity_WebRtcNetwork_Dequeue(mReference, mTypeidBuffer, mConidBuffer, buf.array, 0, buf.array.Length, mDataWrittenLenBuffer);
                //set the write correctly
                buf.PositionWriteRelative = mDataWrittenLenBuffer[0];

                NetEventType type = (NetEventType)mTypeidBuffer[0];
                ConnectionId id;
                id.id = (short)mConidBuffer[0];

                //TODO: add a way to move error information from java script to Unity for
                //NetworkEvent.ErrorInfo
                if (buf.PositionWriteRelative == 0 || buf.PositionWriteRelative == -1) //no data
                {
                    //was an empty buffer -> release it and
                    buf.Dispose();
                    evt = new NetworkEvent(type, id);
                }
                else if (type == NetEventType.ReliableMessageReceived || type == NetEventType.UnreliableMessageReceived)
                {
                    evt = new NetworkEvent(type, id, buf);
                }
                else
                {
                    //non data message with data attached -> can only be a string
                    string stringData = Encoding.ASCII.GetString(buf.array, 0, buf.PositionWriteRelative);
                    evt = new NetworkEvent(type, id, stringData);
                    buf.Dispose();
                }


                UnityEngine.Debug.Log("event" + type + " received");
                HandleEventInternally(ref evt);
                return(eventFound);
            }
        }