Source Code (Use browser search to find items of interest.)

Class Index

ksirtet'Socket (./kdegames/ksirtet/lib/socket.h:7)

class Socket
{
 public:
 	Socket(int fd = -1, bool createNotifier = FALSE,
		   QObject *parent = 0, const char *name = 0);

	/** Invalid socket so that the destructor will not close it */
	void remove() { _fd = -1; }

	/** Close valid socket */
    ~Socket();
	
	int fd() const { return _fd; }

	/**
	 * @return the socket notifier associated with the socket
	 * (0 if none).
	 */
	QSocketNotifier *notifier() const { return _notifier; }
	
	/** 
	 * Write data contained in the writing stream to the socket.
	 * It clears the stream.
	 * @return TRUE if all was written without error.
	 */
	bool write();
	bool write(const QByteArray &a);
	
	/** @return the QDataStream for writing. */
	WritingStream &writingStream() { return writing; }

	/** @return the size of pending data. */
	int pendingData() const;
	
	/**
	 * Read data from socket and append them to reading stream for the specified socket.
	 * The portion of the stream that has been read is cleared.
	 * @return the read size or -1 on error
	 */
	int read();
	
	/** @return the reading stream. */
	ReadingStream &readingStream() { return reading; }
	
 private:
	int _fd;
	QSocketNotifier *_notifier;

	WritingStream writing;
	ReadingStream reading;
};

ksirtet'Socket::Socket() (./kdegames/ksirtet/lib/socket.cpp:24)

Socket::Socket(int fd, bool createNotifier, QObject *parent, const char *name)
: _fd(fd), _notifier(0)
{
	if (createNotifier) {
		ASSERT( _fd!=-1 );
		_notifier = new QSocketNotifier(fd, QSocketNotifier::Read, parent, name);
		_notifier->setEnabled(FALSE);
	}
}


ksirtet'Socket::~Socket() (./kdegames/ksirtet/lib/socket.cpp:34)

Socket::~Socket()
{
	if (_notifier) delete _notifier;
	if ( _fd!=-1 ) closeSocket(_fd);
}


ksirtet'Socket::write() (./kdegames/ksirtet/lib/socket.cpp:40)

bool Socket::write(const QByteArray &a)
{
	ASSERT( _fd!=-1 );
	return ( ::write(_fd, a.data(), a.size())==(int)a.size() );
}


ksirtet'Socket::write() (./kdegames/ksirtet/lib/socket.cpp:46)

bool Socket::write()
{
	bool res = write(writing.buffer());
	writing.clear();
	return res;
}


ksirtet'Socket::pendingData() (./kdegames/ksirtet/lib/socket.cpp:53)

int Socket::pendingData() const
{
	ASSERT( _fd!=-1 );
	int size = 0;
	if ( ioctl(_fd, FIONREAD, (char *)&size)<0 ) return -1;
	return size;
}


ksirtet'Socket::read() (./kdegames/ksirtet/lib/socket.cpp:61)

int Socket::read()
{
	ASSERT( _fd!=-1 );
	reading.clearRead();
	
	int size = pendingData();
	if ( size==-1 ) return -1;
	
	reading.device()->close();
	int dec = reading.size();
	reading.buffer().resize(dec + size);
	size = ::read(_fd, reading.buffer().data() + dec, size);
	if ( size==-1 ) reading.buffer().resize(dec);
	reading.device()->open(IO_ReadOnly);
	
	return size;
}