示例#1
0
文件: socket.cpp 项目: graehu/NetGame
bool socket::send(const address & destination, const void * data, int size)
{
	assert(data);
	assert(size > 0);

	if (m_socket == 0)
		return false;

	assert(destination.getAddress() != 0);
	assert(destination.getPort() != 0);

	sockaddr_in address;
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = htonl(destination.getAddress());
	address.sin_port = htons((unsigned short) destination.getPort());
	int sent_bytes = sendto(m_socket, (const char*)data, size, 0, (sockaddr*)&address, sizeof(sockaddr_in));
	return sent_bytes == size;
}
示例#2
0
void connection::connect(const address & _address)
{
  printf("client trying: %d.%d.%d.%d:%d\n",
	 _address.getA(),
	 _address.getB(),
	 _address.getC(),
	 _address.getD(),
	 _address.getPort()
	 );

  if(!m_keyPool.empty())   /// If the key pool isn't empty
    {
      /// access mailList based on the key pools returned value
      /// and set mailList element up with the new connections
      /// data
      m_mailList[m_keyPool.back()].first->m_state = e_connecting;
      m_mailList[m_keyPool.back()].first->m_address = _address;
      m_mailList[m_keyPool.back()].first->m_timeoutAccumulator = 0.0f;
      m_mailList[m_keyPool.back()].first->m_stats.reset();
      m_mailList[m_keyPool.back()].second = 0;


      /// add the key onto newConKeys and pop it off of the keyPool
      /// as it is now in use.

      m_newConnKeys.push_back(m_keyPool.back()); ///Receive key
      m_keyPool.pop_back();

      return;
    }

  /// If The key pool Is empty
  /// allocate a new sender
  /// initialise it as a new connection
  /// push it back onto mailList
  /// then push it's array position onto newConKeys

  sender* n_mailer = new sender(m_maxSequence);
  n_mailer->m_address = _address;
  n_mailer->m_state = e_connecting;
  n_mailer->m_timeoutAccumulator = 0.0f;
  unsigned short sendKey = 0;
  m_mailList.push_back(std::pair<sender*, unsigned short>(n_mailer, sendKey));
  m_newConnKeys.push_back(m_mailList.size()-1); /// Receive key
}