Esempio n. 1
0
void UDPEchoClient::onReadyRead () {
	{
		LockGuard guard (mMutex);
		if (mState != WAIT) return; // ignoring; timeout.
		// Check protocol
		String from;
		int fromPort;
		ByteArrayPtr data = mSocket.recvFrom(&from,&fromPort);
		if (!data) {
			Log (LogWarning) << LOGID << "Could not read any data tough readyRead() signal" << std::endl;
			return; // ?
		}
		String toParse (data->const_c_array(), data->size());
		ArgumentList list;
		sf::argSplit (toParse, &list);
		// Format: TOKEN IP-Address Port
		if (list.size() != 4 || list[0] != "condataReply") {
			Log (LogInfo) << LOGID << "Invalid protocol in answer " << *data << ", token=" << list.size() <<  std::endl;
			return;   // invalid protocol
		}
		if (list[1] != mToken){
			Log (LogInfo) << LOGID << "Token mismatch in answer " << *data << std::endl;
			return; // invalid token
		}
		mState = READY;
		mAddress = list[2];
		mPort = atoi (list[3].c_str());
		Log (LogInfo) << LOGID << "Successfully decoded echo answer: " << mAddress << ":" << mPort << " coming from " << from << ":" << fromPort << std::endl;
		sf::cancelTimer(mTimeoutHandle);
	}
	mResultDelegate (NoError);
}
Esempio n. 2
0
void SocketMessage::setPayload(const ByteArrayPtr &msg)
{
	clear();
	payload_type=Variant::TYPE_BYTEARRAY;
	payload_size=msg.size();
	payload=malloc(payload_size);
	if (!payload) throw OutOfMemoryException();
	memcpy(payload,msg.ptr(),payload_size);
}
Esempio n. 3
0
sf::ByteArrayPtr BoshTransport::read (long maxSize) {
	ByteArrayPtr result = sf::createByteArrayPtr();
	if (maxSize < 0 || maxSize >= (long) mInputBuffer.size()) {
		result->swap (mInputBuffer);
	} else {
		result->append (mInputBuffer.const_c_array(), maxSize);
		mInputBuffer.l_truncate(maxSize);
	}
	return result;
}
Esempio n. 4
0
	/// Eat something what was written
	ByteArrayPtr consume (long maxSize = -1) {
		if (mIngoingBuffer.empty()) return ByteArrayPtr();
		ByteArrayPtr result = createByteArrayPtr();
		if (maxSize < 0 || maxSize >= (int64_t) mIngoingBuffer.size()){
			result->swap (mIngoingBuffer);
			return result;
		}
		result->assign (mIngoingBuffer.begin(), mIngoingBuffer.begin() + maxSize);
		mIngoingBuffer.l_truncate (maxSize);
		notifyAsync (mChanged);
		return result;
	}
Esempio n. 5
0
void XMPPStream::onChannelChange   () {
	ByteArrayPtr data = mChannel->read();
	if (data && data->size() > 0){
#ifndef NDEBUG
		Log (LogInfo) << LOGID << "recv " << *data << std::endl;
#endif
		mXmlStreamDecoder.onWrite(*data);
	}
	// mXmlStreamDecoder can remove the channel by its own callbacks!
	if (mChannel && mChannel->error()) {
		onChannelError (mChannel->error());
	}
}
Esempio n. 6
0
	virtual sf::Error read (const ds::Range & range, ByteArray & dst) {
		LockGuard guard (mMutex);
		ds::Range realRange = ds::Range (range.from - mPosition, range.to - mPosition);

		if (realRange.from != 0)
			return error::NotSupported; // no seeking
		ByteArrayPtr data = mChannel->read (range.to);
		if (data) {
			dst.swap (*data);
		}
		if (!data || data->empty()){
			mReady = false;
		}
		mPosition+=dst.size();
		return mChannel->error();
	}
Esempio n. 7
0
void LocalChannel::incoming (ByteArrayPtr data){
	if (mCollector) mCollector->addPendingData (data->size());
	mInputBuffer.append(*data);
	if (mChanged) {
		xcall (mChanged);
	}
}
Esempio n. 8
0
Error LocalChannel::write (const ByteArrayPtr& data, const ResultCallback & callback) {
	if (!mOther) return error::NotInitialized; // no target
	mOther->incoming (data);
	if (callback) xcall (abind (callback, NoError));
	if (mCollector) mCollector->addTransferred (mHostId, mOther->hostId(), data->size(), mHops);
	return NoError;
}
Esempio n. 9
0
	sf::Error write (const ds::Range & range, const ByteArrayPtr & data) {
		LockGuard guard (mMutex);
		ds::Range realRange = ds::Range (range.from - mWritePosition, range.to - mWritePosition);
		if (realRange.from != 0) {
			// no seeking
			return error::NotSupported;
		}
		mWritePosition+=data->size();
		return mChannel->write (data);
	}
Esempio n. 10
0
ByteArrayPtr Datagram::encode () const {
	// simple encoding headerLength, contentLength, header, content
	// both with 4 bytes
	size_t headerLength  = mHeader ? mHeader->size() : 0;
	if (headerLength > 2147483647) {
		Log (LogError) << LOGID << "Header to long!" << std::endl;
		assert (false);
		return ByteArrayPtr();
	}
	size_t contentLength = mContent ? mContent->size() : 0;
	if (contentLength > 2147483647) {
		Log (LogError) << LOGID << "Content to long" << std::endl;
		assert (false);
		return ByteArrayPtr();
	}
	uint32_t hln = htonl ((uint32_t)headerLength);
	uint32_t cln = htonl ((uint32_t)contentLength);

	ByteArrayPtr dest = createByteArrayPtr ();
	dest->reserve(8 + headerLength + contentLength);
	dest->append((const char*) &hln, 4);
	dest->append((const char*) &cln, 4);
	if (mHeader)
		dest->append(*mHeader);
	if (mContent)
		dest->append(*mContent);
	assert (dest->size() == 8 + headerLength + contentLength);
	return dest;
}
Esempio n. 11
0
void HttpConnectionManager::onChannelChange (AsyncOpId id) {
	PendingConnectionOp * op;
	getReadyAsyncOp (id, PendingConnection, &op);
	if (!op) return;

	ByteArrayPtr all = op->connection->channel->read();
	if (all && !all->empty()){
		Log (LogInfo) << LOGID << "Recv in pending connection: " << *all << std::endl;
		op->connection->inputBuffer.append(*all);
	}

	Error e = op->connection->channel->error();
	if (e) {
		// adieu
		Log (LogInfo) << LOGID << "Closing channel to " << op->connection->host << " as channel reported error: " << toString (e) << std::endl;
		xcall (abind (&throwAway, op->connection));
		removeFromPendingConnections (op);
		delete op;
		return;
	}
	// data ready? nobody knows, add it again...
	addAsyncOp (op);
}
Esempio n. 12
0
/*!\brief Nutzdaten des Chunks setzen
 *
 * \desc
 * Mit dieser Funktion werden die Nutzdaten des Chunks angegeben. Die
 * Daten werden dabei in einen eigenen Speicherbereich kopiert.
 *
 * \param data Eine Referenz aif ein ByteArray oder ByteArrayPtr
 * \exception NullPointerException Wird geworfen, wenn \p ptr auf NULL zeigt
 * \exception OutOfMemoryException Nicht genug Speicher
 */
void PFPChunk::setData(const ByteArrayPtr &data)
{
	setData(data.ptr(),data.size());
}