Beispiel #1
0
        private void SendPings()
        {
            IWebSocketPacket lPacketPing = new WebSocketPacket(WebSocketFrameType.PING, WebSocketMessage.HELLO);

            lPacketPing.IsFragmented = true;

            try
            {
                Thread.Sleep(WebSocketConstants.DEFAULT_PING_DELAY);
                if (IsRunning)
                {
                    WebSocketTimeout.CallWithTimeout(SendPacket, WebSocketConstants.DEFAULT_PING_TIMEOUT, lPacketPing);
                }
            }
            catch (Exception lEx)
            {
                if (mLog.IsErrorEnabled)
                {
                    mLog.Error(WebSocketMessage.ERROR_WHILE_SENDING_PING);
                }
                OnError(new WebSocketError(WebSocketMessage.ERROR_WHILE_SENDING_PING));
                throw new WebSocketException(WebSocketMessage.ERROR_WHILE_SENDING_PING
                                             + WebSocketMessage.SEPARATOR + lEx);
            }
        }
Beispiel #2
0
 internal void SendPacketText(string aUTF8String, int aFragmentSize)
 {
     if (!aFragmentSize.Equals(-1))
     {
         if (aFragmentSize <= WebSocketConstants.DEFAULT_MAX_FRAME_SIZE)
         {
             SendFragmentsText(aUTF8String, aFragmentSize);
         }
         else
         {
             if (mLog.IsErrorEnabled)
             {
                 mLog.Error(WebSocketMessage.FRAGMENT_SIXE_EXCEED_MAX_SIZE);
             }
             OnError(new WebSocketError(WebSocketMessage.FRAGMENT_SIXE_EXCEED_MAX_SIZE));
             throw new WebSocketException(WebSocketMessage.FRAGMENT_SIXE_EXCEED_MAX_SIZE
                                          + WebSocketMessage.SEPARATOR + WebSocketConstants.DEFAULT_MAX_FRAME_SIZE);
         }
     }
     else
     {
         if (aUTF8String.Length > WebSocketConstants.DEFAULT_MAX_FRAME_SIZE)
         {
             SendFragmentsText(aUTF8String, WebSocketConstants.DEFAULT_MAX_FRAME_SIZE);
         }
         else
         {
             IWebSocketPacket lPacket = new WebSocketPacket(WebSocketFrameType.TEXT, aUTF8String);
             lPacket.IsFragmented = true;
             SendInternal(WebSocketProtocolAbstraction.RawToProtocolPacket(lPacket));
         }
     }
 }
Beispiel #3
0
 internal void SendPacketBinary(byte[] aBinaryData, int aFragmentSize)
 {
     if (!aFragmentSize.Equals(-1))
     {
         if (aFragmentSize <= WebSocketConstants.DEFAULT_MAX_FRAME_SIZE)
         {
             SendfragmentsBinary(aBinaryData, aFragmentSize);
         }
         else
         {
             if (mLog.IsErrorEnabled)
             {
                 mLog.Error(WebSocketMessage.FRAGMENT_SIXE_EXCEED_MAX_SIZE);
             }
             OnError(new WebSocketError(WebSocketMessage.FRAGMENT_SIXE_EXCEED_MAX_SIZE));
             throw new WebSocketException(WebSocketMessage.FRAGMENT_SIXE_EXCEED_MAX_SIZE
                                          + WebSocketMessage.SEPARATOR + WebSocketConstants.DEFAULT_MAX_FRAME_SIZE);
         }
     }
     else
     {
         if (aBinaryData.Length > WebSocketConstants.DEFAULT_MAX_FRAME_SIZE)
         {
             SendfragmentsBinary(aBinaryData, WebSocketConstants.DEFAULT_MAX_FRAME_SIZE);
         }
         else
         {
             IWebSocketPacket lPacket = new WebSocketPacket(WebSocketFrameType.BINARY, aBinaryData);
             lPacket.IsFragmented = true;
             SendInternal(WebSocketProtocolAbstraction.RawToProtocolPacket(lPacket));
         }
     }
 }
Beispiel #4
0
        internal void SendFragmentsText(string aUTF8String, int aFragmentSize)
        {
            StringBuilder lBuild           = new StringBuilder(aUTF8String);
            int           lBuildSize       = lBuild.Length;
            int           lFragmentCount   = CalcFragmentCount(lBuildSize, aFragmentSize);
            int           lCurrentFragment = 1;
            int           lPos             = 0;

            while (lPos < lBuildSize)
            {
                if (lPos + aFragmentSize > lBuildSize)
                {
                    aFragmentSize = lBuildSize - lPos;
                }

                IWebSocketPacket lPacket;
                if (lCurrentFragment.Equals(1))
                {
                    lPacket = new WebSocketPacket(WebSocketFrameType.TEXT,
                                                  lBuild.ToString(lPos, aFragmentSize));
                }
                else if (lCurrentFragment.Equals(lFragmentCount))
                {
                    lPacket = new WebSocketPacket(WebSocketFrameType.FRAGMENT,
                                                  lBuild.ToString(lPos, aFragmentSize));
                    lPacket.IsFragmented = true;
                }
                else
                {
                    lPacket = new WebSocketPacket(WebSocketFrameType.FRAGMENT,
                                                  lBuild.ToString(lPos, aFragmentSize));
                }

                SendInternal(WebSocketProtocolAbstraction.RawToProtocolPacket(lPacket));

                if (lPos + aFragmentSize > lBuildSize)
                {
                    lPos += lBuildSize - lPos;
                }
                else
                {
                    lPos += aFragmentSize;
                }
                lCurrentFragment++;
                Thread.Sleep((aFragmentSize * 1) / 100);
            }
        }
Beispiel #5
0
        internal void SendfragmentsBinary(byte[] aBinaryData, int aFragmentSize)
        {
            byte[] lNewBinaryData   = new byte[aFragmentSize];
            int    lBinaryDataSize  = aBinaryData.Length;
            int    lFragmentCount   = CalcFragmentCount(lBinaryDataSize, aFragmentSize);
            int    lCurrentFragment = 1;
            int    lPos             = 0;

            while (lPos < lBinaryDataSize)
            {
                if (lPos + aFragmentSize > lBinaryDataSize)
                {
                    aFragmentSize  = lBinaryDataSize - lPos;
                    lNewBinaryData = new byte[aFragmentSize];
                }
                Array.Copy(aBinaryData, lPos, lNewBinaryData, 0, aFragmentSize);
                IWebSocketPacket lPacket;
                if (lCurrentFragment.Equals(1))
                {
                    lPacket = new WebSocketPacket(WebSocketFrameType.BINARY, lNewBinaryData);
                }
                else if (lCurrentFragment.Equals(lFragmentCount))
                {
                    lPacket = new WebSocketPacket(WebSocketFrameType.FRAGMENT, lNewBinaryData);
                    lPacket.IsFragmented = true;
                }
                else
                {
                    lPacket = new WebSocketPacket(WebSocketFrameType.FRAGMENT, lNewBinaryData);
                }

                SendInternal(WebSocketProtocolAbstraction.RawToProtocolPacket(lPacket));

                if (lPos + aFragmentSize > lBinaryDataSize)
                {
                    lPos += lBinaryDataSize - lPos;
                }
                else
                {
                    lPos += aFragmentSize;
                }
                lCurrentFragment++;
                Thread.Sleep((aFragmentSize * 1) / 100);
            }
        }
Beispiel #6
0
 private void SendCloseHandshake()
 {
     try
     {
         IWebSocketPacket lPacket = new WebSocketPacket(WebSocketFrameType.CLOSE, WebSocketMessage.BYE);
         lPacket.IsFragmented = true;
         lock (mWriteLock)
             SendPacket(lPacket);
     }
     catch (IOException lIOEx)
     {
         if (mLog.IsErrorEnabled)
         {
             mLog.Error(WebSocketMessage.ERROR_WHILE_SENDING_CLOSE_HANDSHAKE
                        + WebSocketMessage.SEPARATOR + lIOEx.Message);
         }
         OnError(new WebSocketError(WebSocketMessage.ERROR_WHILE_SENDING_CLOSE_HANDSHAKE));
         throw new WebSocketException(WebSocketMessage.ERROR_WHILE_SENDING_CLOSE_HANDSHAKE
                                      + WebSocketMessage.SEPARATOR + lIOEx.Message);
     }
 }
        public static IWebSocketPacket protocolToRawPacket(NetworkStream aIS)
        {
            int lFlags = aIS.ReadByte();
            if (lFlags == -1)
                return null;

            byte[] lBuff = new byte[0];

            bool lFragmented = false;

            if ((lFlags & 0x80) == 0x00)
                lFragmented = true;

            bool lMasked = true;
            int[] lMask = new int[4];

            int lOpcode = lFlags & 0x0F;

            WebSocketFrameType lFrameType = (WebSocketFrameType)lOpcode;

            if (lFrameType.Equals(WebSocketFrameType.INVALID))
            {
                throw new WebSocketException(WebSocketMessage.INVALID_FRAME_TYPE);
            }
            else
            {
                long lPayloadLen = Read(aIS);

                lMasked = (lPayloadLen & 0x80) == 0x80;
                lPayloadLen &= 0x7F;

                if (lPayloadLen == 126)
                {
                    lPayloadLen = Read(aIS) & 0xFF;
                    lPayloadLen = (lPayloadLen << 8) ^ (Read(aIS) & 0xFF);
                }
                else if (lPayloadLen == 127)
                {

                    lPayloadLen = Read(aIS) & 0xFF;
                    lPayloadLen = (lPayloadLen << 8) ^ (Read(aIS) & 0xFF);
                    lPayloadLen = (lPayloadLen << 8) ^ (Read(aIS) & 0xFF);
                    lPayloadLen = (lPayloadLen << 8) ^ (Read(aIS) & 0xFF);
                    lPayloadLen = (lPayloadLen << 8) ^ (Read(aIS) & 0xFF);
                    lPayloadLen = (lPayloadLen << 8) ^ (Read(aIS) & 0xFF);
                    lPayloadLen = (lPayloadLen << 8) ^ (Read(aIS) & 0xFF);
                    lPayloadLen = (lPayloadLen << 8) ^ (Read(aIS) & 0xFF);
                }

                if (lMasked)
                {
                    lMask[0] = Read(aIS) & 0xFF;
                    lMask[1] = Read(aIS) & 0xFF;
                    lMask[2] = Read(aIS) & 0xFF;
                    lMask[3] = Read(aIS) & 0xFF;
                }

                if (lPayloadLen > 0)
                {
                    if (lMasked)
                    {
                        int j = 0;
                        while (lPayloadLen-- > 0)
                        {
                            WriteByte(ref lBuff, Convert.ToByte(Read(aIS) ^ lMask[j]));
                            j++;
                            j &= 3;
                        }
                    }
                    else
                    {
                        while (lPayloadLen-- > 0)
                        {
                            WriteByte(ref lBuff, (byte)Read(aIS));
                        }
                    }
                }
            }
            IWebSocketPacket lRes = new WebSocketPacket(lFrameType, lBuff);
            return lRes;
        }
Beispiel #8
0
        private void SendPings()
        {
            IWebSocketPacket lPacketPing = new WebSocketPacket(WebSocketFrameType.PING, WebSocketMessage.HELLO);
            lPacketPing.IsFragmented = true;

            try
            {
                Thread.Sleep(WebSocketConstants.DEFAULT_PING_DELAY);
                if (IsRunning)
                    WebSocketTimeout.CallWithTimeout(SendPacket, WebSocketConstants.DEFAULT_PING_TIMEOUT, lPacketPing);
            }
            catch (Exception lEx)
            {
                if (mLog.IsErrorEnabled)
                    mLog.Error(WebSocketMessage.ERROR_WHILE_SENDING_PING);
                OnError(new WebSocketError(WebSocketMessage.ERROR_WHILE_SENDING_PING));
                throw new WebSocketException(WebSocketMessage.ERROR_WHILE_SENDING_PING
                    + WebSocketMessage.SEPARATOR + lEx);
            }
        }
Beispiel #9
0
 private void SendCloseHandshake()
 {
     try
     {
         IWebSocketPacket lPacket = new WebSocketPacket(WebSocketFrameType.CLOSE, WebSocketMessage.BYE);
         lPacket.IsFragmented = true;
         lock (mWriteLock)
             SendPacket(lPacket);
     }
     catch (IOException lIOEx)
     {
         if (mLog.IsErrorEnabled)
             mLog.Error(WebSocketMessage.ERROR_WHILE_SENDING_CLOSE_HANDSHAKE
                 + WebSocketMessage.SEPARATOR + lIOEx.Message);
         OnError(new WebSocketError(WebSocketMessage.ERROR_WHILE_SENDING_CLOSE_HANDSHAKE));
         throw new WebSocketException(WebSocketMessage.ERROR_WHILE_SENDING_CLOSE_HANDSHAKE
             + WebSocketMessage.SEPARATOR + lIOEx.Message);
     }
 }
Beispiel #10
0
 internal void SendPacketText(string aUTF8String, int aFragmentSize)
 {
     if (!aFragmentSize.Equals(-1))
     {
         if (aFragmentSize <= WebSocketConstants.DEFAULT_MAX_FRAME_SIZE)
             SendFragmentsText(aUTF8String, aFragmentSize);
         else
         {
             if (mLog.IsErrorEnabled)
                 mLog.Error(WebSocketMessage.FRAGMENT_SIXE_EXCEED_MAX_SIZE);
             OnError(new WebSocketError(WebSocketMessage.FRAGMENT_SIXE_EXCEED_MAX_SIZE));
             throw new WebSocketException(WebSocketMessage.FRAGMENT_SIXE_EXCEED_MAX_SIZE
                 + WebSocketMessage.SEPARATOR + WebSocketConstants.DEFAULT_MAX_FRAME_SIZE);
         }
     }
     else
     {
         if (aUTF8String.Length > WebSocketConstants.DEFAULT_MAX_FRAME_SIZE)
             SendFragmentsText(aUTF8String, WebSocketConstants.DEFAULT_MAX_FRAME_SIZE);
         else
         {
             IWebSocketPacket lPacket = new WebSocketPacket(WebSocketFrameType.TEXT, aUTF8String);
             lPacket.IsFragmented = true;
             SendInternal(WebSocketProtocolAbstraction.RawToProtocolPacket(lPacket));
         }
     }
 }
Beispiel #11
0
 internal void SendPacketBinary(byte[] aBinaryData, int aFragmentSize)
 {
     if (!aFragmentSize.Equals(-1))
     {
         if (aFragmentSize <= WebSocketConstants.DEFAULT_MAX_FRAME_SIZE)
             SendfragmentsBinary(aBinaryData, aFragmentSize);
         else
         {
             if (mLog.IsErrorEnabled)
                 mLog.Error(WebSocketMessage.FRAGMENT_SIXE_EXCEED_MAX_SIZE);
             OnError(new WebSocketError(WebSocketMessage.FRAGMENT_SIXE_EXCEED_MAX_SIZE));
             throw new WebSocketException(WebSocketMessage.FRAGMENT_SIXE_EXCEED_MAX_SIZE
                 + WebSocketMessage.SEPARATOR + WebSocketConstants.DEFAULT_MAX_FRAME_SIZE);
         }
     }
     else
     {
         if (aBinaryData.Length > WebSocketConstants.DEFAULT_MAX_FRAME_SIZE)
             SendfragmentsBinary(aBinaryData, WebSocketConstants.DEFAULT_MAX_FRAME_SIZE);
         else
         {
             IWebSocketPacket lPacket = new WebSocketPacket(WebSocketFrameType.BINARY, aBinaryData);
             lPacket.IsFragmented = true;
             SendInternal(WebSocketProtocolAbstraction.RawToProtocolPacket(lPacket));
         }
     }
 }
Beispiel #12
0
        internal void SendFragmentsText(string aUTF8String, int aFragmentSize)
        {
            StringBuilder lBuild = new StringBuilder(aUTF8String);
            int lBuildSize = lBuild.Length;
            int lFragmentCount = CalcFragmentCount(lBuildSize, aFragmentSize);
            int lCurrentFragment = 1;
            int lPos = 0;

            while (lPos < lBuildSize)
            {
                if (lPos + aFragmentSize > lBuildSize)
                    aFragmentSize = lBuildSize - lPos;

                IWebSocketPacket lPacket;
                if (lCurrentFragment.Equals(1))
                    lPacket = new WebSocketPacket(WebSocketFrameType.TEXT,
                            lBuild.ToString(lPos, aFragmentSize));
                else if (lCurrentFragment.Equals(lFragmentCount))
                {
                    lPacket = new WebSocketPacket(WebSocketFrameType.FRAGMENT,
                           lBuild.ToString(lPos, aFragmentSize));
                    lPacket.IsFragmented = true;
                }
                else
                    lPacket = new WebSocketPacket(WebSocketFrameType.FRAGMENT,
                            lBuild.ToString(lPos, aFragmentSize));

                SendInternal(WebSocketProtocolAbstraction.RawToProtocolPacket(lPacket));

                if (lPos + aFragmentSize > lBuildSize)
                    lPos += lBuildSize - lPos;
                else
                    lPos += aFragmentSize;
                lCurrentFragment++;
                Thread.Sleep((aFragmentSize * 1) / 100);
            }
        }
Beispiel #13
0
        internal void SendfragmentsBinary(byte[] aBinaryData, int aFragmentSize)
        {
            byte[] lNewBinaryData = new byte[aFragmentSize];
            int lBinaryDataSize = aBinaryData.Length;
            int lFragmentCount = CalcFragmentCount(lBinaryDataSize, aFragmentSize);
            int lCurrentFragment = 1;
            int lPos = 0;

            while (lPos < lBinaryDataSize)
            {
                if (lPos + aFragmentSize > lBinaryDataSize)
                {
                    aFragmentSize = lBinaryDataSize - lPos;
                    lNewBinaryData = new byte[aFragmentSize];
                }
                Array.Copy(aBinaryData, lPos, lNewBinaryData, 0, aFragmentSize);
                IWebSocketPacket lPacket;
                if (lCurrentFragment.Equals(1))
                    lPacket = new WebSocketPacket(WebSocketFrameType.BINARY, lNewBinaryData);
                else if (lCurrentFragment.Equals(lFragmentCount))
                {
                    lPacket = new WebSocketPacket(WebSocketFrameType.FRAGMENT, lNewBinaryData);
                    lPacket.IsFragmented = true;
                }
                else
                    lPacket = new WebSocketPacket(WebSocketFrameType.FRAGMENT, lNewBinaryData);

                SendInternal(WebSocketProtocolAbstraction.RawToProtocolPacket(lPacket));

                if (lPos + aFragmentSize > lBinaryDataSize)
                    lPos += lBinaryDataSize - lPos;
                else
                    lPos += aFragmentSize;
                lCurrentFragment++;
                Thread.Sleep((aFragmentSize * 1) / 100);
            }
        }