MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / create_socket

Function create_socket

examples/server/httplib.h:2642–2762  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2640
2641template <typename BindOrConnect>
2642socket_t create_socket(const std::string &host, const std::string &ip, int port,
2643 int address_family, int socket_flags, bool tcp_nodelay,
2644 SocketOptions socket_options,
2645 BindOrConnect bind_or_connect) {
2646 // Get address info
2647 const char *node = nullptr;
2648 struct addrinfo hints;
2649 struct addrinfo *result;
2650
2651 memset(&hints, 0, sizeof(struct addrinfo));
2652 hints.ai_socktype = SOCK_STREAM;
2653 hints.ai_protocol = 0;
2654
2655 if (!ip.empty()) {
2656 node = ip.c_str();
2657 // Ask getaddrinfo to convert IP in c-string to address
2658 hints.ai_family = AF_UNSPEC;
2659 hints.ai_flags = AI_NUMERICHOST;
2660 } else {
2661 if (!host.empty()) { node = host.c_str(); }
2662 hints.ai_family = address_family;
2663 hints.ai_flags = socket_flags;
2664 }
2665
2666#ifndef _WIN32
2667 if (hints.ai_family == AF_UNIX) {
2668 const auto addrlen = host.length();
2669 if (addrlen > sizeof(sockaddr_un::sun_path)) return INVALID_SOCKET;
2670
2671 auto sock = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol);
2672 if (sock != INVALID_SOCKET) {
2673 sockaddr_un addr{};
2674 addr.sun_family = AF_UNIX;
2675 std::copy(host.begin(), host.end(), addr.sun_path);
2676
2677 hints.ai_addr = reinterpret_cast<sockaddr *>(&addr);
2678 hints.ai_addrlen = static_cast<socklen_t>(
2679 sizeof(addr) - sizeof(addr.sun_path) + addrlen);
2680
2681 fcntl(sock, F_SETFD, FD_CLOEXEC);
2682 if (socket_options) { socket_options(sock); }
2683
2684 if (!bind_or_connect(sock, hints)) {
2685 close_socket(sock);
2686 sock = INVALID_SOCKET;
2687 }
2688 }
2689 return sock;
2690 }
2691#endif
2692
2693 auto service = std::to_string(port);
2694
2695 if (getaddrinfo(node, service.c_str(), &hints, &result)) {
2696#if defined __linux__ && !defined __ANDROID__
2697 res_init();
2698#endif
2699 return INVALID_SOCKET;

Callers 2

create_client_socketFunction · 0.85
create_server_socketMethod · 0.85

Calls 8

close_socketFunction · 0.85
lengthMethod · 0.80
to_stringFunction · 0.70
copyFunction · 0.50
emptyMethod · 0.45
c_strMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected