Guest User

Untitled

a guest
Dec 29th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.70 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "../object.h"
  4. #include "../promise.h"
  5. #include "../string-view.h"
  6. #include "../timestamp.h"
  7.  
  8. #include "common-http.h"
  9. #include "message.h"
  10.  
  11. namespace fn {
  12.     class SocketAddress;
  13. }
  14.  
  15. namespace fn {
  16. FN_UNINDENT
  17. namespace http {
  18.  
  19.     class ClientSession;
  20.  
  21.     FN_DECLARE_EXCEPTION(FN_EXPORT, WebSocketProtocolException, HTTPException)
  22.  
  23.     class WebSocketImpl;
  24.  
  25.     class FN_EXPORT WebSocket : public RefCountedObject
  26.     {
  27.     public:
  28.         // Received web-socket frame
  29.         class FN_EXPORT Frame
  30.         {
  31.         public:
  32.             enum PayloadType
  33.             {
  34.                 BINARY,
  35.                 UTF8
  36.             };
  37.  
  38.             enum FrameType
  39.             {
  40.                 SINGLE,       // not fragmented
  41.                 FRAGMENT,     // intermediate (non-final) fragment
  42.                 FRAGMENT_LAST // final fragment
  43.             };
  44.  
  45.             PayloadType payloadType() const { return _payloadType; }
  46.             FrameType frameType() const { return _frameType; }
  47.  
  48.             const UInt8 * payload() const { return _payload; }
  49.             SizeType payloadLength() const { return _payloadLength; }
  50.  
  51.             StringView payloadUTF8() const; // will trigger assertion when called on binary frames
  52.  
  53.             FN_MAKE_NONCOPYABLE(Frame);
  54.  
  55.         private:
  56.             Frame(PayloadType, FrameType, const UInt8 *, SizeType);
  57.             friend class WebSocketImpl;
  58.             PayloadType _payloadType;
  59.             FrameType _frameType;
  60.             const UInt8 * _payload;
  61.             SizeType _payloadLength;
  62.         };
  63.  
  64.         //
  65.         // WebSocket callbacks
  66.         //
  67.         // frame - invoked for every received binary or UTF8 frame
  68.         // error - invoked when something goes wrong with connection (other than
  69.         // unexpected disconnect) unexpectedDisconnect - invoked when connection gets
  70.         // terminated without proper closing sequence remoteCloseRequest - invoked when
  71.         // peer initiated closing sequence; This needs to be confirmed using close(),
  72.         //                      preferably with same reasonCode
  73.         //
  74.         // This method needs to be called reasonably early after connection is
  75.         // established, otherwise the connection may be terminated due to pings not being
  76.         // transmited
  77.         //
  78.  
  79.         // clang-format off
  80.         virtual void on(Function<void(const Frame &)> frame,
  81.                         Function<void(std::exception_ptr)> error,
  82.                         Function<void(bool pongNotReceived)> unexpectedDisconnect,
  83.                         Function<void(UInt16 reasonCode, const String & reasonMessage)> remoteCloseRequest) = 0;
  84.         // clang-format on
  85.  
  86.         // No callbacks will be invoked *after close callback*; ReasonMessage will be
  87.         // trimmed to 123 bytes
  88.         virtual void close(UInt16 reasonCode, const String & reasonMessage,
  89.                            Function<void(bool clean)> cb) = 0;
  90.  
  91.         virtual void sendFrame(const UInt8 * data, SizeType length, bool final = true,
  92.                                Function<void()> cb = nullptr) = 0;
  93.  
  94.         virtual void sendFrame(const StringView & text, bool final = true, Function<void()> cb = nullptr) = 0;
  95.  
  96.         //
  97.  
  98.         // How often ping will be sent
  99.         virtual void setPingInterval(const Duration & interval) = 0;
  100.         virtual const Duration & pingInterval() const = 0;
  101.  
  102.         // How long to wait for pong before considering the connection dead
  103.         virtual void setPingTimeout(const Duration & timeout) = 0;
  104.         virtual const Duration & pingTimeout() const = 0;
  105.  
  106.         virtual SocketAddress getSocketAddress() const = 0;
  107.         virtual SocketAddress getPeerAddress() const = 0;
  108.  
  109.         //
  110.         // Web Socket client
  111.         //
  112.  
  113.         // Connects to given URL
  114.         static Promise<ap<WebSocket>> connect(ap_<ClientSession> clientSession, const URL & url);
  115.  
  116.         // Connects using given request. Web-Socket specific headers on request will be
  117.         // overriden
  118.         static Promise<ap<WebSocket>> connect(ap_<ClientSession> clientSession, const Request & req);
  119.  
  120.         //
  121.         // Web Socket server
  122.         //
  123.  
  124.         // Will get rejected with ProtocolException when requests headers are incorrect
  125.         static Promise<ap<WebSocket>> accept(const http::Request & request, //
  126.                                              ap_<uv::Stream> /**/ stream);
  127.     };
  128.  
  129.     //
  130.     //
  131.     //
  132.  
  133.     inline StringView WebSocket::Frame::payloadUTF8() const
  134.     {
  135.         FN_ASSERT_MSG(_payloadType == UTF8, "Wrong payload type");
  136.         return StringView(as<const char *>(_payload), _payloadLength);
  137.     }
  138.  
  139. } // namespace http
  140. } // namespace fn
Add Comment
Please, Sign In to add comment