1 /**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "test_server.h"
17 #include "test_config.h"
18
19 #include "websocketpp/connection.hpp"
20
21 #include <cstddef>
22 #include <system_error>
23
24 #define CONFIG test::TestConfig
25 #include "../server_endpoint-inl.h"
26 #undef CONFIG
27
28 namespace panda::tooling::inspector::test {
TestServer(std::string_view name,TestLogger & logger)29 TestServer::TestServer(std::string_view name, TestLogger &logger) : TestEventLoop(name, logger) {}
30
Connect(const websocketpp::connection<TestConfig>::ptr & clientConnection,TestEventLoop & clientEventLoop)31 void TestServer::Connect(const websocketpp::connection<TestConfig>::ptr &clientConnection,
32 TestEventLoop &clientEventLoop)
33 {
34 auto serverConnection = endpoint_.get_connection();
35
36 using weak = websocketpp::connection<TestConfig>::weak_ptr;
37
38 serverConnection->set_write_handler(
39 [connection = weak(clientConnection), &clientEventLoop](auto /* hdl */, const char *buffer, size_t size) {
40 clientEventLoop.Push(connection.lock(), {buffer, buffer + size});
41 return std::error_code();
42 });
43
44 clientConnection->set_write_handler(
45 [this, connection = weak(serverConnection)](auto /* hdl */, const char *buffer, size_t size) {
46 Push(connection.lock(), {buffer, buffer + size});
47 return std::error_code();
48 });
49
50 serverConnection->start();
51 clientConnection->start();
52 }
53 } // namespace panda::tooling::inspector::test
54