Esempio n. 1
0
int gzfilebuf::overflow( int c ) {

  if ( !is_open() || !(mode & ios::out) )
    return EOF;

  if ( !base() ) {
    if ( allocate() == EOF )
      return EOF;
    setg(0,0,0);
  } else {
    if (in_avail()) {
	return EOF;
    }
    if (out_waiting()) {
      if (flushbuf() == EOF)
	return EOF;
    }
  }

  int bl = blen();
  setp( base(), base() + bl);

  if ( c != EOF ) {

    *pptr() = c;
    pbump(1);

  }

  return 0;

}
Esempio n. 2
0
int	
PRfilebuf::underflow()
{
    int count;
    unsigned char tbuf;

    if (in_avail())
        return (int)(unsigned char) *gptr();

    if (allocate()==EOF)        // make sure there is a reserve area
        return EOF;
    if (PRfilebuf::sync()==EOF)
        return EOF;

    if (unbuffered())
        {
        if (PR_Read(_fd,(void *)&tbuf,1)<=0)
            return EOF;
        return (int)tbuf;
        }

    if ((count=PR_Read(_fd,(void *)base(),blen())) <= 0)
        return EOF;     // reached EOF
    setg(base(),base(),base()+count);
    return (int)(unsigned char) *gptr();
}
Esempio n. 3
0
int rpcbuf::underflow() {
    if (!_opened || allocate() == EOF) {
	return EOF;
    }

    if (overflow() == EOF) {
	return EOF;
    }

    int nunread = in_avail();
    if (nunread) {
	Memory::copy(gptr(), eback(), nunread);
    }
    setg(eback(), eback(), eback() + nunread);

    int nread = read(_fd, egptr(), ebuf() - egptr());
    if (nread < 0) {
	sys_error("rpcbuf::underflow: read");
	return EOF;
    }
    if (nread == 0) {
	return EOF;
    }

    setg(eback(), gptr(), egptr() + nread);
    return zapeof(*gptr());
}
// ---------
streamsize CEEStdStreamBufProxy::xsgetn(TCHAR *dest, streamsize sSize)
{
	int nRead = 0;
	int avail;

	while (sSize)
	{
		// get number of character available
		avail = in_avail();
		if (!avail)
		{
			if (underflow() == EOF)
				break;
		}

		if (avail > sSize)
		{
			avail = sSize;
		}

		memcpy(dest + nRead, gptr(), avail);
		gbump(avail);

		nRead += avail;
		sSize -= avail;
	}

	return nRead;
}
Esempio n. 5
0
oskit_size_t out_avail(lmm_flags_t flags)
{
        oskit_size_t r;
        mem_lock();
        r = in_avail(flags);
        mem_unlock();
        return r;
}
Esempio n. 6
0
bool TCPStream::isPending(Pending pending, timeout_t timer)
{
    if(pending == pendingInput && in_avail())
        return true;
    else if(pending == pendingOutput)
        flush();

    return Socket::isPending(pending, timer);
}
Esempio n. 7
0
/***
*virtual int filebuf::sync() - synchronize buffers with external file postion.
*
*Purpose:
*	Synchronizes buffer with external file, by flushing any output and/or
*	discarding any unread input data.  Discards any get or put area(s).
*
*Entry:
*	None.
*
*Exit:
*	Returns EOF if error, else 0.
*
*Exceptions:
*	Returns EOF if error.
*
*******************************************************************************/
int filebuf::sync()
{
        long count, nout;
        char * p;
        if (x_fd==-1)
            return(EOF);

        if (!unbuffered())
        {
            if ((count=out_waiting())!=0)
            {
                if ((nout =_write(x_fd,(void *) pbase(),(unsigned int)count)) != count)
                {
                    if (nout > 0) {
                        // should set _pptr -= nout
                        pbump(-(int)nout);
                        memmove(pbase(), pbase()+nout, (int)(count-nout));
                    }
                    return(EOF);
                }
            }
            setp(0,0); // empty put area

            if ((count=in_avail()) > 0)
            {
                // can't use seekoff here!!
#ifdef  _WIN32
                if (_osfile(x_fd) & FTEXT)
#else
                if (_osfile[x_fd] & FTEXT)
#endif
                {
                    // If text mode, need to account for CR/LF etc.
                    for (p = gptr(); p < egptr(); p++)
                        if (*p == '\n')
                            count++;

                    // account for EOF if read, not counted by _read
#ifdef  _WIN32
                    if ((_osfile(x_fd) & FEOFLAG))
#else
                    if ((_osfile[x_fd] & FEOFLAG))
#endif
                        count++;

                }
                if (_lseek(x_fd, -count, SEEK_CUR)==-1L)
                {
//                  unlock();
                    return (EOF);
                }
            }
            setg(0,0,0); // empty get area
        }
//      unlock();
        return(0);
}
int filebuf::underflow()
{
    if( ! opened  ||  (mode & (ios::in | ios::out)) == ios::out )
        return EOF;

    if( in_avail() )    // no action needed
        return (unsigned char) *gptr();

    int c;  // the return value
    int count;  // input character count

    if ( ! unbuffered()  &&  base() )
        {     // this is buffered

        if (sync() != 0)
                return EOF;

        // find buffer data
        int pb = (blen() > 8) ? 4 : 1;  // putback area size
        char *b = base();

        // read in a new buffer
        count = ::read(xfd, b+pb, blen()-pb);
        if( count == OS_err )
            return EOF;

        // set up get and put areas
        setg(b, b+pb, b+pb+count);
        setp(b+pb, b+pb);

        if( count )
            c = (unsigned char) *gptr();
        }
    else
        {     // this is not buffered
        count = ::read(xfd, lahead, 1);
        if( count == OS_err)
            {
            c = EOF;
            setg(0, 0, 0);
            }
        else
            {
            c = (unsigned char)lahead[0];
            setg(lahead, lahead, lahead+1);
            }
        }

    if( ! count )
        c = EOF;    // end of file

    return c;
}
Esempio n. 9
0
int rpcbuf::read_request() {
    if (!_mystream) {
	return EOF;
    }

    if (!_actualWidth) {
	char* orig = pptr();
	const int length = 0;
	mystream().width(FIELDWIDTH);
	mystream() << length;
	_actualWidth = pptr() - orig;
	pbump(orig - pptr());
    }

    int navail = in_avail();
    if (navail < _actualWidth) {
	return EOF;
    }

    char* orig = gptr();
    int length = 0;
    mystream() >> length;
    gbump(orig - gptr());

    if (length <= 0) {
	error("rpcbuf::read_request: zero or negative length");
	return EOF;
    }

    if (length > ebuf() - eback() && !expand_g(length * 2)) {
	error("rpcbuf::read_request: out of memory");
	return EOF;
    }

    if (navail < length) {
	return EOF;
    } else {
	return 0;
    }
}
Esempio n. 10
0
int 
PRfilebuf::sync()
{
    PRInt32 count; 

    if (_fd==0)
        return(EOF);

    if (!unbuffered()){
        // Sync write area
        if ((count=out_waiting())!=0){
            PRInt32 nout;
            if ((nout =PR_Write(_fd,
                               (void *) pbase(),
                               (unsigned int)count)) != count){
                if (nout > 0) {
                    // should set _pptr -= nout
                    pbump(-(int)nout);
                    memmove(pbase(), pbase()+nout, (int)(count-nout));
                }
                return(EOF);
            }
        }
        setp(0,0); // empty put area

        if (PR_GetDescType(_fd) == PR_DESC_FILE){
            // Sockets can't seek; don't need this
            if ((count=in_avail()) > 0){
                if (PR_Seek(_fd, -count, PR_SEEK_CUR)!=-1L)
                {
                    return (EOF);
                }
            }
        }
        setg(0,0,0); // empty get area
    }
    return(0);
}
Esempio n. 11
0
int gzfilebuf::underflow() {

  // If the file hasn't been opened for reading, error.
  if ( !is_open() || !(mode & ios::in) )
    return EOF;

  // if a buffer doesn't exists, allocate one.
  if ( !base() ) {

    if ( (allocate()) == EOF )
      return EOF;
    setp(0,0);

  } else {

    if ( in_avail() )
      return (unsigned char) *gptr();

    if ( out_waiting() ) {
      if ( flushbuf() == EOF )
	return EOF;
    }

  }

  // Attempt to fill the buffer.

  int result = fillbuf();
  if ( result == EOF ) {
    // disable get area
    setg(0,0,0);
    return EOF;
  }

  return (unsigned char) *gptr();

}
Esempio n. 12
0
void * sp_in_thread::Entry()
{
   while ( !this->TestDestroy()){
      if ( idx == 999){
         idx = 0;
      }
      auto & app = wxGetApp();
      bool new_data = false;
      char ch;
      { // CS
         wxCriticalSectionLocker lock(app.m_sp_CS);
         if ( app.have_sp() ){
            auto sp = app.get_sp();
            if ( sp->in_avail() ){
               unsigned  char uch;
               sp->read(&uch,1);
               ch = (char) uch;
               new_data = true;
            }
         }
      }// ~CS
      if (new_data){
         if ( ch == '\n'){
            buffer[idx] = '\0';
            idx = 0;
            parse_buffer();
         }else{
            buffer[idx] = ch;
            ++idx;
         }
      }
   }
   wxCriticalSectionLocker(m_main_frame->m_thread_CS);
   m_main_frame->m_sp_in_thread = nullptr;
   return nullptr;
}
Esempio n. 13
0
streampos stdiobuf::seekoff(streamoff off, ios::seek_dir dir, int /*mode ignored*/)
{
    if( out_waiting()  ||  in_avail() )
        {
        if( sync() == EOF )
            return EOF;
        }

    int w;
    if( dir == ios::beg )
        w = SEEK_SET;
    else if( dir == ios::end )
        w = SEEK_END;
    else
        w = SEEK_CUR;
    w = fseek(sio, off, w);
    char *b = base();
    if( ! unbuffered()  &&  b )
        {   // reset get and put areas
        setp(b+4, b+4);
        setg(b, b+4, b+4);
        }
    return w ? EOF : ftell(sio);
}
Esempio n. 14
0
  int filebuf::overflow( int c ) {

    unsigned int    waiting;
    int             written;
    streamoff       offset;

    __lock_it( __b_lock );

    // Flush any input waiting in the buffer:
    if( in_avail() > 0 ) {
        // note that sync() will discard get area
        if( sync() == EOF ) {
            return( EOF );
        }
    } else {
        // discard get area
        setg( NULL, NULL, NULL );
    }

    // Try to allocate a buffer:
    if( base() == NULL ) {
        if( allocate() == EOF ) {
            return( EOF );
        }
        if( base() == NULL ) {
            if( c != EOF ) {
                char charbuf = (char)c;
                written = ::write( fd(), &charbuf, sizeof( charbuf ) );
                return( written == sizeof( charbuf ) ? __NOT_EOF : EOF );
            }
            return( __NOT_EOF );
        } else {
            setp( base(), ebuf() );
        }
    } else if( pptr() <= pbase() ) {
        setp( base(), ebuf() );
    }

    // if appending, remember current offset and seek to end
    if( __file_mode & ios::app ) {
        offset = tell( fd() );
        if( offset < 0 ) {
            return( EOF );
        }
        if( lseek( fd(), 0, ios::end ) < 0 ) {
            return( EOF );
        }
    }

    // Now write the output:
    if( (c != EOF) && (pptr() < epptr()) ) {
        *(pptr()) = (char)c;
        pbump( 1 );
        c = EOF;
    }
    waiting = (__huge_ptr_int)(pptr() - pbase());
    while( waiting > 0 ) {
        written = ::write( fd(), pbase(), (waiting>INT_MAX?INT_MAX:waiting) );
        if( written == -1 ) {
            return( EOF );
        } else if( written == 0 ) {
            break;
        }
        waiting -= written;
        if( waiting > 0 ) {
            ::memmove( pbase(), pbase() + written, waiting );
            setp( pbase(), epptr() );
            pbump( waiting );
        } else {
            setp( pbase(), epptr() );
        }
    }
    if( c != EOF ) {
        if( pptr() < epptr() ) {
            *(pptr()) = (char)c;
            pbump( 1 );
        } else {
            return( EOF );
        }
    }

    // if appending, restore current offset
    if( __file_mode & ios::app ) {
        if( lseek( fd(), offset, ios::beg ) < 0 ) {
            return( EOF );
        }
    }

    if( waiting ) {
        return( EOF );
    }
    return( __NOT_EOF );
  }
Esempio n. 15
0
oskit_size_t out_avail(lmm_flags_t flags)
{
        oskit_size_t r;
        r = in_avail(flags);
        return r;
}
Esempio n. 16
0
std::streamsize Socket::showmanyc() {
	#ifdef WIN32
	unsigned long result = 0;
	if(ioctlsocket(handle, FIONREAD, &result)) {
	#else
	int result = 0;
	if(ioctl(handle, FIONREAD, &result)) {
	#endif
        disconnect();
		throw Exception(Exception::ERROR_IOCTL);
    }else
        return result;
}

std::streamsize Socket::advanceInputBuffer() {
    if(inputIntermediateSize == 0) //No input buffer
        return 0;

    std::streamsize inAvail;
    if(type == UDP_PEER)
        inAvail = 0;
    else{
        inAvail = egptr()-gptr();
        memmove(eback(), gptr(), inAvail);
    }

    try {
        inAvail += receive(eback()+inAvail, inputIntermediateSize-inAvail);
    } catch(Exception err) {

    }

    setg(eback(), eback(), eback()+inAvail);
    return inAvail;
}

std::streamsize Socket::receive(char_type* buffer, std::streamsize size) {
	if(type == TCP_SERVER)
		throw Exception(Exception::BAD_TYPE);
	if(status != Socket::Status::READY && status != Socket::Status::BUSY)
		return 0;
    size = std::min(size, showmanyc());
    if(size == 0)
		return 0;

    switch(type) {
        case UDP_PEER: {
            struct sockaddr_storage remoteAddr;
			#ifdef WIN32
			int addrSize = sizeof(remoteAddr);
			#else
			unsigned int addrSize = sizeof(remoteAddr);
			#endif
            int result = recvfrom(handle, (char*)buffer, size, 0, reinterpret_cast<struct sockaddr*>(&remoteAddr), &addrSize);

            if(result <= 0) {
                portRemote = 0;
                hostRemote = "";
                throw Exception(Exception::ERROR_READ);
            }else
                readSockaddr(&remoteAddr, hostRemote, portRemote);

            return result;
        }
        case TCP_CLIENT:
        case TCP_SERVERS_CLIENT: {
            int result = recv(handle, (char*)buffer, size, 0);

            if(result <= 0)
				throw Exception(Exception::ERROR_READ);

            return result;
        }
		default:
        case NONE:
        case TCP_SERVER:
            throw Exception(Exception::BAD_TYPE);
    }
}

std::streamsize Socket::send(const char_type* buffer, std::streamsize size) {
	if(type == TCP_SERVER)
		throw Exception(Exception::BAD_TYPE);
	if(status != Socket::Status::READY || size == 0)
		return 0;

    switch(type) {
        case UDP_PEER: {
            AddrinfoContainer info = getSocketInfoFor(hostRemote.c_str(), portRemote, false);

            size_t sentBytes = 0;
            while(sentBytes < (size_t)size) {
                int result = ::sendto(handle, (const char*)buffer + sentBytes, size - sentBytes, 0, info->ai_addr, info->ai_addrlen);
				if(result <= 0) {
					status = BUSY;
	                throw Exception(Exception::ERROR_SEND);
				}
                sentBytes += result;
            }

            return sentBytes;
        }
        case TCP_CLIENT:
        case TCP_SERVERS_CLIENT: {
            size_t sentBytes = 0;
            while(sentBytes < (size_t)size) {
                int result = ::send(handle, (const char*)buffer + sentBytes, size - sentBytes, 0);
				if(result <= 0) {
					status = BUSY;
	                throw Exception(Exception::ERROR_SEND);
				}
                sentBytes += result;
            }
            return sentBytes;
        }
		default:
        case NONE:
        case TCP_SERVER:
            throw Exception(Exception::BAD_TYPE);
    }
}

std::streamsize Socket::redirect(const std::vector<std::shared_ptr<Socket>>& destinations) {
	if(type == TCP_SERVER)
		throw Exception(Exception::BAD_TYPE);

	std::streamsize size = 0;
	while(in_avail()) {
		auto length = egptr()-gptr();
		for(const auto& destination : destinations)
			if(destination->sputn(gptr(), length) < length)
				throw Exception(Exception::ERROR_SEND);
		gbump(length);
		size += length;
		advanceInputBuffer();
	}

	return size;
}

std::streamsize Socket::getInputBufferSize() {
    return inputIntermediateSize;
}

std::streamsize Socket::getOutputBufferSize() {
    return epptr()-pbase();
}

void Socket::setInputBufferSize(std::streamsize n) {
    if(eback()) {
        delete[] eback();
        setg(NULL, NULL, NULL);
    }
    if(n == 0) return;
    if(type == TCP_SERVER)
        throw Exception(Exception::BAD_TYPE);

    char_type* readBuffer = new char_type[n];
    setg(readBuffer, readBuffer, readBuffer);
    inputIntermediateSize = n;
}

void Socket::setOutputBufferSize(std::streamsize n) {
    if(pbase()) {
        delete[] pbase();
        setp(NULL, NULL);
    }
    if(n == 0) return;
    if(type == TCP_SERVER)
        throw Exception(Exception::BAD_TYPE);

    char_type* writeBuffer = new char_type[n];
    setp(writeBuffer, writeBuffer+n);
}

void Socket::setBlockingMode(bool blocking) {
	#ifdef WIN32
	unsigned long flag = !blocking;
	if(ioctlsocket(handle, FIONBIO, &flag) != 0)
	#else
    int flags = fcntl(handle, F_GETFL);
    if(blocking)
        flags &= ~O_NONBLOCK;
    else
        flags |= O_NONBLOCK;
    if(fcntl(handle, F_SETFL, flags) == -1)
	#endif
		throw Exception(Exception::ERROR_IOCTL);
}
Esempio n. 17
0
streampos rpcbuf::seekoff(streamoff offset, ios::seek_dir dir, int mode) {
#else
streampos rpcbuf::seekoff(streamoff offset, seek_dir dir, int mode) {
#endif
    if (!_opened || !gptr()) {
	return EOF;
    }

    if (offset != 0 || dir != ios::cur || mode != ios::in) {
	return EOF;
    }

    return (streampos)gptr();
}

// Refuse any attempt to set the buffers for storing incoming and
// outgoing RPC requests because we need the ability to dynamically
// expand the buffers' sizes.

streambuf* rpcbuf::setbuf(char*, int) {
    return nil;
}

// Dynamically allocate two separate buffers for storing incoming and
// outgoing RPC requests.  Allocating separate buffers for the get and
// put areas makes it easier to expand either area later if necessary.

int rpcbuf::doallocate() {
    const int RPCBUFSIZE = 2032;

    char* get = new char[RPCBUFSIZE];
    if (!get) {
	error("rpcbuf::doallocate: out of memory");
	return EOF;
    }
    setb(get, get + RPCBUFSIZE, true);
    setg(get, get, get);

    char* put = new char[RPCBUFSIZE];
    if (!put) {
	error("rpcbuf::doallocate: out of memory");
	return EOF;
    }
    setp(put, put + RPCBUFSIZE);
    setr(nil);

    return 0;
}

// Expand the get area to make room for a large incoming request.

boolean rpcbuf::expand_g(int newsize) {
    char* get = new char[newsize];
    if (!get) {
	return false;
    }

    int navail = in_avail();
    Memory::copy(gptr(), get, navail);
    delete eback();
    setb(get, get + newsize, true);
    setg(get, get, get + navail);

    return true;
}
Esempio n. 18
0
int rpcbuf::sync() {
    gbump(in_avail());
    return out_waiting() ? overflow() : 0;
}