rippled
Loading...
Searching...
No Matches
WSSession.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_SERVER_WSSESSION_H_INCLUDED
21#define RIPPLE_SERVER_WSSESSION_H_INCLUDED
22
23#include <xrpl/server/Handoff.h>
24#include <xrpl/server/Port.h>
25#include <xrpl/server/Writer.h>
26
27#include <boost/asio/buffer.hpp>
28#include <boost/asio/ip/tcp.hpp>
29#include <boost/beast/core/buffers_prefix.hpp>
30#include <boost/beast/websocket/rfc6455.hpp>
31#include <boost/logic/tribool.hpp>
32
33#include <algorithm>
34#include <functional>
35#include <memory>
36#include <utility>
37#include <vector>
38
39namespace ripple {
40
41class WSMsg
42{
43public:
44 WSMsg() = default;
45 WSMsg(WSMsg const&) = delete;
46 WSMsg&
47 operator=(WSMsg const&) = delete;
48 virtual ~WSMsg() = default;
49
68 prepare(std::size_t bytes, std::function<void(void)> resume) = 0;
69};
70
71template <class Streambuf>
72class StreambufWSMsg : public WSMsg
73{
74 Streambuf sb_;
76
77public:
78 StreambufWSMsg(Streambuf&& sb) : sb_(std::move(sb))
79 {
80 }
81
83 prepare(std::size_t bytes, std::function<void(void)>) override
84 {
85 if (sb_.size() == 0)
86 return {true, {}};
87 sb_.consume(n_);
88 boost::tribool done;
89 if (bytes < sb_.size())
90 {
91 n_ = bytes;
92 done = false;
93 }
94 else
95 {
96 n_ = sb_.size();
97 done = true;
98 }
99 auto const pb = boost::beast::buffers_prefix(n_, sb_.data());
101 std::distance(pb.begin(), pb.end()));
102 std::copy(pb.begin(), pb.end(), std::back_inserter(vb));
103 return {done, vb};
104 }
105};
106
108{
110
111 virtual ~WSSession() = default;
112 WSSession() = default;
113 WSSession(WSSession const&) = delete;
114 WSSession&
115 operator=(WSSession const&) = delete;
116
117 virtual void
118 run() = 0;
119
120 virtual Port const&
121 port() const = 0;
122
123 virtual http_request_type const&
124 request() const = 0;
125
126 virtual boost::asio::ip::tcp::endpoint const&
127 remote_endpoint() const = 0;
128
130 virtual void
132
133 virtual void
134 close() = 0;
135
136 virtual void
137 close(boost::beast::websocket::close_reason const& reason) = 0;
138
143 virtual void
144 complete() = 0;
145};
146
147} // namespace ripple
148
149#endif
T back_inserter(T... args)
std::pair< boost::tribool, std::vector< boost::asio::const_buffer > > prepare(std::size_t bytes, std::function< void(void)>) override
Retrieve message data.
Definition WSSession.h:83
StreambufWSMsg(Streambuf &&sb)
Definition WSSession.h:78
WSMsg & operator=(WSMsg const &)=delete
virtual ~WSMsg()=default
virtual std::pair< boost::tribool, std::vector< boost::asio::const_buffer > > prepare(std::size_t bytes, std::function< void(void)> resume)=0
Retrieve message data.
WSMsg(WSMsg const &)=delete
WSMsg()=default
T copy(T... args)
T distance(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
Definition Handoff.h:33
STL namespace.
Configuration information for a Server listening port.
Definition Port.h:50
virtual void close()=0
std::shared_ptr< void > appDefined
Definition WSSession.h:109
virtual http_request_type const & request() const =0
virtual boost::asio::ip::tcp::endpoint const & remote_endpoint() const =0
WSSession & operator=(WSSession const &)=delete
virtual Port const & port() const =0
virtual void run()=0
virtual void send(std::shared_ptr< WSMsg > w)=0
Send a WebSockets message.
WSSession()=default
virtual void close(boost::beast::websocket::close_reason const &reason)=0
WSSession(WSSession const &)=delete
virtual void complete()=0
Indicate that the response is complete.
virtual ~WSSession()=default