Revision 3188

View differences:

libsylph/socket.c
670 670
{
671 671
	gint ret;
672 672

  
673
#ifdef G_OS_UNIX
673
#ifdef G_OS_WIN32
674
	WSAEVENT hevent;
675
	gint err;
676
	DWORD dwret;
677
	WSANETWORKEVENTS events;
678

  
679
	errno = 0;
680

  
681
	hevent = WSACreateEvent();
682
	if (hevent == WSA_INVALID_EVENT)
683
		return -1;
684

  
685
	ret = WSAEventSelect(sock, hevent, FD_CONNECT);
686
	if (ret == SOCKET_ERROR) {
687
		g_warning("sock_connect_with_timeout: WSAEventSelect");
688
		WSACloseEvent(hevent);
689
		return -1;
690
	}
691

  
692
	ret = connect(sock, serv_addr, addrlen);
693

  
694
	if (ret == SOCKET_ERROR) {
695
		err = WSAGetLastError();
696
		if (err != WSAEWOULDBLOCK) {
697
			g_warning("sock_connect_with_timeout: connect (%d)", err);
698
			ret = -1;
699
			goto end;
700
		}
701
	}
702

  
703
	dwret = WSAWaitForMultipleEvents(1, &hevent, FALSE, timeout_secs * 1000, FALSE);
704
	if (dwret == WSA_WAIT_TIMEOUT) {
705
		g_warning("sock_connect_with_timeout: timeout");
706
		errno = WSAETIMEDOUT;
707
		ret = -1;
708
		goto end;
709
	} else if (dwret != WSA_WAIT_EVENT_0) {
710
		g_warning("sock_connect_with_timeout: WSAWaitForMultipleEvents (%d)", dwret);
711
		ret = -1;
712
		goto end;
713
	}
714

  
715
	ret = WSAEnumNetworkEvents(sock, hevent, &events);
716
	if (ret == SOCKET_ERROR) {
717
		g_warning("sock_connect_with_timeout: WSAEnumNetworkEvents (%d)", ret);
718
		ret = -1;
719
		goto end;
720
	}
721

  
722
	if ((events.lNetworkEvents & FD_CONNECT) &&
723
	     events.iErrorCode[FD_CONNECT_BIT] == 0) {
724
		ret = 0;
725
		errno = 0;
726
	} else
727
		ret = -1;
728

  
729
end:
730
	WSAEventSelect(sock, NULL, 0);
731
	WSACloseEvent(hevent);
732

  
733
	set_nonblocking_mode(sock, FALSE);
734
#else
674 735
	set_nonblocking_mode(sock, TRUE);
675
#endif
676 736

  
677 737
	ret = connect(sock, serv_addr, addrlen);
678 738

  
679
#ifdef G_OS_UNIX
680 739
	if (ret < 0) {
681 740
		if (EINPROGRESS == errno) {
682 741
			fd_set fds;
ChangeLog
1
2012-11-20
2

  
3
	* libsylph/socket.c: sock_connect_with_timeout(): enabled timeout
4
	  setting on Windows.
5

  
1 6
2012-10-30
2 7

  
3 8
	* version 3.3.0

Also available in: Unified diff