Debian Codegraph @

src/sockettest/socket.cpp

raw symbols SCIP
1 /************************************************************************************
2 *
3 * D++, A Lightweight C++ library for Discord
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 * Copyright 2021 Craig Edwards and D++ contributors
7 * (https://github.com/brainboxdotcc/DPP/graphs/contributors)
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 ************************************************************************************/
22
23 #include <dpp/dpp.h>cxx . . $ `<file>/include/dpp/dpp.h`/
24 #include <iostream>
25 #include <dpp/dns.h>cxx . . $ `<file>/include/dpp/dns.h`/
26 #include <string_view>
27 #ifndef _WIN32
28 #include <unistd.h>
29 #else
30 /* Windows-specific sockets includes */
31 #include <WinSock2.h>
32 #include <WS2tcpip.h>
33 #include <io.h>
34 /* Windows sockets library */
35 #pragma comment(lib, "ws2_32")
36 #endif
37
38 int maincxx . . $ main(b126dc7c1de90089).() {
39 dppcxx . . $ dpp/::clustercxx . . $ dpp/cluster# clcxx . . $ dpp/cluster#cluster(13cb741d36ace229).("no-token"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
40 auto se = dppcxx . . $ dpp/::create_socket_enginecxx . . $ dpp/create_socket_engine(d5f6969764d0cd28).(&cl);
41
42 const dppcxx . . $ dpp/::dns_cache_entrycxx . . $ dpp/dns_cache_entry#* addr = dppcxx . . $ dpp/::resolve_hostnamecxx . . $ dpp/resolve_hostname(b0ff627c08867a3f).("neuron.brainbox.cc"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "80"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
43 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Connect to IP: " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). addr->resolved_addrcxx . . $ dpp/dns_cache_entry#resolved_addr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n";
44 dppcxx . . $ dpp/::socketcxx . . $ dpp/socket# sfd = addr->make_connecting_socketcxx . . $ dpp/dns_cache_entry#make_connecting_socket(455f465bc33b4cdf).();
45 dppcxx . . $ dpp/::address_tcxx . . $ dpp/address_t# destination = addr->get_connecting_addresscxx . . $ dpp/dns_cache_entry#get_connecting_address(a1e46e487284a381).(80);
46 if (sfd == INVALID_SOCKETcxx . . $ `include/dpp/socket.h:54:9`!) {
47 stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Couldn't create outbound socket on port 80\n";
48 exit(1);
49 #ifndef _WIN32
50 } else if (::connect(sfd, destination.get_socket_addresscxx . . $ dpp/address_t#get_socket_address(62da9280157a6e33).(), destination.sizecxx . . $ dpp/address_t#size(f708dce9a8856c70).()) != 0) {
51 #else
52 } else if (::WSAConnect(sfd, destination.get_socket_address(), destination.size(), nullptr, nullptr, nullptr, nullptr) != 0) {
53 #endif
54 dppcxx . . $ dpp/::close_socketcxx . . $ dpp/close_socket(83c267b20bac841a).(sfd);
55 stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Couldn't connect outbound socket on port 80\n";
56 exit(1);
57 }
58
59 dppcxx . . $ dpp/::socket_eventscxx . . $ dpp/socket_events# eventscxx . . $ dpp/socket_events#socket_events(aee884f1e436864).(
60 sfd,
61 dppcxx . . $ dpp/::WANT_READcxx . . $ dpp/WANT_READ. | dppcxx . . $ dpp/::WANT_WRITEcxx . . $ dpp/WANT_WRITE. | dppcxx . . $ dpp/::WANT_ERRORcxx . . $ dpp/WANT_ERROR.,
62 [cxx . . $ std/function#function(bdc3712e480e4d04).&se](dppcxx . . $ dpp/::socketcxx . . $ dpp/socket# fd, const struct dppcxx . . $ dpp/::socket_eventscxx . . $ dpp/socket_events#& e) {
63 int r = 0;
64 do {
65 char buf[128]{0};
66 r = ::recv(e.fdcxx . . $ dpp/socket_events#fd., buf, sizeof(buf), 0);
67 if (r > 0) {
68 buf[127] = 0;
69 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). buf;
70 stdcxx . . $ std/::coutcxx . . $ std/cout..flushcxx . . $ std/basic_ostream#flush(e66e05a7745427d2).();
71 }
72 } while (r > 0);
73 if (r == 0 || (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EAGAINcxx . . $ `<external>/8987895939074593727/errno-base.h:15:9`! && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EWOULDBLOCKcxx . . $ `<external>/17922322188510340118/errno.h:22:9`! && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != EINTRcxx . . $ `<external>/8987895939074593727/errno-base.h:8:9`!)) {
74 dppcxx . . $ dpp/::close_socketcxx . . $ dpp/close_socket(83c267b20bac841a).(fd);
75 se->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).delete_socketcxx . . $ dpp/socket_engine_base#delete_socket(83c267b20bac841a).(fd);
76 }
77 },
78 [cxx . . $ std/function#function(bdc3712e480e4d04).](dppcxx . . $ dpp/::socketcxx . . $ dpp/socket# fd, const struct dppcxx . . $ dpp/::socket_eventscxx . . $ dpp/socket_events#& e) {
79 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "WANT_WRITE event on socket " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). fd <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n";
80 constexpr stdcxx . . $ std/::string_viewcxx . . $ std/string_view# requestcxx . . $ std/basic_string_view#basic_string_view(837a1ee5a10cb792).{"GET / HTTP/1.0\r\nConnection: close\r\n\r\n"};
81 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Writing: " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). request.datacxx . . $ std/basic_string_view#data(8e614c7de0b1573e).() <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n";
82 auto written = ::send(e.fdcxx . . $ dpp/socket_events#fd., request.datacxx . . $ std/basic_string_view#data(8e614c7de0b1573e).(), request.lengthcxx . . $ std/basic_string_view#length(c9b1e749b64d5edc).(), 0);
83 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Written: " <<cxx . . $ std/basic_ostream#`operator<<`(761e6a6e6922c86). written <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n";
84 },
85 [cxx . . $ std/function#function(bdc3712e480e4d04).](dppcxx . . $ dpp/::socketcxx . . $ dpp/socket# fd, const struct dppcxx . . $ dpp/::socket_eventscxx . . $ dpp/socket_events#&, int error_code) {
86 stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "WANT_ERROR event on socket " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). fd <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " with code " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). error_code <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n";
87 }
88 );
89
90 se->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).register_socketcxx . . $ dpp/socket_engine_base#register_socket(b90c5fd4f77d1923).(events);
91
92 do {
93 se->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).process_eventscxx . . $ dpp/socket_engine_base#process_events(49f6e7a06ebc5aa8).();
94 } while (true);
95 }