void TCPStream::connect(const char *target, unsigned mss) { char namebuf[128]; char *cp; struct servent *svc; tpport_t port; snprintf(namebuf, sizeof(namebuf), "%s", target); cp = strrchr(namebuf, '/'); if(!cp) cp = strrchr(namebuf, ':'); if(!cp) { endStream(); connectError(); return; } *(cp++) = 0; if(isdigit(*cp)) port = atoi(cp); else { mutex.enter(); svc = getservbyname(cp, "tcp"); if(svc) port = ntohs(svc->s_port); mutex.leave(); if(!svc) { endStream(); connectError(); return; } } switch(family) { case IPV4: connect(IPV4Host(namebuf), port, mss); break; #ifdef CCXX_IPV6 case IPV6: connect(IPV6Host(namebuf), port, mss); break; #endif default: endStream(); connectError(); } }
void TCPStream::disconnect(void) { if(Socket::state == AVAILABLE) return; endStream(); so = socket(family, SOCK_STREAM, IPPROTO_TCP); if(so != INVALID_SOCKET) Socket::state = AVAILABLE; }
bool ConnectionAutomaton::endStreamInternal( StreamId streamId, StreamCompletionSignal signal) { VLOG(6) << "endStreamInternal"; auto it = streams_.find(streamId); if (it == streams_.end()) { // Unsubscribe handshake initiated by the connection, we're done. return false; } // Remove from the map before notifying the automaton. auto automaton = it->second; streams_.erase(it); automaton->endStream(signal); return true; }
GeneralWriter :: ~GeneralWriter () { try { endStream (); } catch ( ... ) { } delete [] output_buffer; delete [] packing_buffer; output_bsize = output_marker = 0; output_buffer = packing_buffer = 0; }
void TCPStream::connect(TCPV6Socket &tcpip) { tpport_t port; endStream(); family = IPV6; so = accept(tcpip.getSocket(), NULL, NULL); if(so == INVALID_SOCKET) return; IPV6Host host = getIPV6Peer(&port); if(!tcpip.onAccept(host, port)) { endSocket(); iostream::clear(ios::failbit | rdstate()); return; } segmentBuffering(tcpip.getSegmentSize()); Socket::state = CONNECTED; }
int GzipBuf::processFile( int type, const char * pFileName, const char * pCompressFileName ) { int fd; int ret = 0; fd = open( pFileName, O_RDONLY ); if ( fd == -1 ) return -1; VMemBuf gzFile; ret = gzFile.set( pCompressFileName, -1 ); if ( !ret ) { setCompressCache( &gzFile ); if ( ((ret = init( type, 6 )) == 0 )&&((ret = beginStream()) == 0 )) { int len; char achBuf[16384]; while( true ) { len = nio_read( fd, achBuf, sizeof( achBuf ) ); if ( len <= 0 ) break; if ( this->write( achBuf, len ) ) { ret = -1; break; } } if ( !ret ) { ret = endStream(); long size; if ( !ret ) ret = gzFile.exactSize( &size ); } gzFile.close(); } } ::close( fd ); return ret; }
void WAVStream::run() { //char chunkID[5]; //chunkID[4] = 0; //input_->getStream()->read(chunkID, 4); //cout << "CHUNK ID: " << chunkID << endl; streamInfo_ = StreamInfo(); string chunkId = readString(4); if (chunkId.compare(WAVFormat::CHUNK_ID) != 0) { cerr << "ERROR: Invalid chunk ID. Stream may not be in WAV format." << endl; return; } int32_t chunkSize = readInt32(); string chunkFormat = readString(4); if (chunkFormat.compare(WAVFormat::CHUNK_FORMAT) != 0) { cerr << "ERROR: Invalid chunk format. Stream may not be in WAV format." << endl; return; } chunkSize -= 4; formatRead_ = false; dataRead_ = false; dataInfo_ = DataInfo(); while (chunkSize > 0) { chunkSize -= readSubchunk(); } endStream(); //if (backend_.isNotNull()) // backend_->endStream(); }
void TCPStream::connect(const IPV6Host &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_in6 addr; memset(&addr, 0, sizeof(addr)); addr.sin6_family = AF_INET6; addr.sin6_addr = host.getAddress(i); addr.sin6_port = htons(port); if(timeout) setCompletion(false); // Win32 will crash if you try to connect to INADDR_ANY. if ( !memcmp(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any))) memcpy(&addr.sin6_addr, &in6addr_loopback, sizeof(in6addr_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_INET6, 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; }
TCPStream::TCPStream(TCPSocket &server, bool throwflag, timeout_t to) : streambuf(), Socket(accept(server.getSocket(), NULL, NULL)), #ifdef OLD_IOSTREAM iostream() #else iostream((streambuf *)this) #endif ,bufsize(0) ,gbuf(NULL) ,pbuf(NULL) { tpport_t port; family = IPV4; #ifdef OLD_IOSTREAM init((streambuf *)this); #endif timeout = to; setError(throwflag); IPV4Host host = getPeer(&port); if(!server.onAccept(host, port)) { endSocket(); error(errConnectRejected); iostream::clear(ios::failbit | rdstate()); return; } segmentBuffering(server.getSegmentSize()); Socket::state = CONNECTED; } #ifdef CCXX_IPV6 TCPStream::TCPStream(TCPV6Socket &server, bool throwflag, timeout_t to) : streambuf(), Socket(accept(server.getSocket(), NULL, NULL)), #ifdef OLD_IOSTREAM iostream() #else iostream((streambuf *)this) #endif ,bufsize(0) ,gbuf(NULL) ,pbuf(NULL) { tpport_t port; family = IPV6; #ifdef OLD_IOSTREAM init((streambuf *)this); #endif timeout = to; setError(throwflag); IPV6Host host = getIPV6Peer(&port); if(!server.onAccept(host, port)) { endSocket(); error(errConnectRejected); iostream::clear(ios::failbit | rdstate()); return; } segmentBuffering(server.getSegmentSize()); Socket::state = CONNECTED; } #endif TCPStream::TCPStream(const IPV4Host &host, tpport_t port, unsigned size, bool throwflag, timeout_t to) : streambuf(), Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP), #ifdef OLD_IOSTREAM iostream(), #else iostream((streambuf *)this), #endif bufsize(0),gbuf(NULL),pbuf(NULL) { #ifdef OLD_IOSTREAM init((streambuf *)this); #endif family = IPV4; timeout = to; setError(throwflag); connect(host, port, size); } #ifdef CCXX_IPV6 TCPStream::TCPStream(const IPV6Host &host, tpport_t port, unsigned size, bool throwflag, timeout_t to) : streambuf(), Socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP), #ifdef OLD_IOSTREAM iostream(), #else iostream((streambuf *)this), #endif bufsize(0),gbuf(NULL),pbuf(NULL) { family = IPV6; #ifdef OLD_IOSTREAM init((streambuf *)this); #endif timeout = to; setError(throwflag); connect(host, port, size); } #endif TCPStream::~TCPStream() { #ifdef CCXX_EXCEPTIONS try { endStream(); } catch( ... ) { if ( ! std::uncaught_exception()) throw;}; #else endStream(); #endif } #ifdef HAVE_GETADDRINFO void TCPStream::connect(const char *target, unsigned mss) { char namebuf[128]; char *cp; struct addrinfo hint, *list = NULL, *next, *first; bool connected = false; snprintf(namebuf, sizeof(namebuf), "%s", target); cp = strrchr(namebuf, '/'); if(!cp) cp = strrchr(namebuf, ':'); if(!cp) { endStream(); connectError(); return; } *(cp++) = 0; memset(&hint, 0, sizeof(hint)); hint.ai_family = family; hint.ai_socktype = SOCK_STREAM; hint.ai_protocol = IPPROTO_TCP; if(getaddrinfo(namebuf, cp, &hint, &list) || !list) { endStream(); connectError(); return; } first = list; #ifdef TCP_MAXSEG if(mss) setsockopt(so, IPPROTO_TCP, TCP_MAXSEG, (char *)&mss, sizeof(mss)); #endif while(list) { if(!::connect(so, list->ai_addr, (socklen_t)list->ai_addrlen)) { connected = true; break; } next = list->ai_next; list = next; } freeaddrinfo(first); if(!connected) { endStream(); connectError(); return; } segmentBuffering(mss); Socket::state = CONNECTED; }
TCPSession::~TCPSession() { endStream(); }