web_socket.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_SERVER_WEB_SOCKET_H_
  5. #define NET_SERVER_WEB_SOCKET_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/strings/string_piece.h"
  10. #include "net/traffic_annotation/network_traffic_annotation.h"
  11. #include "net/websockets/websocket_frame.h"
  12. namespace net {
  13. class HttpConnection;
  14. class HttpServer;
  15. class HttpServerRequestInfo;
  16. class WebSocketEncoder;
  17. class WebSocket final {
  18. public:
  19. enum ParseResult {
  20. // Final frame of a text message or compressed frame.
  21. FRAME_OK_FINAL,
  22. // Other frame of a text message.
  23. FRAME_OK_MIDDLE,
  24. FRAME_PING,
  25. FRAME_PONG,
  26. FRAME_INCOMPLETE,
  27. FRAME_CLOSE,
  28. FRAME_ERROR
  29. };
  30. WebSocket(HttpServer* server, HttpConnection* connection);
  31. void Accept(const HttpServerRequestInfo& request,
  32. const NetworkTrafficAnnotationTag traffic_annotation);
  33. ParseResult Read(std::string* message);
  34. void Send(base::StringPiece message,
  35. WebSocketFrameHeader::OpCodeEnum op_code,
  36. const NetworkTrafficAnnotationTag traffic_annotation);
  37. WebSocket(const WebSocket&) = delete;
  38. WebSocket& operator=(const WebSocket&) = delete;
  39. ~WebSocket();
  40. private:
  41. void Fail();
  42. void SendErrorResponse(const std::string& message,
  43. const NetworkTrafficAnnotationTag traffic_annotation);
  44. const raw_ptr<HttpServer> server_;
  45. const raw_ptr<HttpConnection> connection_;
  46. std::unique_ptr<WebSocketEncoder> encoder_;
  47. bool closed_ = false;
  48. std::unique_ptr<NetworkTrafficAnnotationTag> traffic_annotation_ = nullptr;
  49. };
  50. } // namespace net
  51. #endif // NET_SERVER_WEB_SOCKET_H_