MCPcopy Create free account
hub / github.com/Tencent/UnLua / socket_connect

Function socket_connect

Plugins/UnLuaExtensions/LuaSocket/Source/src/wsocket.cpp:128–152  ·  view source on GitHub ↗

-------------------------------------------------------------------------*\ * Connects or returns error message \*-------------------------------------------------------------------------*/

Source from the content-addressed store, hash-verified

126* Connects or returns error message
127\*-------------------------------------------------------------------------*/
128int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) {
129 int err;
130 /* don't call on closed socket */
131 if (*ps == SOCKET_INVALID) return IO_CLOSED;
132 /* ask system to connect */
133 if (connect(*ps, addr, len) == 0) return IO_DONE;
134 /* make sure the system is trying to connect */
135 err = WSAGetLastError();
136 if (err != WSAEWOULDBLOCK && err != WSAEINPROGRESS) return err;
137 /* zero timeout case optimization */
138 if (timeout_iszero(tm)) return IO_TIMEOUT;
139 /* we wait until something happens */
140 err = socket_waitfd(ps, WAITFD_C, tm);
141 if (err == IO_CLOSED) {
142 int elen = sizeof(err);
143 /* give windows time to set the error (yes, disgusting) */
144 Sleep(10);
145 /* find out why we failed */
146 getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &elen);
147 /* we KNOW there was an error. if 'why' is 0, we will return
148 * "unknown error", but it's not really our fault */
149 return err > 0? err: IO_UNKNOWN;
150 } else return err;
151
152}
153
154/*-------------------------------------------------------------------------*\
155* Binds or returns error message

Callers

nothing calls this directly

Calls 1

socket_waitfdFunction · 0.70

Tested by

no test coverage detected