public override StringPackageInfo ResolvePackage(IList <ArraySegment <byte> > packageData)
        {
            var session       = AppContext.CurrentSession;
            var bufferManager = session.AppServer.BufferManager;
            var context       = WebSocketContext.Get(session);

            var responseBuilder = new StringBuilder();

            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseHeadLine00);
            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseUpgradeLine);
            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseConnectionLine);

            if (!string.IsNullOrEmpty(context.Origin))
            {
                responseBuilder.AppendFormatWithCrCf(WebSocketConstant.ResponseOriginLine, context.Origin);
            }

            responseBuilder.AppendFormatWithCrCf(WebSocketConstant.ResponseLocationLine, context.UriScheme, context.Host, context.Path);

            var subProtocol = context.GetAvailableSubProtocol();

            if (!string.IsNullOrEmpty(subProtocol))
            {
                responseBuilder.AppendFormatWithCrCf(WebSocketConstant.ResponseProtocolLine, subProtocol);
            }

            responseBuilder.AppendWithCrCf();
            var response = responseBuilder.ToString();
            var encoding = Encoding.UTF8;
            var data     = bufferManager.GetBuffer(encoding.GetMaxByteCount(response.Length));
            var length   = encoding.GetBytes(response, 0, response.Length, data, 0);

            var secKey1 = context.HandshakeRequest.Get(WebSocketConstant.SecWebSocketKey1);
            var secKey2 = context.HandshakeRequest.Get(WebSocketConstant.SecWebSocketKey2);

            byte[] secret;

            if (packageData.Count == 1)
            {
                secret = GetResponseSecurityKey(secKey1, secKey2, packageData[0]);
            }
            else
            {
                var secKey3 = bufferManager.GetBuffer(8);
                try
                {
                    secret = GetResponseSecurityKey(secKey1, secKey2, new ArraySegment <byte>(secKey3));
                }
                finally
                {
                    bufferManager.ReturnBuffer(secKey3);
                }
            }

            session.SocketSession.TrySend(new ArraySegment <byte>(secret));
            NextReceiveFilter = new DraftHybi00DataReceiveFilter();
            return(null);
        }
Exemple #2
0
        public override WebSocketPackageInfo ResolvePackage(IList <ArraySegment <byte> > packageData)
        {
            var context       = Context;
            var bufferManager = context.BufferManager;

            var responseBuilder = new StringBuilder();

            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseHeadLine00);
            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseUpgradeLine);
            responseBuilder.AppendWithCrCf(WebSocketConstant.ResponseConnectionLine);

            if (!string.IsNullOrEmpty(context.Origin))
            {
                responseBuilder.AppendFormatWithCrCf(WebSocketConstant.ResponseOriginLine, context.Origin);
            }

            responseBuilder.AppendFormatWithCrCf(WebSocketConstant.ResponseLocationLine, context.UriScheme, context.Host, context.Path);


            ///TODO: get available sub protocols
            //var subProtocol = context.GetAvailableSubProtocol();

            //if (!string.IsNullOrEmpty(subProtocol))
            //  responseBuilder.AppendFormatWithCrCf(WebSocketConstant.ResponseProtocolLine, subProtocol);

            responseBuilder.AppendWithCrCf();
            var response = responseBuilder.ToString();
            var encoding = Encoding.UTF8;

            byte[] data = encoding.GetBytes(response);

            context.Channel.Send(new ArraySegment <byte>(data));

            var secKey1 = context.HandshakeRequest.Get(WebSocketConstant.SecWebSocketKey1);
            var secKey2 = context.HandshakeRequest.Get(WebSocketConstant.SecWebSocketKey2);

            byte[] secret;

            using (var stream = this.GetBufferStream(packageData))
            {
                var secKey3 = bufferManager.GetBuffer(c_Key3Len);

                try
                {
                    stream.Read(secKey3, 0, c_Key3Len);
                    secret = GetResponseSecurityKey(secKey1, secKey2, new ArraySegment <byte>(secKey3, 0, c_Key3Len));
                }
                finally
                {
                    bufferManager.ReturnBuffer(secKey3);
                }
            }

            context.Channel.Send(new ArraySegment <byte>(secret));
            NextReceiveFilter = new DraftHybi00DataReceiveFilter(context);
            return(null);
        }