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;