Skip to content

Commit

Permalink
C 代码就是这样乱用全局变量,风格很差很差
Browse files Browse the repository at this point in the history
  • Loading branch information
microcai committed Dec 17, 2024
1 parent 61a8d5d commit f46bd28
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions example/echo_server/echo_server_stackfull.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,19 @@ enum // configuration
// the completion port
static HANDLE cpl_port;

// the listening socket
static SOCKET listener;

// -----------------------------------------------------------------------------

// prototypes - main functions
static void init(void);
// prototypes - helper functions

static void bind_listening_socket(void);
static void bind_listening_socket(SOCKET);
static SOCKET create_accepting_socket(void);
static void create_io_completion_port(void);
static void create_listening_socket(void);
static SOCKET create_listening_socket(void);
static BOOL get_completion_status(DWORD*, ULONG_PTR*, OVERLAPPED**);
static void init_winsock(void);
static void prepare_endpoint(struct sockaddr_in*, u_long, u_short);
static void start_listening(void);
static void start_listening(SOCKET);

// -----------------------------------------------------------------------------

Expand All @@ -50,7 +46,7 @@ int main(void)

// -----------------------------------------------------------------------------

static void bind_listening_socket(void)
static void bind_listening_socket(SOCKET listener)
{
struct sockaddr_in sin;

Expand Down Expand Up @@ -92,9 +88,9 @@ static void create_io_completion_port(void)

// -----------------------------------------------------------------------------

static void create_listening_socket(void)
static SOCKET create_listening_socket(void)
{
listener = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0 , 0, WSA_FLAG_OVERLAPPED);
SOCKET listener = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0 , 0, WSA_FLAG_OVERLAPPED);
if (listener == INVALID_SOCKET)
{
printf("* error creating listening socket!\n");
Expand All @@ -107,6 +103,7 @@ static void create_listening_socket(void)
printf("* error %d in listener\n", err);
exit(1);
}
return listener;
}

static void echo_sever_client_session(void* param)
Expand Down Expand Up @@ -172,6 +169,7 @@ static void echo_sever_client_session(void* param)
static void accept_coroutine(void* param)
{
char addr_buf[1024];
SOCKET listener = (SOCKET)param;

for (;;)
{
Expand Down Expand Up @@ -206,9 +204,9 @@ static void init(void)
{
init_winsock();
create_io_completion_port();
create_listening_socket();
bind_listening_socket();
start_listening();
SOCKET listener = create_listening_socket();
bind_listening_socket(listener);
start_listening(listener);

#ifdef _WIN32
ConvertThreadToFiber(0);
Expand Down Expand Up @@ -243,7 +241,7 @@ static void prepare_endpoint(struct sockaddr_in* sin, u_long address,
sin->sin_port = htons(port);
}

static void start_listening(void)
static void start_listening(SOCKET listener)
{
if (listen(SOCKET_get_fd(listener), 100) == SOCKET_ERROR)
{
Expand Down

0 comments on commit f46bd28

Please sign in to comment.