void OutputMessagePool::send(OutputMessage_ptr msg)
{
	m_outputPoolLock.lock();
	OutputMessage::OutputMessageState state = msg->getState();
	m_outputPoolLock.unlock();

	if (state == OutputMessage::STATE_ALLOCATED_NO_AUTOSEND) {
		Connection_ptr connection = msg->getConnection();
		if (connection && !connection->send(msg)) {
			// Send only fails when connection is closing (or in error state)
			// This call will free the message
			msg->getProtocol()->onSendMessage(msg);
		}
	}
}
Esempio n. 2
0
size_t TCP::send(Connection_ptr conn, const char* buffer, size_t n) {
  size_t written{0};
  auto packets = inet_.transmit_queue_available();

  debug2("<TCP::send> Send request for %u bytes\n", n);

  if(packets > 0) {
    written += conn->send(buffer, n, packets);
  }
  // if connection still can send (means there wasn't enough packets)
  // only requeue if not already queued
  if(conn->can_send() and !conn->is_queued()) {
    debug2("<TCP::send> Conn queued.\n");
    writeq.push_back(conn);
    conn->set_queued(true);
  }

  return written;
}
void OutputMessagePool::sendAll()
{
	std::lock_guard<std::recursive_mutex> lockClass(m_outputPoolLock);

	const int64_t dropTime = m_frameTime - 10000;
	const int64_t frameTime = m_frameTime - 10;

	for (OutputMessage_ptr omsg : m_toAddQueue) {
		const int64_t msgFrame = omsg->getFrame();
		if (msgFrame >= dropTime) {
			omsg->setState(OutputMessage::STATE_ALLOCATED);

			if (frameTime > msgFrame) {
				m_autoSendOutputMessages.push_front(omsg);
			} else {
				m_autoSendOutputMessages.push_back(omsg);
			}
		} else {
			//drop messages that are older than 10 seconds
			omsg->getProtocol()->onSendMessage(omsg);
		}
	}
	m_toAddQueue.clear();

	for (auto it = m_autoSendOutputMessages.begin(), end = m_autoSendOutputMessages.end(); it != end; it = m_autoSendOutputMessages.erase(it)) {
		OutputMessage_ptr omsg = *it;
		if (frameTime <= omsg->getFrame()) {
			break;
		}

		Connection_ptr connection = omsg->getConnection();
		if (connection && !connection->send(omsg)) {
			// Send only fails when connection is closing (or in error state)
			// This call will free the message
			omsg->getProtocol()->onSendMessage(omsg);
		}
	}
}