create and bind the server socket, will reuse old port if supplied handle valid
| 103 | |
| 104 | // create and bind the server socket, will reuse old port if supplied handle valid |
| 105 | static void dmLogInitSocket( dmSocket::Socket& server_socket ) |
| 106 | { |
| 107 | if (!dLib::IsDebugMode() || !dLib::FeaturesSupported(DM_FEATURE_BIT_SOCKET_SERVER_TCP)) |
| 108 | return; |
| 109 | |
| 110 | dmSocket::Result r; |
| 111 | dmSocket::Address address; |
| 112 | uint16_t port = 0; |
| 113 | char error_msg[1024] = { 0 }; |
| 114 | |
| 115 | // If the env variable DM_LOG_PORT is set we will try to use |
| 116 | // that as port for the log server instead of a dynamic one. |
| 117 | const char* env_log_port = getenv("DM_LOG_PORT"); |
| 118 | if (env_log_port != 0x0) |
| 119 | { |
| 120 | long t_port = strtol(env_log_port, 0, 10); |
| 121 | if (t_port > 0 && t_port < UINT16_MAX) |
| 122 | { |
| 123 | port = t_port; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (server_socket != dmSocket::INVALID_SOCKET_HANDLE) |
| 128 | { |
| 129 | r = dmSocket::GetName(server_socket, &address, &port); // need to reuse the port |
| 130 | if (r != dmSocket::RESULT_OK) |
| 131 | { |
| 132 | snprintf(error_msg, sizeof(error_msg), "Unable to retrieve socket information (%d): %s", r, dmSocket::ResultToString(r)); |
| 133 | goto bail; |
| 134 | } |
| 135 | |
| 136 | r = dmSocket::Delete(server_socket); |
| 137 | server_socket = dmSocket::INVALID_SOCKET_HANDLE; |
| 138 | if (r != dmSocket::RESULT_OK) |
| 139 | { |
| 140 | snprintf(error_msg, sizeof(error_msg), "Unable to delete old log socket (%d): %s", r, dmSocket::ResultToString(r)); |
| 141 | goto bail; |
| 142 | } |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | r = dmSocket::GetHostByName(DM_UNIVERSAL_BIND_ADDRESS_IPV4, &address); |
| 147 | if (r != dmSocket::RESULT_OK) |
| 148 | { |
| 149 | snprintf(error_msg, sizeof(error_msg), "Unable to get listening address for log socket (%d): %s", r, dmSocket::ResultToString(r)); |
| 150 | goto bail; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | r = dmSocket::New(address.m_family, dmSocket::TYPE_STREAM, dmSocket::PROTOCOL_TCP, &server_socket); |
| 155 | if (r != dmSocket::RESULT_OK) |
| 156 | { |
| 157 | snprintf(error_msg, sizeof(error_msg), "Unable to create log socket (%d): %s", r, dmSocket::ResultToString(r)); |
| 158 | goto bail; |
| 159 | } |
| 160 | |
| 161 | dmSocket::SetReuseAddress(server_socket, true); |
| 162 | r = dmSocket::Bind(server_socket, address, port); |
no test coverage detected