Commit ebebef9f authored by chenhuaqing's avatar chenhuaqing

Implement close for mixed_message_oriented_layer

parent e41eaa05
...@@ -236,18 +236,23 @@ public: ...@@ -236,18 +236,23 @@ public:
} }
template <class LowerLayerPtr> template <class LowerLayerPtr>
void abort(LowerLayerPtr, const caf::error&) { void abort(LowerLayerPtr down, const caf::error&) {
// nop std::cout << "app abort " << "\n";
} }
template <class LowerLayerPtr> template <class LowerLayerPtr>
ptrdiff_t consume_text(LowerLayerPtr, caf::string_view text) { ptrdiff_t consume_text(LowerLayerPtr down, caf::string_view text) {
// Discard any input. // Discard any input.
std::cout << down->handle().id << " receive text: " << text << "\n";
if ("close" == text) {
down->close(1000, "Bye");
}
return static_cast<ptrdiff_t>(text.size()); return static_cast<ptrdiff_t>(text.size());
} }
template <class LowerLayerPtr> template <class LowerLayerPtr>
ptrdiff_t consume_binary(LowerLayerPtr, caf::byte_span bytes) { ptrdiff_t consume_binary(LowerLayerPtr down, caf::byte_span bytes) {
// Discard any input. // Discard any input.
return static_cast<ptrdiff_t>(bytes.size()); return static_cast<ptrdiff_t>(bytes.size());
} }
......
...@@ -54,6 +54,11 @@ public: ...@@ -54,6 +54,11 @@ public:
lptr_->end_text_message(llptr_); lptr_->end_text_message(llptr_);
} }
void close(uint16_t code, string_view reason) {
lptr_->close(llptr_, code, reason);
abort_reason();
}
void abort_reason(error reason) { void abort_reason(error reason) {
return lptr_->abort_reason(llptr_, std::move(reason)); return lptr_->abort_reason(llptr_, std::move(reason));
} }
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "caf/byte.hpp" #include "caf/byte.hpp"
#include "caf/byte_span.hpp" #include "caf/byte_span.hpp"
#include "caf/detail/network_order.hpp"
#include "caf/detail/rfc6455.hpp" #include "caf/detail/rfc6455.hpp"
#include "caf/net/mixed_message_oriented_layer_ptr.hpp" #include "caf/net/mixed_message_oriented_layer_ptr.hpp"
#include "caf/sec.hpp" #include "caf/sec.hpp"
...@@ -116,6 +117,11 @@ public: ...@@ -116,6 +117,11 @@ public:
ship_frame(down, text_buf_); ship_frame(down, text_buf_);
} }
template <class LowerLayerPtr>
void close(LowerLayerPtr down, int code, string_view reason) {
ship_close(down, code, reason);
}
template <class LowerLayerPtr> template <class LowerLayerPtr>
static void abort_reason(LowerLayerPtr parent, error reason) { static void abort_reason(LowerLayerPtr parent, error reason) {
return parent->abort_reason(std::move(reason)); return parent->abort_reason(std::move(reason));
...@@ -278,6 +284,23 @@ private: ...@@ -278,6 +284,23 @@ private:
down->end_output(); down->end_output();
} }
template <class LowerLayerPtr>
void ship_close(LowerLayerPtr down, uint16_t code, string_view reason) {
byte_buffer payload{sizeof(code) + reason.size()};
auto status_code = caf::detail::to_network_order(code);
memcpy(payload.data(), &status_code, sizeof(status_code));
memcpy(payload.data() + sizeof(status_code), reason.data(), reason.size());
uint32_t mask_key = 0;
if (mask_outgoing_frames) {
mask_key = static_cast<uint32_t>(rng_());
detail::rfc6455::mask_data(mask_key, payload);
}
down->begin_output();
detail::rfc6455::assemble_frame(detail::rfc6455::connection_close, mask_key,
payload, down->output_buffer());
down->end_output();
}
template <class LowerLayerPtr, class T> template <class LowerLayerPtr, class T>
void ship_frame(LowerLayerPtr down, std::vector<T>& buf) { void ship_frame(LowerLayerPtr down, std::vector<T>& buf) {
uint32_t mask_key = 0; uint32_t mask_key = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment