internal static string GetSupportedVersion()
        {
            if (!IsSupported)
            {
                HttpWebSocket.ThrowPlatformNotSupportedException_WSPC();
            }

            SafeWebSocketHandle?webSocketHandle = null;

            try
            {
                int errorCode = Interop.WebSocket.WebSocketCreateClientHandle(null !, 0, out webSocketHandle);
                ThrowOnError(errorCode);

                if (webSocketHandle == null ||
                    webSocketHandle.IsInvalid)
                {
                    HttpWebSocket.ThrowPlatformNotSupportedException_WSPC();
                }

                IntPtr additionalHeadersPtr;
                uint   additionalHeaderCount;

                errorCode = Interop.WebSocket.WebSocketBeginClientHandshake(webSocketHandle !,
                                                                            IntPtr.Zero,
                                                                            0,
                                                                            IntPtr.Zero,
                                                                            0,
                                                                            s_initialClientRequestHeaders,
                                                                            (uint)s_initialClientRequestHeaders.Length,
                                                                            out additionalHeadersPtr,
                                                                            out additionalHeaderCount);
                ThrowOnError(errorCode);

                Interop.WebSocket.HttpHeader[] additionalHeaders = MarshalHttpHeaders(additionalHeadersPtr, (int)additionalHeaderCount);

                string?version = null;
                foreach (Interop.WebSocket.HttpHeader header in additionalHeaders)
                {
                    if (string.Equals(header.Name,
                                      HttpKnownHeaderNames.SecWebSocketVersion,
                                      StringComparison.OrdinalIgnoreCase))
                    {
                        version = header.Value;
                        break;
                    }
                }
                Debug.Assert(version != null, "'version' MUST NOT be NULL.");

                return(version);
            }
            finally
            {
                if (webSocketHandle != null)
                {
                    webSocketHandle.Dispose();
                }
            }
        }
Esempio n. 2
0
        internal static void WebSocketCreateServerHandle(Interop.WebSocket.Property[] properties,
                                                         int propertyCount,
                                                         out SafeWebSocketHandle webSocketHandle)
        {
            Debug.Assert(propertyCount >= 0, "'propertyCount' MUST NOT be negative.");
            Debug.Assert((properties == null && propertyCount == 0) ||
                         (properties != null && propertyCount == properties.Length),
                         "'propertyCount' MUST MATCH 'properties.Length'.");

            if (!IsSupported)
            {
                HttpWebSocket.ThrowPlatformNotSupportedException_WSPC();
            }

            int errorCode = Interop.WebSocket.WebSocketCreateServerHandle(properties, (uint)propertyCount, out webSocketHandle);

            ThrowOnError(errorCode);

            if (webSocketHandle == null ||
                webSocketHandle.IsInvalid)
            {
                HttpWebSocket.ThrowPlatformNotSupportedException_WSPC();
            }

            IntPtr responseHeadersPtr;
            uint   responseHeaderCount;

            // Currently the WSPC doesn't allow to initiate a data session
            // without also being involved in the http handshake
            // There is no information whatsoever, which is needed by the
            // WSPC for parsing WebSocket frames from the HTTP handshake
            // In the managed implementation the HTTP header handling
            // will be done using the managed HTTP stack and we will
            // just fake an HTTP handshake for the WSPC calling
            // WebSocketBeginServerHandshake and WebSocketEndServerHandshake
            // with statically defined dummy headers.
            errorCode = Interop.WebSocket.WebSocketBeginServerHandshake(webSocketHandle,
                                                                        IntPtr.Zero,
                                                                        IntPtr.Zero,
                                                                        0,
                                                                        s_ServerFakeRequestHeaders,
                                                                        (uint)s_ServerFakeRequestHeaders.Length,
                                                                        out responseHeadersPtr,
                                                                        out responseHeaderCount);

            ThrowOnError(errorCode);

            Interop.WebSocket.HttpHeader[] responseHeaders = MarshalHttpHeaders(responseHeadersPtr, (int)responseHeaderCount);
            errorCode = Interop.WebSocket.WebSocketEndServerHandshake(webSocketHandle);

            ThrowOnError(errorCode);

            Debug.Assert(webSocketHandle != null, "'webSocketHandle' MUST NOT be NULL at this point.");
        }
Esempio n. 3
0
        internal static SafeWebSocketHandle WebSocketCreateServerHandle(Interop.WebSocket.Property[] properties, int propertyCount)
        {
            Debug.Assert(propertyCount >= 0, "'propertyCount' MUST NOT be negative.");
            Debug.Assert((properties == null && propertyCount == 0) ||
                         (properties != null && propertyCount == properties.Length),
                         "'propertyCount' MUST MATCH 'properties.Length'.");

            if (!IsSupported)
            {
                HttpWebSocket.ThrowPlatformNotSupportedException_WSPC();
            }

            SafeWebSocketHandle?webSocketHandle = null;

            try
            {
                int errorCode = Interop.WebSocket.WebSocketCreateServerHandle(properties !, (uint)propertyCount, out webSocketHandle);
                ThrowOnError(errorCode);
                if (webSocketHandle.IsInvalid)
                {
                    HttpWebSocket.ThrowPlatformNotSupportedException_WSPC();
                }

                // Currently the WSPC doesn't allow to initiate a data session
                // without also being involved in the http handshake
                // There is no information whatsoever, which is needed by the
                // WSPC for parsing WebSocket frames from the HTTP handshake
                // In the managed implementation the HTTP header handling
                // will be done using the managed HTTP stack and we will
                // just fake an HTTP handshake for the WSPC calling
                // WebSocketBeginServerHandshake and WebSocketEndServerHandshake
                // with statically defined dummy headers.
                errorCode = Interop.WebSocket.WebSocketBeginServerHandshake(webSocketHandle,
                                                                            IntPtr.Zero,
                                                                            IntPtr.Zero,
                                                                            0,
                                                                            s_ServerFakeRequestHeaders !,
                                                                            (uint)s_ServerFakeRequestHeaders !.Length,
                                                                            out _,
                                                                            out _);
                ThrowOnError(errorCode);

                errorCode = Interop.WebSocket.WebSocketEndServerHandshake(webSocketHandle);
                ThrowOnError(errorCode);
            }
            catch
            {
                webSocketHandle?.Dispose();
                throw;
            }

            return(webSocketHandle);
        }
Esempio n. 4
0
        public ServerWebSocket(Stream innerStream,
                               string subProtocol,
                               int receiveBufferSize,
                               TimeSpan keepAliveInterval,
                               ArraySegment <byte> internalBuffer)
            : base(innerStream, subProtocol, keepAliveInterval,
                   WebSocketBuffer.CreateServerBuffer(internalBuffer, receiveBufferSize))
        {
            _properties    = InternalBuffer.CreateProperties(false);
            _sessionHandle = CreateWebSocketHandle();

            if (_sessionHandle == null || _sessionHandle.IsInvalid)
            {
                HttpWebSocket.ThrowPlatformNotSupportedException_WSPC();
            }

            StartKeepAliveTimer();
        }
Esempio n. 5
0
        internal static WebSocket Create(Stream innerStream,
                                         string subProtocol,
                                         int receiveBufferSize,
                                         TimeSpan keepAliveInterval,
                                         ArraySegment <byte> internalBuffer)
        {
            if (!WebSocketProtocolComponent.IsSupported)
            {
                HttpWebSocket.ThrowPlatformNotSupportedException_WSPC();
            }

            HttpWebSocket.ValidateInnerStream(innerStream);
            HttpWebSocket.ValidateOptions(subProtocol, receiveBufferSize, HttpWebSocket.MinSendBufferSize, keepAliveInterval);
            WebSocketValidate.ValidateArraySegment(internalBuffer, nameof(internalBuffer));
            WebSocketBuffer.Validate(internalBuffer.Count, receiveBufferSize, HttpWebSocket.MinSendBufferSize, true);

            return(new ServerWebSocket(innerStream,
                                       subProtocol,
                                       receiveBufferSize,
                                       keepAliveInterval,
                                       internalBuffer));
        }