Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "../object.h"
- #include "../promise.h"
- #include "../string-view.h"
- #include "../timestamp.h"
- #include "common-http.h"
- #include "message.h"
- namespace fn {
- class SocketAddress;
- }
- namespace fn {
- FN_UNINDENT
- namespace http {
- class ClientSession;
- FN_DECLARE_EXCEPTION(FN_EXPORT, WebSocketProtocolException, HTTPException)
- class WebSocketImpl;
- class FN_EXPORT WebSocket : public RefCountedObject
- {
- public:
- // Received web-socket frame
- class FN_EXPORT Frame
- {
- public:
- enum PayloadType
- {
- BINARY,
- UTF8
- };
- enum FrameType
- {
- SINGLE, // not fragmented
- FRAGMENT, // intermediate (non-final) fragment
- FRAGMENT_LAST // final fragment
- };
- PayloadType payloadType() const { return _payloadType; }
- FrameType frameType() const { return _frameType; }
- const UInt8 * payload() const { return _payload; }
- SizeType payloadLength() const { return _payloadLength; }
- StringView payloadUTF8() const; // will trigger assertion when called on binary frames
- FN_MAKE_NONCOPYABLE(Frame);
- private:
- Frame(PayloadType, FrameType, const UInt8 *, SizeType);
- friend class WebSocketImpl;
- PayloadType _payloadType;
- FrameType _frameType;
- const UInt8 * _payload;
- SizeType _payloadLength;
- };
- //
- // WebSocket callbacks
- //
- // frame - invoked for every received binary or UTF8 frame
- // error - invoked when something goes wrong with connection (other than
- // unexpected disconnect) unexpectedDisconnect - invoked when connection gets
- // terminated without proper closing sequence remoteCloseRequest - invoked when
- // peer initiated closing sequence; This needs to be confirmed using close(),
- // preferably with same reasonCode
- //
- // This method needs to be called reasonably early after connection is
- // established, otherwise the connection may be terminated due to pings not being
- // transmited
- //
- // clang-format off
- virtual void on(Function<void(const Frame &)> frame,
- Function<void(std::exception_ptr)> error,
- Function<void(bool pongNotReceived)> unexpectedDisconnect,
- Function<void(UInt16 reasonCode, const String & reasonMessage)> remoteCloseRequest) = 0;
- // clang-format on
- // No callbacks will be invoked *after close callback*; ReasonMessage will be
- // trimmed to 123 bytes
- virtual void close(UInt16 reasonCode, const String & reasonMessage,
- Function<void(bool clean)> cb) = 0;
- virtual void sendFrame(const UInt8 * data, SizeType length, bool final = true,
- Function<void()> cb = nullptr) = 0;
- virtual void sendFrame(const StringView & text, bool final = true, Function<void()> cb = nullptr) = 0;
- //
- // How often ping will be sent
- virtual void setPingInterval(const Duration & interval) = 0;
- virtual const Duration & pingInterval() const = 0;
- // How long to wait for pong before considering the connection dead
- virtual void setPingTimeout(const Duration & timeout) = 0;
- virtual const Duration & pingTimeout() const = 0;
- virtual SocketAddress getSocketAddress() const = 0;
- virtual SocketAddress getPeerAddress() const = 0;
- //
- // Web Socket client
- //
- // Connects to given URL
- static Promise<ap<WebSocket>> connect(ap_<ClientSession> clientSession, const URL & url);
- // Connects using given request. Web-Socket specific headers on request will be
- // overriden
- static Promise<ap<WebSocket>> connect(ap_<ClientSession> clientSession, const Request & req);
- //
- // Web Socket server
- //
- // Will get rejected with ProtocolException when requests headers are incorrect
- static Promise<ap<WebSocket>> accept(const http::Request & request, //
- ap_<uv::Stream> /**/ stream);
- };
- //
- //
- //
- inline StringView WebSocket::Frame::payloadUTF8() const
- {
- FN_ASSERT_MSG(_payloadType == UTF8, "Wrong payload type");
- return StringView(as<const char *>(_payload), _payloadLength);
- }
- } // namespace http
- } // namespace fn
Add Comment
Please, Sign In to add comment