void UDPSocket::setPeer(const IPV4Host &ia, tpport_t port) { memset(&peer.ipv4, 0, sizeof(peer.ipv4)); peer.ipv4.sin_family = AF_INET; peer.ipv4.sin_addr = ia.getAddress(); peer.ipv4.sin_port = htons(port); }
Socket::Error UDPReceive::connect(const IPV4Host &ia, tpport_t port) { int len = sizeof(peer.ipv4); peer.ipv4.sin_family = AF_INET; peer.ipv4.sin_addr = ia.getAddress(); peer.ipv4.sin_port = htons(port); // Win32 will crash if you try to connect to INADDR_ANY. if ( INADDR_ANY == peer.ipv4.sin_addr.s_addr ) peer.ipv4.sin_addr.s_addr = INADDR_LOOPBACK; if(::connect(so, (struct sockaddr *)&peer.ipv4, len)) return connectError(); return errSuccess; }
TCPSession::TCPSession(const IPV4Host &ia, tpport_t port, size_t size, int pri, size_t stack) : Thread(pri, stack), TCPStream(IPV4) { setCompletion(false); setError(false); allocate(size); size_t i; for(i = 0 ; i < ia.getAddressCount(); i++) { struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr = ia.getAddress(i); addr.sin_port = htons(port); // Win32 will crash if you try to connect to INADDR_ANY. if ( INADDR_ANY == addr.sin_addr.s_addr ) addr.sin_addr.s_addr = INADDR_LOOPBACK; if(::connect(so, (struct sockaddr *)&addr, (socklen_t)sizeof(addr)) == 0) break; #ifdef _MSWINDOWS_ if(WSAGetLastError() == WSAEISCONN || WSAGetLastError() == WSAEWOULDBLOCK) #else if(errno == EINPROGRESS) #endif { Socket::state = CONNECTING; return; } } if(i == ia.getAddressCount()) { endSocket(); Socket::state = INITIAL; return; } setCompletion(true); Socket::state = CONNECTED; }
Socket::Error UDPReceive::connect(const IPV4Host &ia, tpport_t port) { return connect(ucommon::Socket::address(ia.getAddress(), port)); }
void UDPSocket::setPeer(const IPV4Host &ia, tpport_t port) { peer = ucommon::Socket::address(ia.getAddress(), port); }
void TCPStream::connect(const IPV4Host &host, tpport_t port, unsigned mss) { size_t i; fd_set fds; struct timeval to; bool connected = false; int rtn; int sockopt; socklen_t len = sizeof(sockopt); #ifdef TCP_MAXSEG if(mss) setsockopt(so, IPPROTO_TCP, TCP_MAXSEG, (char *)&mss, sizeof(mss)); #endif for(i = 0 ; i < host.getAddressCount(); i++) { struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr = host.getAddress(i); addr.sin_port = htons(port); if(timeout) setCompletion(false); // Win32 will crash if you try to connect to INADDR_ANY. if ( INADDR_ANY == addr.sin_addr.s_addr ) addr.sin_addr.s_addr = INADDR_LOOPBACK; rtn = ::connect(so, (struct sockaddr *)&addr, (socklen_t)sizeof(addr)); if(!rtn) { connected = true; break; } #ifndef _MSWINDOWS_ if(errno == EINPROGRESS) #else if(WSAGetLastError() == WSAEINPROGRESS) #endif { FD_ZERO(&fds); FD_SET(so, &fds); to.tv_sec = timeout / 1000; to.tv_usec = timeout % 1000 * 1000; // timeout check for connect completion if(::select((int)so + 1, NULL, &fds, NULL, &to) < 1) continue; getsockopt(so, SOL_SOCKET, SO_ERROR, (char *)&sockopt, &len); if(!sockopt) { connected = true; break; } endSocket(); so = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(so == INVALID_SOCKET) break; } } setCompletion(true); if(!connected) { rtn = errno; endStream(); errno = rtn; connectError(); return; } segmentBuffering(mss); Socket::state = CONNECTED; }