xtd - Reference Guide  0.1.2
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Loading...
Searching...
No Matches
tcp_listener.h
Go to the documentation of this file.
1
4#pragma once
5
6#include "tcp_client.h"
7
9namespace xtd {
11 namespace net {
13 namespace sockets {
30 class tcp_listener : public xtd::object {
31 class async_result_socket : public xtd::object, public xtd::iasync_result {
32 public:
33 async_result_socket(std::any async_state) : async_state_(async_state) {}
34 std::any async_state() const noexcept override {return async_state_;}
35 std::shared_mutex& async_mutex() override {return async_mutex_;}
36 bool completed_synchronously() const noexcept override {return false;}
37 bool is_completed() const noexcept override {return is_completed_;};
38
39 std::any async_state_;
40 bool is_completed_ = false;
41 std::shared_mutex async_mutex_;
43 std::exception_ptr exception_;
44 };
45
46 class async_result_accept_socket : public async_result_socket {
47 public:
48 async_result_accept_socket(std::any async_state) : async_result_socket(async_state) {}
50 };
51
52 class async_result_accept_tcp_client : public async_result_socket {
53 public:
54 async_result_accept_tcp_client(std::any async_state) : async_result_socket(async_state) {}
56 };
57
58 public:
67
77
79 tcp_listener(tcp_listener&&) = default;
80 tcp_listener(const tcp_listener&) = default;
82 tcp_listener& operator=(const tcp_listener&) = default;
83 friend std::ostream& operator <<(std::ostream& os, const tcp_listener& tl) noexcept {return os << tl.to_string();}
84 bool operator==(const tcp_listener& s) const {return data_ == s.data_;};
85 bool operator!=(const tcp_listener& s) const {return !operator==(s);};
87
111
115 const xtd::net::end_point& local_end_point() const noexcept;
116
122 xtd::net::sockets::socket server() const noexcept;
123
133 xtd::net::sockets::socket accept_socket();
134
143
154 std::shared_ptr<xtd::iasync_result> begin_accept_socket(xtd::async_callback callback, const std::any& state);
155
165 std::shared_ptr<xtd::iasync_result> begin_accept_tcp_client(xtd::async_callback callback, const std::any& state);
166
170 static tcp_listener create(uint16_t port);
171
181 xtd::net::sockets::socket end_accept_socket(std::shared_ptr<xtd::iasync_result> async_result);
182
193 xtd::net::sockets::tcp_client end_accept_tcp_client(std::shared_ptr<xtd::iasync_result> async_result);
194
201 bool pending();
202
209 void start();
210
220 void start(size_t backlog);
221
227 void stop();
228
229 protected:
234 bool active() const noexcept;
235
236 private:
237 struct data;
238 std::shared_ptr<data> data_;
239 };
240 }
241 }
242}
Represents the status of an asynchronous operation.
Definition iasync_result.h:21
Identifies a network address. This is an abstract class.
Definition end_point.h:24
Provides an Internet Protocol (IP) address.
Definition ip_address.h:30
Represents a network endpoint as an IP address and a port number.
Definition ip_end_point.h:20
Implements the Berkeley sockets interface.
Definition socket.h:63
Provides client connections for TCP network services.
Definition tcp_client.h:39
Listens for connections from TCP network clients.
Definition tcp_listener.h:30
void start()
xtd::net::sockets::tcp_listener::starts listening for incoming connection requests.
bool exclusive_address_use() const
Gets a bool value that specifies whether the xtd::net::sockets::tcp_listener allows only one underlyi...
xtd::net::sockets::tcp_client accept_tcp_client()
Accepts a pending connection request.
xtd::net::sockets::socket accept_socket()
Accepts a pending connection request.
static tcp_listener create(uint16_t port)
Creates a new xtd::net::sockets::tcp_listener instance to listen on the specified port.
bool pending()
Determines if there are pending connection requests.
tcp_listener(const xtd::net::ip_address &ip_address, uint16_t port)
Initializes a new instance of the xtd::net::sockets::tcp_listener class that listens for incoming con...
xtd::net::sockets::socket server() const noexcept
Gets the underlying network xtd::net::sockets::socket.
tcp_listener & exclusive_address_use(bool value)
Sets a bool value that specifies whether the xtd::net::sockets::tcp_listener allows only one underlyi...
std::shared_ptr< xtd::iasync_result > begin_accept_socket(xtd::async_callback callback, const std::any &state)
Begins an asynchronous operation to accept an incoming connection attempt.
bool active() const noexcept
Gets a value that indicates whether xtd::net::sockets::tcp_listener is actively listening for client ...
xtd::net::sockets::socket end_accept_socket(std::shared_ptr< xtd::iasync_result > async_result)
Asynchronously accepts an incoming connection attempt and creates a new Socket to handle remote host ...
const xtd::net::end_point & local_end_point() const noexcept
Gets the underlying xtd::net::end_point of the current xtd::net::sockets::tcp_listener.
xtd::net::sockets::tcp_client end_accept_tcp_client(std::shared_ptr< xtd::iasync_result > async_result)
Asynchronously accepts an incoming connection attempt and creates a new xtd::net::sockets::tcp_client...
void stop()
Closes the listener.
tcp_listener(const xtd::net::ip_end_point &local_end_point)
Initializes a new instance of the xtd::net::sockets::tcp_listener class with the specified local endp...
std::shared_ptr< xtd::iasync_result > begin_accept_tcp_client(xtd::async_callback callback, const std::any &state)
Begins an asynchronous operation to accept an incoming connection attempt.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:26
virtual xtd::ustring to_string() const noexcept
Returns a std::string that represents the current object.
socket_error
Defines error codes for the xtd::net::sockets::socket class.
Definition socket_error.h:24
delegate< void(std::shared_ptr< xtd::iasync_result > ar)> async_callback
References a method to be called when a corresponding asynchronous operation completes.
Definition async_callback.h:18
@ success
Operation successful.
@ any
Indicates that all styles except allow_binary_specifier, allow_octal_specifier and allow_hex_specifie...
@ s
The S key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition system_report.h:17
Contains xtd::net::sockets::tcp_client class.