Path: blob/master/Utilities/cmcppdap/include/dap/network.h
3158 views
// Copyright 2019 Google LLC1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// https://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#ifndef dap_network_h15#define dap_network_h1617#include <functional>18#include <memory>19#include <stdint.h>2021namespace dap {22class ReaderWriter;2324namespace net {2526// connect() connects to the given TCP address and port.27// If timeoutMillis is non-zero and no connection was made before timeoutMillis28// milliseconds, then nullptr is returned.29std::shared_ptr<ReaderWriter> connect(const char* addr,30int port,31uint32_t timeoutMillis = 0);3233// Server implements a basic TCP server.34class Server {35// ignoreErrors() matches the OnError signature, and does nothing.36static inline void ignoreErrors(const char*) {}3738public:39using OnError = std::function<void(const char*)>;40using OnConnect = std::function<void(const std::shared_ptr<ReaderWriter>&)>;4142virtual ~Server() = default;4344// create() constructs and returns a new Server.45static std::unique_ptr<Server> create();4647// start() begins listening for connections on localhost and the given port.48// callback will be called for each connection.49// onError will be called for any connection errors.50virtual bool start(int port,51const OnConnect& callback,52const OnError& onError = ignoreErrors) = 0;5354// start() begins listening for connections on the given specific address and port.55// callback will be called for each connection.56// onError will be called for any connection errors.57virtual bool start(const char* address,58int port,59const OnConnect& callback,60const OnError& onError = ignoreErrors) = 0;6162// stop() stops listening for connections.63// stop() is implicitly called on destruction.64virtual void stop() = 0;65};6667} // namespace net68} // namespace dap6970#endif // dap_network_h717273