Skip to content

Commit

Permalink
Simplify the implementation of NanaGet::LocalAria2Instance::PickUnuse…
Browse files Browse the repository at this point in the history
…dTcpPort method.
  • Loading branch information
MouriNaruto committed Sep 3, 2023
1 parent ac16f08 commit 2df7de3
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions NanaGet/NanaGetCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

#include "NanaGetCore.h"

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define _WINSOCKAPI_

#include <Windows.h>
#include <ShlObj.h>
Expand Down Expand Up @@ -890,11 +888,10 @@ std::uint16_t NanaGet::LocalAria2Instance::PickUnusedTcpPort()
{
std::uint16_t Result = 0;

WSADATA WSAData;
int Status = ::WSAStartup(
WSADATA WSAData = { 0 };
if (NO_ERROR == ::WSAStartup(
MAKEWORD(2, 2),
&WSAData);
if (ERROR_SUCCESS == Status)
&WSAData))
{
SOCKET ListenSocket = ::socket(
AF_INET,
Expand All @@ -906,22 +903,21 @@ std::uint16_t NanaGet::LocalAria2Instance::PickUnusedTcpPort()
Service.sin_family = AF_INET;
Service.sin_addr.s_addr = INADDR_ANY;
Service.sin_port = ::htons(0);
Status = ::bind(
if (SOCKET_ERROR != ::bind(
ListenSocket,
reinterpret_cast<LPSOCKADDR>(&Service),
sizeof(Service));
if (ERROR_SUCCESS == Status)
sizeof(Service)))
{
int NameLength = sizeof(Service);
Status = ::getsockname(
if (SOCKET_ERROR != ::getsockname(
ListenSocket,
reinterpret_cast<LPSOCKADDR>(&Service),
&NameLength);
if (ERROR_SUCCESS == Status)
&NameLength))
{
Result = ::ntohs(Service.sin_port);
}
}

::closesocket(ListenSocket);
}

Expand Down

0 comments on commit 2df7de3

Please sign in to comment.