Beispiel #1
0
	/*! Send message over socket.
	    \param data char * buffer to send
	    \param remaining number of bytes
	    \param nb - if true, don't block
	    \return number of bytes sent */
	int send(const char *data, size_t remaining, bool nb=false)
	{
		unsigned wrdone(0);

		while (remaining > 0)
		{
			const int wrtSz(_sock->sendBytes(data + wrdone, static_cast<int>(remaining)));
			if (wrtSz < 0)
			{
				if (errno == EAGAIN
#if defined EWOULDBLOCK && EAGAIN != EWOULDBLOCK
					|| errno == EWOULDBLOCK
#endif
				)
				{
					if (nb)
						return wrdone;
					continue;
				}
				throw PeerResetConnection("send: connection gone");
			}

			wrdone += wrtSz;
			remaining -= wrtSz;
		}

		return wrdone;
	}
Beispiel #2
0
	/*! Send message over socket.
	    \param msg message string to send
	    \return number of bytes sent */
	int send(const f8String& msg)
	{
		unsigned remaining(msg.size()), wrdone(0);
		const char *data(msg.data());

		while (remaining > 0)
		{
			const int wrtSz(_sock->sendBytes(data + wrdone, remaining));
			if (wrtSz < 0)
			{
				switch(errno)
				{
				case EAGAIN:
#if defined EWOULDBLOCK && EAGAIN != EWOULDBLOCK
				case EWOULDBLOCK:
#endif
					continue;
				default:
					throw PeerResetConnection("send: connection gone");
				}
			}

			wrdone += wrtSz;
			remaining -= wrtSz;
		}

		return wrdone;
	}
Beispiel #3
0
	/*! Read bytes from the socket layer, throws PeerResetConnection.
	    \param where buffer to place bytes in
	    \param sz number of bytes to read
	    \param maxsz max number of bytes to read
	    \return number of bytes read */
	int realSockRead(size_t sz, size_t maxsz)
	{
		const size_t max_sz(_read_buffer + sizeof(_read_buffer) - _read_buffer_wptr);
		int maxremaining(std::min(maxsz, max_sz)), remaining(std::min(sz, max_sz));
		char *ptr(_read_buffer_wptr), *eptr(_read_buffer + sizeof(_read_buffer));

		int rdsz(0);
		while (remaining > 0 && _read_buffer_wptr < eptr)
		{
			rdsz = _sock->receiveBytes(_read_buffer_wptr, maxremaining);
			if (rdsz <= 0)
			{
				if (errno == EAGAIN
#if defined EWOULDBLOCK && EAGAIN != EWOULDBLOCK
					|| errno == EWOULDBLOCK
#endif
				)
					continue;
				throw PeerResetConnection("sockRead: connection gone");
			}
			_read_buffer_wptr += rdsz;
			remaining -= rdsz;
			maxremaining -= rdsz;
		}
		return _read_buffer_wptr - ptr;
	}
Beispiel #4
0
	/*! Read bytes from the socket layer, throws PeerResetConnection.
	    \param where buffer to place bytes in
	    \param sz number of bytes to read
	    \return number of bytes read */
	int sockRead(char *where, const size_t sz)
	{
		unsigned remaining(sz), rddone(0);

		while (remaining > 0)
		{
			const int rdSz(_sock->receiveBytes(where + rddone, remaining));
			if (rdSz <= 0)
			{
				switch(errno)
				{
				case EAGAIN:
#if defined EWOULDBLOCK && EAGAIN != EWOULDBLOCK
				case EWOULDBLOCK:
#endif
					continue;
				default:
					throw PeerResetConnection("sockRead: connection gone");
				}
			}

			rddone += rdSz;
			remaining -= rdSz;
		}

		return rddone;
	}
Beispiel #5
0
	/*! Read bytes from the socket layer, throws PeerResetConnection.
		 \param where buffer to place bytes in
		 \param sz number of bytes to read
		 \return number of bytes read */
	int sockRead(char *where, const size_t sz)
	{
		unsigned remaining(static_cast<unsigned>(sz)), rddone(0);
		while (remaining > 0)
		{
			const int rdSz(_sock->receiveBytes(where + rddone, remaining));
			if (rdSz <= 0)
			{
				if (errno == EAGAIN
#if defined EWOULDBLOCK && EAGAIN != EWOULDBLOCK
					|| errno == EWOULDBLOCK
#endif
				)
					continue;
				throw PeerResetConnection("sockRead: connection gone");
			}
			rddone += rdSz;
			remaining -= rdSz;
		}
		return rddone;
	}