MCPcopy Create free account
hub / github.com/defold/defold / ServerThread

Function ServerThread

engine/dlib/src/test/test_socket.cpp:73–126  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

71}
72
73static void ServerThread(void* arg)
74{
75 struct ServerThreadInfo* info = (struct ServerThreadInfo *) arg;
76 dmSocket::Result result = dmSocket::RESULT_OK;
77 dmSocket::Socket server_sock = -1;
78 dmSocket::Address server_addr;
79 dmSocket::Socket client_sock = -1;
80 dmSocket::Address client_addr;
81
82#define T_ASSERT_EQ(_A, _B) \
83 if ( (_A) != (_B) ) { \
84 printf("%s:%d: ASSERT: %s != %s: %d != %d", __FILE__, __LINE__, #_A, #_B, (_A), (_B)); \
85 } \
86 assert( (_A) == (_B) );
87
88 // Setup server socket and listen for a client to connect
89 result = dmSocket::New(info->domain, dmSocket::TYPE_STREAM, dmSocket::PROTOCOL_TCP, &server_sock);
90 T_ASSERT_EQ(dmSocket::RESULT_OK, result);
91
92 result = dmSocket::SetReuseAddress(server_sock, true);
93 T_ASSERT_EQ(dmSocket::RESULT_OK, result);
94
95 const char* hostname = dmSocket::IsSocketIPv4(server_sock) ? DM_LOOPBACK_ADDRESS_IPV4 : DM_LOOPBACK_ADDRESS_IPV6;
96 result = dmSocket::GetHostByName(hostname, &server_addr, dmSocket::IsSocketIPv4(server_sock), dmSocket::IsSocketIPv6(server_sock));
97 T_ASSERT_EQ(dmSocket::RESULT_OK, result);
98
99 result = dmSocket::Bind(server_sock, server_addr, info->port);
100 T_ASSERT_EQ(dmSocket::RESULT_OK, result);
101
102 result = dmSocket::Listen(server_sock, 1000); // Backlog = 1000
103 T_ASSERT_EQ(dmSocket::RESULT_OK, result);
104
105 // Wait for a client to connect
106 dmAtomicStore32(&info->listening, 1);
107 result = dmSocket::Accept(server_sock, &client_addr, &client_sock);
108 T_ASSERT_EQ(dmSocket::RESULT_OK, result);
109
110 // Send data to the client for verification
111 int value = 0x00def01d;
112 int written = 0;
113
114 result = dmSocket::Send(client_sock, &value, sizeof(value), &written);
115 T_ASSERT_EQ(dmSocket::RESULT_OK, result);
116 T_ASSERT_EQ((int) sizeof(value), written);
117
118 dmAtomicStore32(&info->sent, 1);
119
120 // Teardown
121 result = dmSocket::Delete(client_sock);
122 T_ASSERT_EQ(dmSocket::RESULT_OK, result);
123
124 result = dmSocket::Delete(server_sock);
125 T_ASSERT_EQ(dmSocket::RESULT_OK, result);
126}
127
128inline dmSocket::Socket GetSocket(dmSocket::Domain domain)
129{

Callers

nothing calls this directly

Calls 11

dmAtomicStore32Function · 0.85
NewFunction · 0.50
SetReuseAddressFunction · 0.50
IsSocketIPv4Function · 0.50
GetHostByNameFunction · 0.50
IsSocketIPv6Function · 0.50
BindFunction · 0.50
ListenFunction · 0.50
AcceptFunction · 0.50
SendFunction · 0.50
DeleteFunction · 0.50

Tested by

no test coverage detected