Beispiel #1
0
void				TCPWinServSocket::ReadData(CircularBuff &circbuff, Selector &sel)
{
	DWORD			recvbytes;
	WSABUF			databuf[3];
	DWORD			flags = 0;
	char			first_buff[4];
	char			second_buff[4];
	char			third_buff[256];

	databuf[0].len = 4;
	databuf[0].buf = first_buff;
	databuf[1].len = 4;
	databuf[1].buf = second_buff;
	databuf[2].len = 256;
	databuf[2].buf = third_buff;

	// pour toutes les sockets
	for (size_t i = 0; i < _clients.size(); i++)
		// si il y a de la data a lire
		if ((sel.Is_readable(_clients[i].get_socket())) == true)
		{
			//on reset les bails
			memset(&first_buff, '\0', 4);
			memset(&second_buff, '\0', 4);
			memset(&third_buff, '\0', 256);
			WSARecv(_clients[i].get_socket(), databuf, 3, &recvbytes, &flags, NULL, NULL);
			if (recvbytes == 0 || (std::string("").compare(databuf[0].buf)) == 0)
			{
				sel.Remove_checkread(_clients[i].get_socket());
				sel.Remove_checkwrite(_clients[i].get_socket());
				if (_clients.size() != 1)
					_clients.erase(_clients.begin() + i);
				else
					_clients.clear();
			}
			else
			{	
				int				len = strlen(databuf[2].buf);
				char			*packet = new char[len + 1];

				memcpy(packet, databuf[2].buf, len + 1);
				Message		message((uint32_t)*(databuf[0].buf), (uint32_t)*(databuf[1].buf), packet, _clients[i]);

				circbuff.add_data(message);
			}
		}
}
Beispiel #2
0
bool		TCPLinSocket::ReadData(CircularBuff &circbuff, Selector &sel)
{
  struct iovec	databuf[3];
  char		first_buff[4];
  char		second_buff[4];
  char		third_buff[256];
  int		recvbytes = 0;

  databuf[0].iov_len = 4;
  databuf[0].iov_base = first_buff;
  databuf[1].iov_len = 4;
  databuf[1].iov_base = second_buff;
  databuf[2].iov_len = 256;
  databuf[2].iov_base = third_buff;

  if ((sel.Is_readable(_fathersocket)) == true)
    {
      memset(&first_buff, '\0', 4);
      memset(&second_buff, '\0', 4);
      memset(&third_buff, '\0', 256);
      recvbytes = readv(_fathersocket, databuf, 3);
      if (recvbytes == 0 || (std::string("").compare((char *)databuf[0].iov_base)) == 0)
		{
		  sel.Remove_checkread(_fathersocket);
		  sel.Remove_checkwrite(_fathersocket);
		  if ((close(_fathersocket)) == -1)
			std::cerr << "Couldn't close the socket" << std::endl;
		  _fathersocket = INVALID_SOCKET;
		  std::cout << "Connection lost with the server" << std::endl;
		  return (false);
		}
	  int				len = strlen((char *)databuf[2].iov_base);
	  char				*packet = new char[len + 1];

	  memcpy(packet, databuf[2].iov_base, len + 1);
      Message		Message((uint32_t)*((char *)(databuf[0].iov_base)), (uint32_t)*((char *)(databuf[1].iov_base)),	packet);

      circbuff.add_data(Message);
    }
  return (true);
}