示例#1
0
文件: recv.hpp 项目: BianJian/mmo
inline std::pair<aid_t, bool> recv_impl(
  gce::nonblocked, Recver& recver, message& msg, pattern& patt, recv_meta meta = recv_meta()
  )
{
  bool has_exit = check_exit(patt.match_list_);
  aid_t sender = recver.recv(msg, patt.match_list_, patt.recver_);
  if (meta.matched_ != 0)
  {
    *meta.matched_ = msg.get_type();
  }
  if (!has_exit && msg.get_type() == exit)
  {
    if (meta.guard_ != 0)
    {
      *meta.guard_ = boost::asio::error::make_error_code(boost::asio::error::shut_down);
      return std::make_pair(sender, false);
    }
    else
    {
      exit_code_t exc;
      std::string errmsg;
      msg >> exc >> errmsg;
      if (exc == exit_normal)
      {
        GCE_VERIFY(false)(exc)(msg)(patt)(sender)
          .msg("gce::normal_exception").except<normal_exception>();
      }
      else if (exc == exit_except)
      {
        GCE_VERIFY(false)(exc)(msg)(patt)(sender)
          .msg("gce::runtime_exception").except<runtime_exception>();
      }
      else if (exc == exit_remote)
      {
        GCE_VERIFY(false)(exc)(msg)(patt)(sender)
          .msg("gce::remote_exception").except<remote_exception>();
      }
      else if (exc == exit_already)
      {
        GCE_VERIFY(false)(exc)(msg)(patt)(sender)
          .msg("gce::already_exit_exception").except<already_exit_exception>();
      }
      else if (exc == exit_neterr)
      {
        GCE_VERIFY(false)(exc)(msg)(patt)(sender)
          .msg("gce::neterr_exception").except<neterr_exception>();
      }
      else
      {
        GCE_ASSERT(false)(exc)(errmsg);
      }
    }
  }
  return std::make_pair(sender, sender != aid_nil);
}
示例#2
0
void GateModule::handle_message(const message& msg)
{
	switch (msg.get_type())
	{
	case EMessageType::SocketConnect:
	{
		ClientConnect(msg);
		break;
	}
	case EMessageType::SocketData:
	{
		ClientData(msg);
		break;
	}
	case EMessageType::SocketClose:
	{
		ClientClose(msg);
		break;
	}
	case EMessageType::ActorData:
	{
		ActorData(msg);
		break;
	}
	case EMessageType::ToClient:
	{
		ToClientData(msg);
		break;
	}
	}
}
示例#3
0
void server::receive(QHostAddress const & ip, quint16 port, message const & msg)
{
	switch (msg.get_type()) {
		case message_type::client_server_request: {
			// TODO: extract file size from message and pass to workers manager.
			qDebug() << "Server: new client arrived.";
			auto workers_ip_ports = workers_manager_.get_workers();
			auto response = create_message<ip_port_array_message>(
				message_type::server_client_response, workers_ip_ports);
			auto sock = find_socket(ip, port);
			qDebug() << "Server: sending client" << workers_ip_ports.size() << "workers.";
			sock->send(*response);
			break;
		}
		case message_type::worker_server_connect: {
			auto m = static_cast<number_message const &>(msg);
			auto sock = find_socket(ip, port);
			worker_ids_[sock] = workers_manager_.add_new_worker(ip, m.number());
			qDebug() << "Server: new worker arrived.";
			break;
		}
		case message_type::worker_server_state_changed: {
			quint32 const load_factor = static_cast<number_message const &>(msg).number();
			auto sock = find_socket(ip, port);
			auto it = worker_ids_.find(sock);
			if (worker_ids_.end() != it)
				workers_manager_.update_worker_load_factor(it.value(), load_factor);
			else
				qDebug() << "Server: trying to set load factor to unknown worker. Do nothing.";
			break;
		}
		default:
			qDebug() << "Server: unknown message type. Dropping message";
	}
}
示例#4
0
文件: recv.hpp 项目: BianJian/mmo
inline std::pair<aid_t, bool> respond_impl(
  Tag, Recver& recver, resp_t res, message& msg, duration_t tmo, recv_meta meta = recv_meta()
  )
{
  aid_t sender = recver.respond(res, msg, tmo);
  if (msg.get_type() == exit)
  {
    if (meta.guard_ != 0)
    {
      *meta.guard_ = boost::asio::error::make_error_code(boost::asio::error::shut_down);
      return std::make_pair(sender, false);
    }
    else
    {
      exit_code_t exc;
      std::string errmsg;
      msg >> exc >> errmsg;
      if (exc == exit_normal)
      {
        GCE_VERIFY(false)(exc)(res)(msg)(sender)(tmo)
          .msg("gce::normal_exception").except<normal_exception>();
      }
      else if (exc == exit_except)
      {
        GCE_VERIFY(false)(exc)(res)(msg)(sender)(tmo)
          .msg("gce::runtime_exception").except<runtime_exception>();
      }
      else if (exc == exit_remote)
      {
        GCE_VERIFY(false)(exc)(res)(msg)(sender)(tmo)
          .msg("gce::remote_exception").except<remote_exception>();
      }
      else if (exc == exit_already)
      {
        GCE_VERIFY(false)(exc)(res)(msg)(sender)(tmo)
          .msg("gce::already_exit_exception").except<already_exit_exception>();
      }
      else if (exc == exit_neterr)
      {
        GCE_VERIFY(false)(exc)(res)(msg)(sender)(tmo)
          .msg("gce::neterr_exception").except<neterr_exception>();
      }
      else
      {
        GCE_ASSERT(false)(exc)(errmsg);
      }
    }
  }

  if (sender == aid_nil && meta.tmo_ != 0)
  {
    *meta.tmo_ = boost::asio::error::make_error_code(boost::asio::error::timed_out);
    return std::make_pair(sender, false);
  }

  GCE_VERIFY(sender != aid_nil)(res)(msg)(tmo)
    .msg("gce::respond_timeout_exception").except<respond_timeout_exception>();
  return std::make_pair(sender, true);
}
示例#5
0
	void actor_manager::broadcast_message(module_id sender,message& msg)
	{
		assert(msg.get_type() != EMessageType::Unknown);
		msg.set_sender(sender);
		msg.set_receiver(module_id::create(0));
		for (auto& w : _workers)
		{
			w->broadcast_message(msg);
		}
	}
示例#6
0
	void actor_manager::send_message(module_id sender, module_id receiver,message& msg)
	{
		assert(msg.get_type() != EMessageType::Unknown);
		msg.set_sender(sender);
		msg.set_receiver(receiver);
		uint8_t workerID = get_workerID(receiver);
		if (workerID < _workers.size())
		{
			_workers[workerID]->dispatch_message(msg);
		}
	}
示例#7
0
文件: recv.hpp 项目: HappySky2046/gce
inline aid_t recv(Recver& recver, message& msg, match& mach)
{
  bool add_exit = check_exit(mach.match_list_);
  if (add_exit)
  {
    mach.match_list_.push_back(exit);
  }

  aid_t sender = recver.recv(msg, mach);
  if (add_exit && msg.get_type() == exit)
  {
    exit_code_t exc;
    std::string errmsg;
    msg >> exc >> errmsg;
    throw std::runtime_error(errmsg);
  }
示例#8
0
///------------------------------------------------------------------------------
void mailbox::add_match_msg(recv_t const& rcv, aid_t sender, message const& msg)
{
  recv_itr itr = recv_que_.insert(recv_que_.end(), std::make_pair(rcv, msg));
  match_t type = msg.get_type();
  match_itr mtr;
  match_queue_t* match_que = 0;

  if (type >= 0 && type < (match_t)cache_match_list_.size())
  {
    match_que = &cache_match_list_[type];
  }
  else
  {
    std::pair<match_queue_list_t::iterator, bool> pr =
      match_queue_list_.insert(std::make_pair(type, dummy2_));
    match_que = &pr.first->second;
  }
  mtr = match_que->insert(match_que->end(), itr);

  if (sender && type == exit)
  {
    if (sender.svc_)
    {
      std::pair<aid_t, match_itr> p = std::make_pair(sender, mtr);
      std::pair<svc_exit_list_t::iterator, bool> pr = 
        svc_exit_list_.insert(std::make_pair(sender.svc_, p));
      if (!pr.second)
      {
        recv_que_.erase(*pr.first->second.second);
        match_que->erase(pr.first->second.second);
        pr.first->second = p;
      }
    }
    else
    {
      std::pair<exit_list_t::iterator, bool> pr = 
        exit_list_.insert(std::make_pair(sender, mtr));
      if (!pr.second)
      {
        recv_que_.erase(*pr.first->second);
        match_que->erase(pr.first->second);
        pr.first->second = mtr;
      }
    }
  }
}
示例#9
0
文件: recv.hpp 项目: BianJian/mmo
inline std::pair<bool, bool> end_recv(
  Stackless recver, aid_t sender, message msg,
  aid_t& osender, std::pair<recv_meta, bool> pr
  )
{
  osender = sender;
  if (!pr.second && msg.get_type() == exit)
  {
    if (pr.first.guard_ != 0)
    {
      *pr.first.guard_ = boost::asio::error::make_error_code(boost::asio::error::shut_down);
      return std::make_pair(false, true);
    }
    else
    {
      exit_code_t exc;
      std::string errmsg;
      msg >> exc >> errmsg;
      recver.get_actor().quit(exc, errmsg);
      return std::make_pair(false, false);
    }
  }
  else if (sender == aid_nil)
示例#10
0
void
make_frames(const message &msg, OutputIterator out)
{
	const channel ch = msg.get_channel();
	switch (msg.get_type()) {
	case MSG:
		{
			msg_frame MSG;
			MSG.channel = ch.get_number();
			MSG.message = ch.get_message_number();
			MSG.more = false;
			MSG.sequence = ch.get_sequence_number();
			MSG.payload = msg.get_payload();
			*out++ = MSG;
		}
		break;
	case RPY:
		{
			rpy_frame RPY;
			RPY.channel = ch.get_number();
			RPY.message = ch.get_message_number();
			RPY.more = false;
			RPY.sequence = ch.get_sequence_number();
			RPY.payload = msg.get_payload();
			*out++ = RPY;
		}
		break;
	case ANS:
		{
			ans_frame ANS;
			ANS.channel = ch.get_number();
			ANS.message = ch.get_message_number();
			ANS.more = false;
			ANS.sequence = ch.get_sequence_number();
			ANS.payload = msg.get_payload();
			ANS.answer = ch.get_answer_number();
			*out++ = ANS;
		}
		break;
	case ERR:
		{
			err_frame ERR;
			ERR.channel = ch.get_number();
			ERR.message = ch.get_message_number();
			ERR.more = false;
			ERR.sequence = ch.get_sequence_number();
			ERR.payload = msg.get_payload();
			*out++ = ERR;
		}
		break;
	case NUL:
		{
			nul_frame NUL;
			NUL.channel = ch.get_number();
			NUL.message = ch.get_message_number();
			NUL.more = false;
			NUL.sequence = ch.get_sequence_number();
			NUL.payload = msg.get_payload();
			*out++ = NUL;
		}
		break;
	case SEQ:
	default:
		std::cerr << "The message has an invalid frame type.\n";
		assert(false);
		/// \todo throw ?
	}
}