View Single Post
  #3  
Old 02-01-2002, 06:08 AM
alkrun
Sarnak
 
Join Date: Jan 2002
Posts: 66
Default

loginserver.cpp

Code:
bool InitLoginServer() {
	if (AttemptingConnect) {
		cout << "Error: InitLoginServer() while already attempting connect" << endl;
		return false;
	}
	if (!net.LoginServerInfo) {
		cout << "Error: Login server info not loaded" << endl;
		return false;
	}

	AttemptingConnect = true;
#ifdef WIN32
	SOCKET tmpsock = INVALID_SOCKET;
#else
	int tmpsock = INVALID_SOCKET;
#endif
	unsigned long nonblocking = 1;
    struct sockaddr_in	server_sin;
    struct in_addr	in;
	struct hostent *phostent = NULL;

#ifdef WIN32
	WORD version = MAKEWORD (1,1);
	WSADATA wsadata;
	WSAStartup (version, &wsadata);
#endif

	if ((tmpsock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)	{
#ifdef WIN32
		cout << "LoginServer connect: Allocating socket failed. Error: " << WSAGetLastError();
#else
		cout << "LoginServer connect: Allocating socket failed. Error: " << strerror(errno);
#endif
		AttemptingConnect = false;
		return false;
	}

	struct sockaddr_in worldaddress;

	unsigned long lWorldAddr = INADDR_NONE;
	memset((char *) &worldaddress, 0, sizeof(worldaddress));
	worldaddress.sin_family = AF_INET;
	worldaddress.sin_port = htons(INADDR_ANY);
	worldaddress.sin_addr.s_addr = htonl(INADDR_ANY);

	//GET WORLD SERVER ADDRESS
	if((lWorldAddr = inet_addr(net.GetWorldAddress())) != INADDR_NONE){
		if((phostent = gethostbyaddr((char*)&lWorldAddr, sizeof(unsigned long), AF_INET)) != NULL){
			memcpy ((char*)&(worldaddress.sin_addr), phostent->h_addr, phostent->h_length);
		}
	}else if((phostent = gethostbyname(net.GetWorldAddress())) != NULL){
		memcpy ((char*)&(worldaddress.sin_addr), phostent->h_addr, phostent->h_length);
	}

	if (bind(tmpsock, (struct sockaddr *) &worldaddress, sizeof(worldaddress)) < 0) 
	{
#ifdef WIN32
		cout << "Login socket failed(bind). Error: " << WSAGetLastError() << endl;
		closesocket(tmpsock);
#else
		cout << "Login socket failed(bind). Error: " << strerror(errno) << endl;
		close(tmpsock);
#endif
		return false;
	}
	server_sin.sin_family = AF_INET;

	if ((phostent = gethostbyname(net.GetLoginAddress())) == NULL) {
#ifdef WIN32
		cout << "Unable to get the host name. Error: " << WSAGetLastError();
	    closesocket(tmpsock);
#else
		cout << "Unable to get the host name. Error: " << strerror(errno);
	    close(tmpsock);
#endif
		AttemptingConnect = false;
		return false;
	}
	memcpy ((char*)&(server_sin.sin_addr), phostent->h_addr, phostent->h_length);
	server_sin.sin_port = htons(LOGIN_PORT);

	// Establish a connection to the server socket.
#ifdef WIN32
	if (connect (tmpsock, (PSOCKADDR) &server_sin, sizeof (server_sin)) == SOCKET_ERROR) {
		cout << "LoginServer connect: Connecting to the server failed. Error: " << WSAGetLastError() << endl;
		closesocket(tmpsock);
#else
	if (connect (tmpsock, (struct sockaddr *) &server_sin, sizeof (server_sin)) == SOCKET_ERROR) {
		cout << "LoginServer connect: Connecting to the server failed. Error: " << strerror(errno) << endl;
		close(tmpsock);
#endif
		AttemptingConnect = false;
		return false;
	}	
#ifdef WIN32
	ioctlsocket (tmpsock, FIONBIO, &nonblocking);
#else
	fcntl(tmpsock, F_SETFL, O_NONBLOCK);
#endif

	in.s_addr = server_sin.sin_addr.s_addr;
	cout << "Connected to LoginServer: " << net.GetLoginAddress() << ":" << ntohs(server_sin.sin_port) << endl;
	loginserver = new LoginServer(in.s_addr, ntohs(server_sin.sin_port), tmpsock);
	AttemptingConnect = false;
#ifdef WIN32
	_beginthread(LoginServerLoop, 0, NULL);
#else
	pthread_t thread;
	pthread_create(&thread, NULL, LoginServerLoop, NULL);
#endif
	return true;
}
Reply With Quote