Пример #1
0
bool			TCPLinServSocket::SearchNewClients(Selector &sel)
{
	SOCKET		newclientsocket;
	int			highestid = 1;
	struct sockaddr_in	s_in;
	socklen_t		s_size;

	s_size = sizeof(s_in);
	if ((sel.Is_readable(_fathersocket)) == true)
	{
		if ((newclientsocket = accept(_fathersocket, (struct sockaddr *)&s_in, &s_size)) == INVALID_SOCKET)
		{
			std::cerr << "Couldn't accept a new client" << std::endl;
			return (false);
		}
		sel.Add_to_checkread(newclientsocket);
		sel.Add_to_checkwrite(newclientsocket);
		for (size_t i = 0; i < _clients.size(); i++)
		if (_clients[i].get_id() >= highestid)
			highestid = _clients[i].get_id() + 1;

		Client	newclient(newclientsocket, highestid);
		_clients.push_back(newclient);
	}
	return (true);
}
Пример #2
0
bool		TCPWinServSocket::SearchNewClients(Selector &sel)
{
	SOCKET	newclientsocket;
	int		highestid = 1;

	if ((sel.Is_readable(_fathersocket)) == true)
	{
		if ((newclientsocket = WSAAccept(_fathersocket, NULL, NULL, NULL, NULL)) == INVALID_SOCKET)
		{
			std::cerr << "Couldn't accept a new client" << std::endl;
			return (false);
		}
		sel.Add_to_checkread(newclientsocket);
		sel.Add_to_checkwrite(newclientsocket);

		for (size_t i = 0; i < _clients.size(); i++)
			if (_clients[i].get_id() >= highestid)
				highestid = _clients[i].get_id() + 1;
		
		Client	newclient(newclientsocket, highestid);

		_clients.push_back(newclient);
	}
	return (true);
}
Пример #3
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);
			}
		}
}
Пример #4
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);
}