public Token PacketToToken(WebSocketPacket aPacket)
 {
     var byteArray = aPacket.GetByteArray();
     var packetString = WebSocketConvert.BytesToString(byteArray, WebSocketTypeEncoding.UTF8);
     var dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(packetString);
     var dictionaryToken = new DictionaryToken();
     dictionaryToken.SetDictionary(dictionary);
     return dictionaryToken;
 }
Example #2
0
 public Token PacketToToken(WebSocketPacket aPacket)
 {
     //DictionaryToken lDicToken = new DictionaryToken();
     //JavaScriptSerializer lJavaScriptSerializer = new JavaScriptSerializer();
     //Dictionary<string, object> lDic = lJavaScriptSerializer.Deserialize<Dictionary<string, object>>(
     //    WebSocketConvert.BytesToString(aPacket.GetByteArray(), WebSocketTypeEncoding.ASCII));
     //lDicToken.SetDictionary(lDic);
     //return lDicToken;
     throw new Exception("Not implemented yet");
 }
Example #3
0
        public static void CallWithTimeout(Action<WebSocketPacket> aAction, int aTimeoutMilliseconds, WebSocketPacket lPacket)
        {
            Thread lThreadToKill = null;
            Action lWrappedAction = () =>
            {
                lThreadToKill = Thread.CurrentThread;
                aAction(lPacket);
            };

            IAsyncResult lResult = lWrappedAction.BeginInvoke(null, null);
            if (lResult.AsyncWaitHandle.WaitOne(aTimeoutMilliseconds))
            {
                lWrappedAction.EndInvoke(lResult);
            }
            else
            {
                lThreadToKill.Abort();
                throw new TimeoutException("Timeout");
            }
        }
        internal static byte[] RawToProtocolPacket(WebSocketPacket aDataPacket)
        {
            byte[] lBuff = new byte[2];
            WebSocketFrameType lFrameType = aDataPacket.GetFrameType();
            int lTargetType = (int)lFrameType;

            if (lTargetType.Equals(-1))
                throw new WebSocketRuntimeException("Cannot construct a packet with unknown packet type: " + lFrameType);

            if (aDataPacket.IsEndFrame())
                lBuff[0] = (byte)(lTargetType | 0x80);
            else
                lBuff[0] = (byte)(lTargetType);

            int lPayloadLen = aDataPacket.GetByteArray().Length;

            if (lPayloadLen < 126)
                lBuff[1] = (byte)(lPayloadLen | 0x80);
            else if (lPayloadLen >= 126 && lPayloadLen < 0xFFFF)
            {
                lBuff[1] = (byte)(126 | 0x80);
                int lSize = lBuff.Length;
                lBuff = CopyOf(lBuff, lSize + 2);
                lBuff[lSize] = (byte)((lPayloadLen >> 8) & 0xFF);
                lBuff[lSize + 1] = (byte)(lPayloadLen & 0xFF);
            }
            else if (lPayloadLen >= 0xFFFF)
            {
                lBuff[1] = (byte)(127 | 0x80);
                long lLen = (long)lPayloadLen;
                int lSize = lBuff.Length;
                lBuff = CopyOf(lBuff, lSize + 8);
                lBuff[lSize] = (byte)(lLen >> 56);
                lBuff[lSize + 1] = (byte)(lLen >> 48);
                lBuff[lSize + 2] = (byte)(lLen >> 40);
                lBuff[lSize + 3] = (byte)(lLen >> 32);
                lBuff[lSize + 4] = (byte)(lLen >> 24);
                lBuff[lSize + 5] = (byte)(lLen >> 16);
                lBuff[lSize + 6] = (byte)(lLen >> 8);
                lBuff[lSize + 7] = (byte)lLen;
            }

            int lSizes = lBuff.Length;
            lBuff = CopyOf(lBuff, lSizes + 4);
            lBuff[lSizes] = (byte)mRan.Next(0, 255);
            lBuff[lSizes + 1] = (byte)mRan.Next(0, 255);
            lBuff[lSizes + 2] = (byte)mRan.Next(0, 255);
            lBuff[lSizes + 3] = (byte)mRan.Next(0, 255);

            byte[] lMask = new byte[4];
            Array.Copy(lBuff, lBuff.Length - 4, lMask, 0, 4);

            byte[] lBuffData = new byte[aDataPacket.GetByteArray().Length];
            Array.Copy(aDataPacket.GetByteArray(), 0, lBuffData, 0, lBuffData.Length);
            byte[] lMaskedData = new byte[lBuffData.Length];
            int lPos = 0;
            for (int i = 0; i < lBuffData.Length; i++)
            {
                lMaskedData[i] = (byte)Convert.ToInt32(lBuffData[i] ^ lMask[lPos]);
                if (lPos == 3)
                    lPos = 0;
                else
                    lPos++;
            }

            lBuff = CopyOf(lBuff, lBuff.Length + lMaskedData.Length);
            Array.Copy(lMaskedData, 0, lBuff, lBuff.Length - lMaskedData.Length, lMaskedData.Length);
            return lBuff;
        }
Example #5
0
 public Token PacketToToken(WebSocketPacket aPacket)
 {
     throw new Exception("Not implemented yet");
 }