wasm.websocket.api.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.logger.api,
  21. wasm.websocket.shared;
  22. Type
  23. TWasmWebSocketLogLevel = TWasmLogLevel;
  24. TWasmWebSocketLogLevels = set of TWasmWebsocketLogLevel;
  25. const
  26. wllTrace = wasm.logger.api.wllTrace;
  27. wllDebug = wasm.logger.api.wllDebug;
  28. wllInfo = wasm.logger.api.wllInfo;
  29. wllWarning = wasm.logger.api.wllWarning;
  30. wllError = wasm.logger.api.wllError;
  31. wllCritical = wasm.logger.api.wllCritical;
  32. function __wasm_websocket_allocate(
  33. aURL : PByte;
  34. aUrlLen : Longint;
  35. aProtocols : PByte;
  36. aProtocolLen : Longint;
  37. aUserData : Pointer;
  38. aWebsocketID : PWasmWebSocketID) : TWasmWebsocketResult; external websocketExportName name websocketFN_Allocate;
  39. function __wasm_websocket_close(
  40. aWebsocketID : TWasmWebSocketID;
  41. aCode : Longint;
  42. aReason : PByte;
  43. aReasonLen : Longint) : TWasmWebsocketResult; external websocketExportName name websocketFN_Close;
  44. function __wasm_websocket_send(
  45. aWebsocketID : TWasmWebSocketID;
  46. aData : PByte;
  47. aDataLen : Longint;
  48. aType : Longint
  49. ) : TWasmWebsocketResult; external websocketExportName name websocketFN_Send;
  50. function __wasm_websocket_deallocate(
  51. aWebsocketID : TWasmWebSocketID) : TWasmWebsocketResult; external websocketExportName name websocketFN_DeAllocate;
  52. Type
  53. TWasmWebsocketErrorCallback = procedure(aWebSocketID : TWasmWebSocketID; aUserData : Pointer);
  54. TWasmWebsocketMessageCallback = procedure(aWebSocketID : TWasmWebSocketID; aUserData : Pointer; aMessageType : TWasmWebSocketMessageType; aMessage : TBytes);
  55. TWasmWebsocketCloseCallback = procedure(aWebSocketID : TWasmWebSocketID; aUserData : Pointer; aCode: Longint; const aReason : String; aClean : Boolean);
  56. TWasmWebsocketOpenCallback = procedure(aWebSocketID : TWasmWebSocketID; aUserData : Pointer);
  57. TWasmWebsocketLogHook = procedure (Level : TWasmWebSocketLogLevel; const Msg : string) of object;
  58. // Callee is responsible for freeing incoming buffers
  59. Function __wasm_websocket_allocate_buffer(aWebsocketID : TWasmWebSocketID; aUserData : Pointer; aBufferLen : Longint) : Pointer;
  60. Function __wasm_websocket_on_error (aWebsocketID : TWasmWebSocketID; aUserData : Pointer) : TWebsocketCallBackResult;
  61. Function __wasm_websocket_on_message (aWebsocketID : TWasmWebSocketID; aUserData : Pointer; aMessageType : TWasmWebSocketMessageType; aMessage : Pointer; aMessageLen : Integer) : TWebsocketCallBackResult;
  62. Function __wasm_websocket_on_open (aWebsocketID : TWasmWebSocketID; aUserData : Pointer) : TWebsocketCallBackResult;
  63. Function __wasm_websocket_on_close (aWebsocketID : TWasmWebSocketID; aUserData : Pointer; aCode: Longint; aReason : PByte; aReasonLen : Longint; aClean : Longint) : TWebsocketCallBackResult;
  64. procedure __wasmwebsocket_log(level : TWasmLogLevel; const Msg : String);
  65. procedure __wasmwebsocket_log(level : TWasmLogLevel; const Fmt : String; const Args : Array of const);
  66. var
  67. WebSocketLogEnabled : Boolean;
  68. WebSocketErrorCallback : TWasmWebsocketErrorCallback;
  69. WebSocketMessageCallback : TWasmWebsocketMessageCallback;
  70. WebSocketCloseCallback : TWasmWebsocketCloseCallback;
  71. WebSocketOpenCallback : TWasmWebsocketOpenCallback;
  72. implementation
  73. procedure __wasmwebsocket_log(level : TWasmWebSocketLogLevel; const Msg : String);
  74. begin
  75. if not WebSocketLogEnabled then
  76. exit;
  77. __wasm_log(level,'websocket',msg);
  78. end;
  79. procedure __wasmwebsocket_log(level : TWasmWebSocketLogLevel; const Fmt : String; const Args : Array of const);
  80. begin
  81. if not WebSocketLogEnabled then
  82. exit;
  83. __wasm_log(level,'websocket',Fmt,Args);
  84. end;
  85. Function __wasm_websocket_allocate_buffer(aWebsocketID : TWasmWebSocketID; aUserData : Pointer; aBufferLen : Longint) : Pointer;
  86. begin
  87. // Silence compiler warning
  88. if (aWebSocketID=0) or (aUserData=Nil) then ;
  89. Result:=GetMem(aBufferLen);
  90. end;
  91. procedure LogError(const aOperation : String; aError : Exception);
  92. begin
  93. __wasmwebsocket_log(wllError,SafeFormat('Error %s during %s callback: %s',[aError.ClassName,aOperation,aError.Message]));
  94. end;
  95. Function __wasm_websocket_on_error (aWebsocketID : TWasmWebSocketID; aUserData : Pointer) : TWebsocketCallBackResult;
  96. begin
  97. if not assigned(WebSocketErrorCallback) then
  98. Exit(WASMWS_CALLBACK_NOHANDLER);
  99. try
  100. WebsocketErrorCallBack(aWebsocketID,aUserData);
  101. Result:=WASMWS_CALLBACK_SUCCESS;
  102. except
  103. On E : exception do
  104. begin
  105. LogError('error',E);
  106. Result:=WASMWS_CALLBACK_ERROR;
  107. end;
  108. end;
  109. end;
  110. Function __wasm_websocket_on_message (aWebsocketID : TWasmWebSocketID; aUserData : Pointer; aMessageType : TWasmWebSocketMessageType; aMessage : Pointer; aMessageLen : Integer) : TWebsocketCallBackResult;
  111. var
  112. Buf : TBytes;
  113. begin
  114. Buf:=[];
  115. try
  116. if not assigned(WebSocketMessageCallback) then
  117. Exit(WASMWS_CALLBACK_NOHANDLER);
  118. try
  119. SetLength(Buf,aMessageLen);
  120. if aMessageLen>0 then
  121. Move(aMessage^,Buf[0],aMessageLen);
  122. WebsocketMessageCallBack(aWebsocketID,aUserData,aMessageType,Buf);
  123. Result:=WASMWS_CALLBACK_SUCCESS;
  124. except
  125. On E : exception do
  126. begin
  127. LogError('message',E);
  128. Result:=WASMWS_CALLBACK_ERROR;
  129. end;
  130. end;
  131. finally
  132. FreeMem(aMessage);
  133. end;
  134. end;
  135. Function __wasm_websocket_on_open (aWebsocketID : TWasmWebSocketID; aUserData : Pointer) : TWebsocketCallBackResult;
  136. begin
  137. if not assigned(WebSocketOpenCallback) then
  138. Exit(WASMWS_CALLBACK_NOHANDLER);
  139. try
  140. WebsocketOpenCallBack(aWebsocketID,aUserData);
  141. Result:=WASMWS_CALLBACK_SUCCESS;
  142. except
  143. On E : exception do
  144. begin
  145. LogError('message',E);
  146. Result:=WASMWS_CALLBACK_ERROR;
  147. end;
  148. end;
  149. end;
  150. Function __wasm_websocket_on_close (aWebsocketID : TWasmWebSocketID; aUserData : Pointer; aCode: Longint; aReason : PByte; aReasonLen : Longint; aClean : Longint) : TWebsocketCallBackResult;
  151. var
  152. lReason : String;
  153. Buf : TBytes;
  154. lClean : Boolean;
  155. begin
  156. Buf:=[];
  157. try
  158. if not assigned(WebSocketCloseCallback) then
  159. Exit(WASMWS_CALLBACK_NOHANDLER);
  160. try
  161. lClean:=(aClean=0);
  162. SetLength(Buf,aReasonLen);
  163. Move(aReason^,Buf[0],aReasonLen);
  164. {$IF SIZEOF(CHAR)=1}
  165. lReason:=TEncoding.UTF8.GetAnsiString(Buf);
  166. {$ELSE}
  167. lReason:=TEncoding.UTF8.GetString(Buf);
  168. {$ENDIF}
  169. WebsocketCloseCallBack(aWebsocketID,aUserData,aCode,lReason,lClean);
  170. Result:=WASMWS_CALLBACK_SUCCESS;
  171. except
  172. On E : exception do
  173. begin
  174. LogError('message',E);
  175. Result:=WASMWS_CALLBACK_ERROR;
  176. end;
  177. end;
  178. finally
  179. FreeMem(aReason);
  180. end;
  181. end;
  182. exports
  183. __wasm_websocket_allocate_buffer,
  184. __wasm_websocket_on_error,
  185. __wasm_websocket_on_message,
  186. __wasm_websocket_on_open,
  187. __wasm_websocket_on_close;
  188. end.