MCPcopy Create free account
hub / github.com/DescentDevelopers/Descent3 / nw_InitSockets

Function nw_InitSockets

networking/networking.cpp:735–799  ·  view source on GitHub ↗

Inits the sockets that the application will be using

Source from the content-addressed store, hash-verified

733
734// Inits the sockets that the application will be using
735void nw_InitSockets(uint16_t port) {
736
737 nw_ListenPort = port;
738 // UDP/TCP socket structure
739 SOCKADDR_IN sock_addr;
740
741 // Now do tcp!
742 TCP_active = 0;
743 TCP_socket = INVALID_SOCKET;
744 // TCP_reliable_socket = INVALID_SOCKET;
745 TCP_listen_socket = INVALID_SOCKET;
746
747 // Initialize the UDP socket
748 TCP_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
749
750 // Initialize all reliable sockets, IPX and TCP
751 nw_InitReliableSocket();
752
753 if (TCP_socket != INVALID_SOCKET) {
754 // bind the socket
755 memset(&sock_addr, 0, sizeof(SOCKADDR_IN));
756 sock_addr.sin_family = AF_INET;
757
758 uint32_t my_ip;
759
760 my_ip = nw_GetThisIP();
761
762 memcpy(&sock_addr.sin_addr.s_addr, &my_ip, sizeof(uint32_t));
763
764 sock_addr.sin_port = htons(port);
765 if (bind(TCP_socket, (SOCKADDR *)&sock_addr, sizeof(sock_addr)) == SOCKET_ERROR) {
766 mprintf(0, "Couldn't bind TCP socket (%d)! Invalidating TCP\n", WSAGetLastError());
767 goto tcp_done;
768 }
769
770 nw_SetSocketOptions(TCP_socket);
771 TCP_active = 1;
772 } else {
773 mprintf(0, "Cannot create TCP socket (%d)!\n", WSAGetLastError());
774 }
775
776tcp_done:
777 int ret;
778 uint32_t isocktrue = 1;
779
780 setsockopt(TCP_socket, SOL_SOCKET, SO_REUSEADDR, (LPSTR)&isocktrue, sizeof(isocktrue));
781 ret = setsockopt(TCP_socket, SOL_SOCKET, SO_BROADCAST, (LPSTR)&isocktrue, sizeof(uint32_t));
782 if (ret == SOCKET_ERROR) {
783 int wserr;
784 wserr = WSAGetLastError();
785 if ((wserr == WSAENOPROTOOPT) || (wserr == WSAEINVAL)) {
786 mprintf(0, "Unable to make socket broadcastable!");
787
788 Int3(); // Get Kevin
789 }
790 }
791
792 Sockets_initted = 1;

Callers 1

InitD3Systems1Function · 0.85

Calls 5

nw_InitReliableSocketFunction · 0.85
nw_GetThisIPFunction · 0.85
nw_SetSocketOptionsFunction · 0.85
nw_psnet_buffer_initFunction · 0.85
nw_RegisterCallbackFunction · 0.85

Tested by

no test coverage detected