wasm.websocket.api.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. {
  2. This file is part of the Free Component Library
  3. Webassembly Websocket API - imported functions and structures.
  4. Copyright (c) 2024 by Michael Van Canneyt [email protected]
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit wasm.websocket.api;
  12. {$mode ObjFPC}{$H+}
  13. interface
  14. uses
  15. {$IFDEF FPC_DOTTEDUNITS}
  16. System.SysUtils,
  17. {$ELSE}
  18. sysutils,
  19. {$ENDIF}
  20. wasm.websocket.shared;
  21. Type
  22. TWasmWebSocketLogLevel = (wllTrace, wllDebug, wllInfo, wllWarning, wllError, wllCritical);
  23. TWasmWebSocketLogLevels = set of TWasmWebsocketLogLevel;
  24. function __wasm_websocket_allocate(
  25. aURL : PByte;
  26. aUrlLen : Longint;
  27. aProtocols : PByte;
  28. aProtocolLen : Longint;
  29. aUserData : Pointer;
  30. aWebsocketID : PWasmWebSocketID) : TWasmWebsocketResult; external websocketExportName name websocketFN_Allocate;
  31. function __wasm_websocket_close(
  32. aWebsocketID : TWasmWebSocketID;
  33. aCode : Longint;
  34. aReason : PByte;
  35. aReasonLen : Longint) : TWasmWebsocketResult; external websocketExportName name websocketFN_Close;
  36. function __wasm_websocket_send(
  37. aWebsocketID : TWasmWebSocketID;
  38. aData : PByte;
  39. aDataLen : Longint;
  40. aType : Longint
  41. ) : TWasmWebsocketResult; external websocketExportName name websocketFN_Send;
  42. function __wasm_websocket_deallocate(
  43. aWebsocketID : TWasmWebSocketID) : TWasmWebsocketResult; external websocketExportName name websocketFN_DeAllocate;
  44. Type
  45. TWasmWebsocketErrorCallback = procedure(aWebSocketID : TWasmWebSocketID; aUserData : Pointer);
  46. TWasmWebsocketMessageCallback = procedure(aWebSocketID : TWasmWebSocketID; aUserData : Pointer; aMessageType : TWasmWebSocketMessageType; aMessage : TBytes);
  47. TWasmWebsocketCloseCallback = procedure(aWebSocketID : TWasmWebSocketID; aUserData : Pointer; aCode: Longint; const aReason : String; aClean : Boolean);
  48. TWasmWebsocketOpenCallback = procedure(aWebSocketID : TWasmWebSocketID; aUserData : Pointer);
  49. TWasmWebsocketLogHook = procedure (Level : TWasmWebSocketLogLevel; const Msg : string) of object;
  50. // Callee is responsible for freeing incoming buffers
  51. Function __wasm_websocket_allocate_buffer(aWebsocketID : TWasmWebSocketID; aUserData : Pointer; aBufferLen : Longint) : Pointer;
  52. Function __wasm_websocket_on_error (aWebsocketID : TWasmWebSocketID; aUserData : Pointer) : TWebsocketCallBackResult;
  53. Function __wasm_websocket_on_message (aWebsocketID : TWasmWebSocketID; aUserData : Pointer; aMessageType : TWasmWebSocketMessageType; aMessage : Pointer; aMessageLen : Integer) : TWebsocketCallBackResult;
  54. Function __wasm_websocket_on_open (aWebsocketID : TWasmWebSocketID; aUserData : Pointer) : TWebsocketCallBackResult;
  55. Function __wasm_websocket_on_close (aWebsocketID : TWasmWebSocketID; aUserData : Pointer; aCode: Longint; aReason : PByte; aReasonLen : Longint; aClean : Longint) : TWebsocketCallBackResult;
  56. procedure __wasmwebsocket_log(level : TWasmWebsocketLogLevel; const Msg : String);
  57. procedure __wasmwebsocket_log(level : TWasmWebSocketLogLevel; const Fmt : String; Args : Array of const);
  58. var
  59. WebSocketErrorCallback : TWasmWebsocketErrorCallback;
  60. WebSocketMessageCallback : TWasmWebsocketMessageCallback;
  61. WebSocketCloseCallback : TWasmWebsocketCloseCallback;
  62. WebSocketOpenCallback : TWasmWebsocketOpenCallback;
  63. OnWebsocketLog : TWasmWebsocketLogHook;
  64. implementation
  65. procedure __wasmwebsocket_log(level : TWasmWebSocketLogLevel; const Msg : String);
  66. begin
  67. if assigned(OnWebsocketLog) then
  68. OnWebSocketLog(level,msg)
  69. end;
  70. procedure __wasmwebsocket_log(level : TWasmWebSocketLogLevel; const Fmt : String; Args : Array of const);
  71. begin
  72. if assigned(OnWebsocketLog) then
  73. OnWebSocketLog(level,SafeFormat(Fmt,Args));
  74. end;
  75. Function __wasm_websocket_allocate_buffer(aWebsocketID : TWasmWebSocketID; aUserData : Pointer; aBufferLen : Longint) : Pointer;
  76. begin
  77. Result:=GetMem(aBufferLen);
  78. end;
  79. procedure LogError(const aOperation : String; aError : Exception);
  80. begin
  81. __wasmwebsocket_log(wllError,SafeFormat('Error %s during %s callback: %s',[aError.ClassName,aError.Message]));
  82. end;
  83. Function __wasm_websocket_on_error (aWebsocketID : TWasmWebSocketID; aUserData : Pointer) : TWebsocketCallBackResult;
  84. var
  85. lErr : String;
  86. Buf : TBytes;
  87. begin
  88. if not assigned(WebSocketErrorCallback) then
  89. Exit(WASMWS_CALLBACK_NOHANDLER);
  90. try
  91. WebsocketErrorCallBack(aWebsocketID,aUserData);
  92. Result:=WASMWS_CALLBACK_SUCCESS;
  93. except
  94. On E : exception do
  95. begin
  96. LogError('error',E);
  97. Result:=WASMWS_CALLBACK_ERROR;
  98. end;
  99. end;
  100. end;
  101. Function __wasm_websocket_on_message (aWebsocketID : TWasmWebSocketID; aUserData : Pointer; aMessageType : TWasmWebSocketMessageType; aMessage : Pointer; aMessageLen : Integer) : TWebsocketCallBackResult;
  102. var
  103. Buf : TBytes;
  104. begin
  105. try
  106. if not assigned(WebSocketMessageCallback) then
  107. Exit(WASMWS_CALLBACK_NOHANDLER);
  108. try
  109. SetLength(Buf,aMessageLen);
  110. if aMessageLen>0 then
  111. Move(aMessage^,Buf[0],aMessageLen);
  112. WebsocketMessageCallBack(aWebsocketID,aUserData,aMessageType,Buf);
  113. Result:=WASMWS_CALLBACK_SUCCESS;
  114. except
  115. On E : exception do
  116. begin
  117. LogError('message',E);
  118. Result:=WASMWS_CALLBACK_ERROR;
  119. end;
  120. end;
  121. finally
  122. FreeMem(aMessage);
  123. end;
  124. end;
  125. Function __wasm_websocket_on_open (aWebsocketID : TWasmWebSocketID; aUserData : Pointer) : TWebsocketCallBackResult;
  126. begin
  127. if not assigned(WebSocketOpenCallback) then
  128. Exit(WASMWS_CALLBACK_NOHANDLER);
  129. try
  130. WebsocketOpenCallBack(aWebsocketID,aUserData);
  131. Result:=WASMWS_CALLBACK_SUCCESS;
  132. except
  133. On E : exception do
  134. begin
  135. LogError('message',E);
  136. Result:=WASMWS_CALLBACK_ERROR;
  137. end;
  138. end;
  139. end;
  140. Function __wasm_websocket_on_close (aWebsocketID : TWasmWebSocketID; aUserData : Pointer; aCode: Longint; aReason : PByte; aReasonLen : Longint; aClean : Longint) : TWebsocketCallBackResult;
  141. var
  142. lReason : String;
  143. Buf : TBytes;
  144. lClean : Boolean;
  145. begin
  146. try
  147. if not assigned(WebSocketCloseCallback) then
  148. Exit(WASMWS_CALLBACK_NOHANDLER);
  149. try
  150. lClean:=(aClean=0);
  151. SetLength(Buf,aReasonLen);
  152. Move(aReason^,Buf[0],aReasonLen);
  153. {$IF SIZEOF(CHAR)=1}
  154. lReason:=TEncoding.UTF8.GetAnsiString(Buf);
  155. {$ELSE}
  156. lReason:=TEncoding.UTF8.GetString(Buf);
  157. {$ENDIF}
  158. WebsocketCloseCallBack(aWebsocketID,aUserData,aCode,lReason,lClean);
  159. Result:=WASMWS_CALLBACK_SUCCESS;
  160. except
  161. On E : exception do
  162. begin
  163. LogError('message',E);
  164. Result:=WASMWS_CALLBACK_ERROR;
  165. end;
  166. end;
  167. finally
  168. FreeMem(aReason);
  169. end;
  170. end;
  171. exports
  172. __wasm_websocket_allocate_buffer,
  173. __wasm_websocket_on_error,
  174. __wasm_websocket_on_message,
  175. __wasm_websocket_on_open,
  176. __wasm_websocket_on_close;
  177. end.