Exemplo n.º 1
0
Arquivo: udp.cpp Projeto: skypjack/uvw
TEST(UDP, ReadTrySend) {
    const std::string address = std::string{"127.0.0.1"};
    const unsigned int port = 4242;

    auto loop = uvw::Loop::getDefault();
    auto server = loop->resource<uvw::UDPHandle>();
    auto client = loop->resource<uvw::UDPHandle>();

    server->on<uvw::ErrorEvent>([](const auto &, auto &) { FAIL(); });
    client->on<uvw::ErrorEvent>([](const auto &, auto &) { FAIL(); });

    server->once<uvw::UDPDataEvent>([&client](const uvw::UDPDataEvent &, uvw::UDPHandle &handle) {
        client->close();
        handle.close();
    });

    server->bind(uvw::Addr{ address, port });
    server->recv();

    auto dataTrySend = std::unique_ptr<char[]>(new char[1]{ 'a' });

    client->trySend(uvw::Addr{ address, port }, dataTrySend.get(), 1);
    client->trySend(address, port, nullptr, 0);

    client->trySend(uvw::Addr{ address, port }, std::move(dataTrySend), 1);
    client->trySend(address, port, std::unique_ptr<char[]>{}, 0);

    loop->run();
}
Exemplo n.º 2
0
void NetworkManager::removeAccount(const QString &protocol, const QString &id)
{
	AccountAction *action = new AccountAction(Action::RemoveAccount);
	action->account = AccountId(id, protocol);
	m_actions << action;
	trySend();
}
Exemplo n.º 3
0
void NetworkManager::sendMessage(const qutim_sdk_0_3::Message &message)
{
	MessageAction *action = new MessageAction();
	ChatUnit *contact = const_cast<ChatUnit*>(message.chatUnit()->getHistoryUnit());
	action->account = AccountId(contact->account());
	action->contact = contact->id();
	action->time = message.time();
	action->text = message.text();
	action->incoming = message.isIncoming();
	if (message.property("otrEncrypted", false))
		action->encryption << QLatin1String("otr");
	if (message.property("pgpEncrypted", false)
	        || (encryptedMessageIdInited
	            && encryptedMessageId == message.id())) {
		action->encryption << QLatin1String("pgp");
	}
    if (message.property("autoreply", false))
        action->encryption << QLatin1String("autoreply");
	m_actions << action;
	trySend();
}
Exemplo n.º 4
0
	void write(const QVariant &vrequest)
	{
		ZurlRequestPacket request;
		if(!request.fromVariant(vrequest))
		{
			QVariantHash vhash = vrequest.toHash();
			if(!vhash["cancel"].toBool())
			{
				QMetaObject::invokeMethod(this, "respondError", Qt::QueuedConnection, Q_ARG(QByteArray, "bad-request"));
			}
			else
			{
				cleanup();
				QMetaObject::invokeMethod(q, "finished", Qt::QueuedConnection);
			}

			return;
		}

		// cancel session if a wrong sequenced packet is received
		if(inSeq == -1 || request.seq == -1 || request.seq != inSeq + 1)
		{
			if(!request.cancel)
			{
				QMetaObject::invokeMethod(this, "respondError", Qt::QueuedConnection, Q_ARG(QByteArray, "cancel"));
			}
			else
			{
				cleanup();
				QMetaObject::invokeMethod(q, "finished", Qt::QueuedConnection);
			}

			return;
		}

		if(request.cancel)
		{
			cleanup();
			QMetaObject::invokeMethod(q, "finished", Qt::QueuedConnection);
			return;
		}

		refreshTimeout();

		inSeq = request.seq;

		// all we care about from follow-up writes are body and credits

		if(request.credits != -1)
			outCredits += request.credits;

		if(!request.body.isNull())
		{
			if(bodySent)
			{
				QMetaObject::invokeMethod(this, "respondError", Qt::QueuedConnection, Q_ARG(QByteArray, "bad-request"));
				return;
			}

			if(!request.body.isEmpty())
				hreq->writeBody(request.body);

			// the 'more' flag only has significance if body field present
			if(!request.more)
			{
				bodySent = true;
				hreq->endBody();
			}
		}

		// if we needed credits to send something, take care of that now
		if(request.credits != -1 && stuffToRead)
			trySend();
	}
Exemplo n.º 5
0
void NetworkManager::updateContact(qutim_sdk_0_3::Contact *contact)
{
	m_actions << new ContactAction(Action::UpdateContact, contact);
	trySend();
}
Exemplo n.º 6
0
Socket::ResumeSend* Socket::send(char const* b, char const* e)
{
	if(!trySend(b, e))
		return new ResumeSend(this, b, e);
	return 0;
}
Exemplo n.º 7
-1
	void write(const QVariant &vrequest)
	{
		ZhttpRequestPacket request;
		if(!request.fromVariant(vrequest))
		{
			QVariantHash vhash = vrequest.toHash();
			if(vhash["type"].toByteArray() != "cancel")
			{
				QMetaObject::invokeMethod(this, "respondError", Qt::QueuedConnection, Q_ARG(QByteArray, "bad-request"));
			}
			else
			{
				cleanup();
				QMetaObject::invokeMethod(q, "finished", Qt::QueuedConnection);
			}

			return;
		}

		// cancel session if a wrong sequenced packet is received
		if(inSeq == -1 || request.seq == -1 || request.seq != inSeq + 1)
		{
			if(request.type != ZhttpRequestPacket::Cancel)
			{
				QMetaObject::invokeMethod(this, "respondCancel", Qt::QueuedConnection);
			}
			else
			{
				cleanup();
				QMetaObject::invokeMethod(q, "finished", Qt::QueuedConnection);
			}

			return;
		}

		if(request.type == ZhttpRequestPacket::Cancel)
		{
			cleanup();
			QMetaObject::invokeMethod(q, "finished", Qt::QueuedConnection);
			return;
		}

		inSeq = request.seq;

		refreshTimeout();

		// all we care about from follow-up writes are body and credits

		if(request.credits != -1)
			outCredits += request.credits;

		if(transport == HttpTransport)
		{
			if(request.type == ZhttpRequestPacket::Data)
			{
				if(bodySent)
				{
					QMetaObject::invokeMethod(this, "respondError", Qt::QueuedConnection, Q_ARG(QByteArray, "bad-request"));
					return;
				}

				refreshActivityTimeout();

				if(!request.body.isEmpty())
					hreq->writeBody(request.body);

				// the 'more' flag only has significance if body field present
				if(!request.more)
				{
					bodySent = true;
					hreq->endBody();
				}
			}
		}
		else // WebSocketTransport
		{
			if(request.type == ZhttpRequestPacket::Data || request.type == ZhttpRequestPacket::Close || request.type == ZhttpRequestPacket::Ping || request.type == ZhttpRequestPacket::Pong)
			{
				refreshActivityTimeout();

				if(request.type == ZhttpRequestPacket::Data)
				{
					WebSocket::Frame::Type ftype;
					if(wsSendingMessage)
						ftype = WebSocket::Frame::Continuation;
					else if(request.contentType == "binary")
						ftype = WebSocket::Frame::Binary;
					else
						ftype = WebSocket::Frame::Text;

					wsSendingMessage = request.more;

					wsPendingWrites += request.body.size();
					ws->writeFrame(WebSocket::Frame(ftype, request.body, request.more));
				}
				else if(request.type == ZhttpRequestPacket::Ping)
				{
					wsPendingWrites += 0;
					ws->writeFrame(WebSocket::Frame(WebSocket::Frame::Ping, QByteArray(), false));
				}
				else if(request.type == ZhttpRequestPacket::Pong)
				{
					wsPendingWrites += 0;
					ws->writeFrame(WebSocket::Frame(WebSocket::Frame::Pong, QByteArray(), false));
				}
				else if(request.type == ZhttpRequestPacket::Close)
					ws->close(request.code);
			}
		}

		// if we needed credits to send something, take care of that now
		if(request.credits != -1 && stuffToRead)
			trySend();
	}