Пример #1
0
 /// <summary>
 /// websocket接收帧回调
 /// </summary>
 /// <param name="frame"></param>
 protected abstract void ReceiveFrameCallback(WebSocketFrame frame);
Пример #2
0
        /// <summary>
        /// 会话线程
        /// </summary>
        /// <param name="item"></param>
        private void SessionThread(object item)
        {
            try
            {
                //ssl握手和websocket握手
                if (this.m_IsSSL)
                {
                    SslStream sslStream = m_Stream as SslStream;
                    if (sslStream == null)
                    {
                        throw new Exception("sslstream is error");
                    }
                    sslStream.AuthenticateAsServer(m_ServerCertificate, false, SslProtocols.Tls, true);
                }

                if (!m_IsHandshake)
                {
                    if (!handshake(m_Stream))
                    {
                        this.Close(CloseReason.Exception);
                        return;
                    }
                }

                //正式接收数据
                while (!this.m_CancelTokenSource.IsCancellationRequested)
                {
                    WebSocketFrame frame = null;
                    try
                    {
                        frame = WebSocketFrame.GetFrameFromStream(m_Stream);
                    }
                    catch
                    {
                        continue;
                    }
                    if (frame == null)
                    {
                        continue;
                    }

                    try
                    {
                        ReceiveFrameCallback(frame);
                    }
                    catch (Exception ex)
                    {
                        Trace.Error("frame处理失败", ex);
                        continue;
                    }

                    //if (frame.IsString)
                    //{
                    //    string content = Encoding.UTF8.GetString(frame.Payload_data.Array, frame.Payload_data.Offset, frame.Payload_data.Count);
                    //    Console.WriteLine("接收数据:" + content);
                    //    SendFrame("is ok all data is receive");
                    //    return;
                    //}
                    //else
                    //{
                    //    Trace.Error("收到未知消息" + frame.ToString());
                    //}
                }
            }
            catch (Exception ex)
            {
                Trace.Error("websocket ssion 中的未知错误", ex);
            }
            this.Close(CloseReason.Exception);
        }
Пример #3
0
        /// <summary>
        /// 得到请求模型 lhg
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public static WebSocketFrame GetFrameFromStream(Stream stream)
        {
            WebSocketFrame frame = new WebSocketFrame();

            byte[] datas     = new byte[1024];
            int    _RecCount = 0;

            _RecCount = LoopReadFormStream(stream, datas, 0, 2);
            if (_RecCount != 2)
            {
                //协议错误
                return(null);
            }
            int i = 0;

            frame.Fin = (byte)(datas[i] >> 7);

            byte opcode_v = (byte)(datas[i++] & 0x0F);

            try
            {
                frame.Opcode = (Opcode)opcode_v;
            }
            catch
            {
                return(null);
            }



            frame.Mask        = (byte)(datas[i] >> 7);
            frame.Payload_len = (byte)(datas[i++] & 0x7F);
            i = 0;

            if (frame.Payload_len < 126)
            {
                frame.FrameLength = frame.Payload_len;
            }
            else
            {
                if (frame.Payload_len == 126)
                {
                    _RecCount = LoopReadFormStream(stream, datas, 0, 2);
                    if (_RecCount != 2)
                    {
                        return(null);
                    }
                    i = 0;
                    frame.FrameLength = datas[i++] << 8 + datas[i++];
                }
                else if (frame.Payload_len == 127)
                {
                    _RecCount = LoopReadFormStream(stream, datas, 0, 8);
                    if (_RecCount != 8)
                    {
                        return(null);
                    }
                    i = 4;
                    frame.FrameLength = (datas[i++] << 24) + (datas[i++] << 16) + (datas[i++] << 8) + datas[i++];
                }
            }

            if (frame.Mask == 1)
            {
                _RecCount = LoopReadFormStream(stream, datas, 0, 4);
                if (_RecCount != 4)
                {
                    return(null);
                }
                i = 0;

                frame.Masking_key = new byte[] { datas[i++], datas[i++], datas[i++], datas[i++] };
            }

            byte[] tempd = new byte[frame.FrameLength];
            _RecCount = LoopReadFormStream(stream, tempd, 0, frame.FrameLength);
            if (_RecCount != frame.FrameLength)
            {
                return(null);
            }

            if (frame.Mask == 1)
            {
                for (int j = 0; j < frame.FrameLength; j++)
                {
                    tempd[j] = (byte)(tempd[j] ^ frame.Masking_key[j % 4]);
                }
            }
            frame.Payload_data = new ArraySegment <byte>(tempd, 0, frame.FrameLength);
            return(frame);
        }