1 /***
2 * ==++==
3 *
4 * Copyright (c) Microsoft Corporation. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * ==--==
17 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
18 *
19 * Websocket incoming and outgoing message definitions.
20 *
21 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
22 ****/
23 #pragma once
24
25 #include "cpprest/details/basic_types.h"
26
27 #if !defined(CPPREST_EXCLUDE_WEBSOCKETS)
28
29 #include <memory>
30 #include <limits>
31
32 #include "pplx/pplxtasks.h"
33 #include "cpprest/streams.h"
34 #include "cpprest/containerstream.h"
35 #include "cpprest/uri.h"
36 #include "cpprest/asyncrt_utils.h"
37
39 {
40 namespace websockets
41 {
42 namespace client
43 {
44
45 namespace details
46 {
47 class winrt_callback_client;
48 class wspp_callback_client;
49 #if defined(__cplusplus_winrt)
50 ref class ReceiveContext;
51 #endif
52 }
53
61 {
62 text_message,
63 binary_message,
64 close,
65 ping,
66 pong
67 };
68
73 {
74 public:
75
81 {
82 this->set_message(concurrency::streams::container_buffer<std::string>(std::move(data)));
83 }
84
90 {
91 this->set_message(concurrency::streams::container_buffer<std::string>(data));
92 }
93
100 {
101 this->set_message(istream, SIZE_MAX, websocket_message_type::text_message);
102 }
103
110 {
111 this->set_message(istream, len, websocket_message_type::text_message);
112 }
113
120 {
121 this->set_message(istream, len, websocket_message_type::binary_message);
122 }
123
130 {
131 this->set_message(istream, SIZE_MAX, websocket_message_type::binary_message);
132 }
133
134 private:
135 friend class details::winrt_callback_client;
136 friend class details::wspp_callback_client;
137
139 concurrency::streams::streambuf<uint8_t> m_body;
141 size_t m_length;
142
143 void signal_body_sent() const
144 {
146 }
147
148 void signal_body_sent(const std::exception_ptr &e) const
149 {
150 m_body_sent.set_exception(e);
151 }
152
154
155 void set_message(const concurrency::streams::container_buffer<std::string> &buffer)
156 {
157 m_msg_type = websocket_message_type::text_message;
158 m_length = static_cast<size_t>(buffer.size());
159 m_body = buffer;
160 }
161
162 void set_message(
const concurrency::streams::istream &istream,
size_t len,
websocket_message_type msg_type)
163 {
164 m_msg_type = msg_type;
165 m_length = len;
166 m_body = istream.streambuf();
167 }
168 };
169
174 {
175 public:
176
183
192 concurrency::streams::istream
body()
const
193 {
194 auto to_uint8_t_stream = [](const concurrency::streams::streambuf<uint8_t> &buf) -> concurrency::streams::istream
195 {
196 return buf.create_istream();
197 };
198 return to_uint8_t_stream(m_body);
199 }
200
205 {
206 return static_cast<size_t>(m_body.size());
207 }
208
212 CASABLANCA_DEPRECATED("Incorrectly spelled API, use message_type() instead.")
214 {
215 return m_msg_type;
216 }
217
223 {
224 return m_msg_type;
225 }
226
227 private:
228 friend class details::winrt_callback_client;
229 friend class details::wspp_callback_client;
230 #if defined(__cplusplus_winrt)
231 friend ref class details::ReceiveContext;
232 #endif
233
234 // Store message body in a container buffer backed by a string.
235 // Allows for optimization in the string message cases.
236 concurrency::streams::container_buffer<std::string> m_body;
238 };
239
240 }}}
241
242 #endif
concurrency::streams::istream body() const
Produces a stream which the caller may use to retrieve body from an incoming message. Can be used for both UTF-8 (text) and binary message types.
Definition: ws_msg.h:192
The task_completion_event class allows you to delay the execution of a task until a condition is sati...
Definition: pplxtasks.h:2934
Represents an incoming websocket message
Definition: ws_msg.h:173
websocket_message_type messge_type() const
Returns the type of the received message.
Definition: ws_msg.h:213
The web namespace contains functionality common to multiple protocols like HTTP and WebSockets...
Definition: base_uri.h:37
void set_utf8_message(const std::string &data)
Sets a UTF-8 message as the message body.
Definition: ws_msg.h:89
_ASYNCRTIMP pplx::task< std::string > extract_string() const
Extracts the body of the incoming message as a string value, only if the message type is UTF-8...
websocket_message_type
The different types of websocket message. Text type contains UTF-8 encoded data. Interpretation of Bi...
Definition: ws_msg.h:60
void set_binary_message(const concurrency::streams::istream &istream, size_t len)
Sets binary data as the message body.
Definition: ws_msg.h:119
websocket_message_type message_type() const
Returns the type of the received message, either string or binary.
Definition: ws_msg.h:222
size_t length() const
Returns the length of the received message.
Definition: ws_msg.h:204
The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed as...
Definition: pplxtasks.h:176
void set_binary_message(const concurrency::streams::istream &istream)
Sets binary data as the message body.
Definition: ws_msg.h:129
void set_utf8_message(const concurrency::streams::istream &istream)
Sets a UTF-8 message as the message body.
Definition: ws_msg.h:99
Represents an outgoing websocket message
Definition: ws_msg.h:72
void set_utf8_message(const concurrency::streams::istream &istream, size_t len)
Sets a UTF-8 message as the message body.
Definition: ws_msg.h:109
void set_utf8_message(std::string &&data)
Sets a UTF-8 message as the message body.
Definition: ws_msg.h:80
bool set() const
Sets the task completion event.
Definition: pplxtasks.h:2950