| 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> |
| 24 | #include <iostream> |
| 25 | #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 main() { |
| 39 | dpp::cluster cl("no-token"); |
| 40 | auto se = dpp::create_socket_engine(&cl); |
| 41 | |
| 42 | const dpp::dns_cache_entry* addr = dpp::resolve_hostname("neuron.brainbox.cc", "80"); |
| 43 | std::cout << "Connect to IP: " << addr->resolved_addr << "\n"; |
| 44 | dpp::socket sfd = addr->make_connecting_socket(); |
| 45 | dpp::address_t destination = addr->get_connecting_address(80); |
| 46 | if (sfd == INVALID_SOCKET) { |
| 47 | std::cerr << "Couldn't create outbound socket on port 80\n"; |
| 48 | exit(1); |
| 49 | #ifndef _WIN32 |
| 50 | } else if (::connect(sfd, destination.get_socket_address(), destination.size()) != 0) { |
| 51 | #else |
| 52 | } else if (::WSAConnect(sfd, destination.get_socket_address(), destination.size(), nullptr, nullptr, nullptr, nullptr) != 0) { |
| 53 | #endif |
| 54 | dpp::close_socket(sfd); |
| 55 | std::cerr << "Couldn't connect outbound socket on port 80\n"; |
| 56 | exit(1); |
| 57 | } |
| 58 | |
| 59 | dpp::socket_events events( |
| 60 | sfd, |
| 61 | dpp::WANT_READ | dpp::WANT_WRITE | dpp::WANT_ERROR, |
| 62 | [&se](dpp::socket fd, const struct dpp::socket_events& e) { |
| 63 | int r = 0; |
| 64 | do { |
| 65 | char buf[128]{0}; |
| 66 | r = ::recv(e.fd, buf, sizeof(buf), 0); |
| 67 | if (r > 0) { |
| 68 | buf[127] = 0; |
| 69 | std::cout << buf; |
| 70 | std::cout.flush(); |
| 71 | } |
| 72 | } while (r > 0); |
| 73 | if (r == 0 || (errno && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) { |
| 74 | dpp::close_socket(fd); |
| 75 | se->delete_socket(fd); |
| 76 | } |
| 77 | }, |
| 78 | [](dpp::socket fd, const struct dpp::socket_events& e) { |
| 79 | std::cout << "WANT_WRITE event on socket " << fd << "\n"; |
| 80 | constexpr std::string_view request{"GET / HTTP/1.0\r\nConnection: close\r\n\r\n"}; |
| 81 | std::cout << "Writing: " << request.data() << "\n"; |
| 82 | auto written = ::send(e.fd, request.data(), request.length(), 0); |
| 83 | std::cout << "Written: " << written << "\n"; |
| 84 | }, |
| 85 | [](dpp::socket fd, const struct dpp::socket_events&, int error_code) { |
| 86 | std::cout << "WANT_ERROR event on socket " << fd << " with code " << error_code << "\n"; |
| 87 | } |
| 88 | ); |
| 89 | |
| 90 | se->register_socket(events); |
| 91 | |
| 92 | do { |
| 93 | se->process_events(); |
| 94 | } while (true); |
| 95 | } |