• mormot对windows websocket的封装


    mormot对windows websocket的封装

    windows 20128 以上版本开始提供websocket.dll动态库给外部程序调用。mormot也调用它。

    procedure WebSocketApiInitialize;
    var
      api: TWebSocketAPIs;
      P: PPointer;
    begin
      if WebSocketAPI.LibraryHandle <> 0 then
        exit; // already loaded
      WebSocketAPI.WebSocketEnabled := false;
      WebSocketAPI.LibraryHandle := SafeLoadLibrary(WEBSOCKET_DLL); //加载 websocket.dll
      if WebSocketAPI.LibraryHandle = 0 then
        exit;
      P := @@WebSocketAPI.AbortHandle;
      for api := low(api) to high(api) do
      begin
        P^ := GetProcAddress(WebSocketAPI.LibraryHandle, WebSocketNames[api]);
        if P^ = nil then
        begin
          FreeLibrary(WebSocketAPI.LibraryHandle);
          WebSocketAPI.LibraryHandle := 0;
          exit;
        end;
        inc(P);
      end;
      WebSocketAPI.WebSocketEnabled := true;
    end;
    

      

      /// direct late-binding access to the WebSocket Protocol Component API functions
      TWebSocketAPI = packed record
        /// acces to the loaded library handle
        LibraryHandle: THandle;
        /// depends on Windows version
        WebSocketEnabled: Boolean;
        /// aborts a WebSocket session handle created by WebSocketCreateClientHandle
        // or WebSocketCreateServerHandle
        AbortHandle: procedure(hWebSocket: WEB_SOCKET_HANDLE); stdcall;
        /// begins the client-side handshake
        BeginClientHandshake: function(hWebSocket: WEB_SOCKET_HANDLE;
          pszSubprotocols: PAnsiChar; ulSubprotocolCount: ULONG; pszExtensions:
          PAnsiChar; ulExtensionCount: ULONG; const pInitialHeaders:
          PWEB_SOCKET_HTTP_HEADER; ulInitialHeaderCount: ULONG;
          out pAdditionalHeaders: PWEB_SOCKET_HTTP_HEADER;
          out pulAdditionalHeaderCount: ULONG): HRESULT; stdcall;
        /// begins the server-side handshake
        BeginServerHandshake: function(hWebSocket: WEB_SOCKET_HANDLE;
          pszSubprotocolSelected: PAnsiChar; pszExtensionSelected: PAnsiChar;
          ulExtensionSelectedCount: ULONG; const pRequestHeaders:
          PWEB_SOCKET_HTTP_HEADER; ulRequestHeaderCount: ULONG; out pResponseHeaders:
          PWEB_SOCKET_HTTP_HEADER; out pulResponseHeaderCount: ULONG): HRESULT; stdcall;
        /// completes an action started by WebSocketGetAction
        CompleteAction: function(hWebSocket: WEB_SOCKET_HANDLE;
          pvActionContext: Pointer; ulBytesTransferred: ULONG): HRESULT; stdcall;
        /// creates a client-side WebSocket session handle
        CreateClientHandle: function(const pProperties: PWEB_SOCKET_PROPERTY;
          ulPropertyCount: ULONG; out phWebSocket: WEB_SOCKET_HANDLE): HRESULT; stdcall;
        /// creates a server-side WebSocket session handle
        CreateServerHandle: function(const pProperties: PWEB_SOCKET_PROPERTY;
          ulPropertyCount: ULONG; out phWebSocket: WEB_SOCKET_HANDLE): HRESULT; stdcall;
        /// deletes a WebSocket session handle created by WebSocketCreateClientHandle
        // or WebSocketCreateServerHandle
        DeleteHandle: procedure(hWebSocket: WEB_SOCKET_HANDLE); stdcall;
        /// completes the client-side handshake
        EndClientHandshake: function(hWebSocket: WEB_SOCKET_HANDLE; const
          pResponseHeaders: PWEB_SOCKET_HTTP_HEADER; ulReponseHeaderCount: ULONG;
          var pulSelectedExtensions: ULONG; var pulSelectedExtensionCount: ULONG;
          var pulSelectedSubprotocol: ULONG): HRESULT; stdcall;
        /// completes the server-side handshake
        EndServerHandshake: function(hWebSocket: WEB_SOCKET_HANDLE): HRESULT; stdcall;
        /// returns an action from a call to WebSocketSend, WebSocketReceive or WebSocketCompleteAction
        GetAction: function(hWebSocket: WEB_SOCKET_HANDLE; eActionQueue:
          WEB_SOCKET_ACTION_QUEUE; pDataBuffers: Pointer {WEB_SOCKET_BUFFER_DATA};
          var pulDataBufferCount: ULONG; var pAction: WEB_SOCKET_ACTION;
          var pBufferType: WEB_SOCKET_BUFFER_TYPE; var pvApplicationContext: Pointer;
          var pvActionContext: Pointer): HRESULT; stdcall;
        /// gets a single WebSocket property
        GetGlobalProperty: function(eType: WEB_SOCKET_PROPERTY_TYPE;
          pvValue: Pointer; var ulSize: ULONG): HRESULT; stdcall;
        /// adds a receive operation to the protocol component operation queue
        Receive: function(hWebSocket: WEB_SOCKET_HANDLE; pBuffer: Pointer;
          pvContext: Pointer): HRESULT; stdcall;
        /// adds a send operation to the protocol component operation queue
        Send: function(hWebSocket: WEB_SOCKET_HANDLE; BufferType:
          WEB_SOCKET_BUFFER_TYPE; pBuffer, Context: Pointer): HRESULT; stdcall;
      end;
    

      

  • 相关阅读:
    又爱又恨的eval
    http_build_query 这个方法会把值为NULL的给干掉
    allow_url_fopen设置
    纠结了下 B 和 STRONG标签区别
    Drupal 发邮件模块 drupal smtp 安装与设置
    php array_merge 和 两数组相加区别
    学历严格正相关于素质 Kai
    表语就是主语补语,靠 Kai
    一些真正有思想的up Kai
    光速不变且最大,换个思路想,非常合理,犹如天经地义【转载】 Kai
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/13473973.html
Copyright © 2020-2023  润新知