CefServerHandler.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// Implement this interface to handle HTTP server requests. A new thread will be
  10. /// created for each CefServer::CreateServer call (the "dedicated server
  11. /// thread"), and the methods of this class will be called on that thread. It is
  12. /// therefore recommended to use a different CefServerHandler instance for each
  13. /// CefServer::CreateServer call to avoid thread safety issues in the
  14. /// CefServerHandler implementation.
  15. /// </summary>
  16. public abstract unsafe partial class CefServerHandler
  17. {
  18. private void on_server_created(cef_server_handler_t* self, cef_server_t* server)
  19. {
  20. CheckSelf(self);
  21. var mServer = CefServer.FromNative(server);
  22. OnServerCreated(mServer);
  23. }
  24. /// <summary>
  25. /// Called when |server| is created. If the server was started successfully
  26. /// then CefServer::IsRunning will return true. The server will continue
  27. /// running until CefServer::Shutdown is called, after which time
  28. /// OnServerDestroyed will be called. If the server failed to start then
  29. /// OnServerDestroyed will be called immediately after this method returns.
  30. /// </summary>
  31. protected abstract void OnServerCreated(CefServer server);
  32. private void on_server_destroyed(cef_server_handler_t* self, cef_server_t* server)
  33. {
  34. CheckSelf(self);
  35. var mServer = CefServer.FromNative(server);
  36. OnServerDestroyed(mServer);
  37. }
  38. /// <summary>
  39. /// Called when |server| is destroyed. The server thread will be stopped after
  40. /// this method returns. The client should release any references to |server|
  41. /// when this method is called. See OnServerCreated documentation for a
  42. /// description of server lifespan.
  43. /// </summary>
  44. protected abstract void OnServerDestroyed(CefServer server);
  45. private void on_client_connected(cef_server_handler_t* self, cef_server_t* server, int connection_id)
  46. {
  47. CheckSelf(self);
  48. var mServer = CefServer.FromNative(server);
  49. OnClientConnected(mServer, connection_id);
  50. }
  51. /// <summary>
  52. /// Called when a client connects to |server|. |connection_id| uniquely
  53. /// identifies the connection. Each call to this method will have a matching
  54. /// call to OnClientDisconnected.
  55. /// </summary>
  56. protected abstract void OnClientConnected(CefServer server, int connectionId);
  57. private void on_client_disconnected(cef_server_handler_t* self, cef_server_t* server, int connection_id)
  58. {
  59. CheckSelf(self);
  60. var mServer = CefServer.FromNative(server);
  61. OnClientDisconnected(mServer, connection_id);
  62. }
  63. /// <summary>
  64. /// Called when a client disconnects from |server|. |connection_id| uniquely
  65. /// identifies the connection. The client should release any data associated
  66. /// with |connection_id| when this method is called and |connection_id| should
  67. /// no longer be passed to CefServer methods. Disconnects can originate from
  68. /// either the client or the server. For example, the server will disconnect
  69. /// automatically after a CefServer::SendHttpXXXResponse method is called.
  70. /// </summary>
  71. protected abstract void OnClientDisconnected(CefServer server, int connectionId);
  72. private void on_http_request(cef_server_handler_t* self, cef_server_t* server, int connection_id, cef_string_t* client_address, cef_request_t* request)
  73. {
  74. CheckSelf(self);
  75. var mServer = CefServer.FromNative(server);
  76. var mClientAddress = cef_string_t.ToString(client_address);
  77. var mRequest = CefRequest.FromNative(request);
  78. OnHttpRequest(mServer, connection_id, mClientAddress, mRequest);
  79. }
  80. /// <summary>
  81. /// Called when |server| receives an HTTP request. |connection_id| uniquely
  82. /// identifies the connection, |client_address| is the requesting IPv4 or IPv6
  83. /// client address including port number, and |request| contains the request
  84. /// contents (URL, method, headers and optional POST data). Call CefServer
  85. /// methods either synchronously or asynchronusly to send a response.
  86. /// </summary>
  87. protected abstract void OnHttpRequest(CefServer server, int connectionId, string clientAddress, CefRequest request);
  88. private void on_web_socket_request(cef_server_handler_t* self, cef_server_t* server, int connection_id, cef_string_t* client_address, cef_request_t* request, cef_callback_t* callback)
  89. {
  90. CheckSelf(self);
  91. var mServer = CefServer.FromNative(server);
  92. var mClientAddress = cef_string_t.ToString(client_address);
  93. var mRequest = CefRequest.FromNative(request);
  94. var mCallback = CefCallback.FromNative(callback);
  95. OnWebSocketRequest(mServer, connection_id, mClientAddress, mRequest, mCallback);
  96. }
  97. /// <summary>
  98. /// Called when |server| receives a WebSocket request. |connection_id| uniquely
  99. /// identifies the connection, |client_address| is the requesting IPv4 or
  100. /// IPv6 client address including port number, and |request| contains the
  101. /// request contents (URL, method, headers and optional POST data). Execute
  102. /// |callback| either synchronously or asynchronously to accept or decline the
  103. /// WebSocket connection. If the request is accepted then OnWebSocketConnected
  104. /// will be called after the WebSocket has connected and incoming messages will
  105. /// be delivered to the OnWebSocketMessage callback. If the request is declined
  106. /// then the client will be disconnected and OnClientDisconnected will be
  107. /// called. Call the CefServer::SendWebSocketMessage method after receiving the
  108. /// OnWebSocketConnected callback to respond with WebSocket messages.
  109. /// </summary>
  110. protected abstract void OnWebSocketRequest(CefServer server, int connectionId, string clientAddress, CefRequest request, CefCallback callback);
  111. private void on_web_socket_connected(cef_server_handler_t* self, cef_server_t* server, int connection_id)
  112. {
  113. CheckSelf(self);
  114. var mServer = CefServer.FromNative(server);
  115. OnWebSocketConnected(mServer, connection_id);
  116. }
  117. /// <summary>
  118. /// Called after the client has accepted the WebSocket connection for |server|
  119. /// and |connection_id| via the OnWebSocketRequest callback. See
  120. /// OnWebSocketRequest documentation for intended usage.
  121. /// </summary>
  122. protected abstract void OnWebSocketConnected(CefServer server, int connectionId);
  123. private void on_web_socket_message(cef_server_handler_t* self, cef_server_t* server, int connection_id, void* data, UIntPtr data_size)
  124. {
  125. CheckSelf(self);
  126. var mServer = CefServer.FromNative(server);
  127. var mData = (IntPtr)data;
  128. var mDataSize = checked((long)data_size);
  129. OnWebSocketMessage(mServer, connection_id, mData, mDataSize);
  130. }
  131. /// <summary>
  132. /// Called when |server| receives an WebSocket message. |connection_id|
  133. /// uniquely identifies the connection, |data| is the message content and
  134. /// |data_size| is the size of |data| in bytes. Do not keep a reference to
  135. /// |data| outside of this method. See OnWebSocketRequest documentation for
  136. /// intended usage.
  137. /// </summary>
  138. protected abstract void OnWebSocketMessage(CefServer server, int connectionId, IntPtr data, long dataSize);
  139. }
  140. }