src/network/network_server.cpp
branchgamebalance
changeset 9895 7bd07f43b0e3
parent 6504 5578f2fa1a73
child 9911 0b8b245a2391
equal deleted inserted replaced
9894:70d78ac95d6c 9895:7bd07f43b0e3
   212 	// Function: Indication to the client that the server needs a password
   212 	// Function: Indication to the client that the server needs a password
   213 	// Data:
   213 	// Data:
   214 	//    uint8:  Type of password
   214 	//    uint8:  Type of password
   215 	//
   215 	//
   216 
   216 
       
   217 	/* Invalid packet when status is AUTH or higher */
       
   218 	if (cs->status >= STATUS_AUTH) return;
       
   219 
       
   220 	cs->status = STATUS_AUTHORIZING;
       
   221 
   217 	Packet *p = NetworkSend_Init(PACKET_SERVER_NEED_PASSWORD);
   222 	Packet *p = NetworkSend_Init(PACKET_SERVER_NEED_PASSWORD);
   218 	p->Send_uint8(type);
   223 	p->Send_uint8(type);
   219 	cs->Send_Packet(p);
   224 	cs->Send_Packet(p);
   220 }
   225 }
   221 
   226 
   690 	const NetworkClientInfo *ci;
   695 	const NetworkClientInfo *ci;
   691 
   696 
   692 	type = (NetworkPasswordType)p->Recv_uint8();
   697 	type = (NetworkPasswordType)p->Recv_uint8();
   693 	p->Recv_string(password, sizeof(password));
   698 	p->Recv_string(password, sizeof(password));
   694 
   699 
   695 	if (cs->status == STATUS_INACTIVE && type == NETWORK_GAME_PASSWORD) {
   700 	if (cs->status == STATUS_AUTHORIZING && type == NETWORK_GAME_PASSWORD) {
   696 		// Check game-password
   701 		// Check game-password
   697 		if (strcmp(password, _network_game_info.server_password) != 0) {
   702 		if (strcmp(password, _network_game_info.server_password) != 0) {
   698 			// Password is invalid
   703 			// Password is invalid
   699 			SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
   704 			SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
   700 			return;
   705 			return;
   708 		}
   713 		}
   709 
   714 
   710 		// Valid password, allow user
   715 		// Valid password, allow user
   711 		SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
   716 		SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
   712 		return;
   717 		return;
   713 	} else if (cs->status == STATUS_INACTIVE && type == NETWORK_COMPANY_PASSWORD) {
   718 	} else if (cs->status == STATUS_AUTHORIZING && type == NETWORK_COMPANY_PASSWORD) {
   714 		ci = DEREF_CLIENT_INFO(cs);
   719 		ci = DEREF_CLIENT_INFO(cs);
   715 
   720 
   716 		if (strcmp(password, _network_player_info[ci->client_playas].password) != 0) {
   721 		if (strcmp(password, _network_player_info[ci->client_playas].password) != 0) {
   717 			// Password is invalid
   722 			// Password is invalid
   718 			SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
   723 			SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
  1221 
  1226 
  1222 // If this fails, check the array above with network_data.h
  1227 // If this fails, check the array above with network_data.h
  1223 assert_compile(lengthof(_network_server_packet) == PACKET_END);
  1228 assert_compile(lengthof(_network_server_packet) == PACKET_END);
  1224 
  1229 
  1225 // This update the company_info-stuff
  1230 // This update the company_info-stuff
  1226 void NetworkPopulateCompanyInfo(void)
  1231 void NetworkPopulateCompanyInfo()
  1227 {
  1232 {
  1228 	char password[NETWORK_PASSWORD_LENGTH];
  1233 	char password[NETWORK_PASSWORD_LENGTH];
  1229 	const Player *p;
  1234 	const Player *p;
  1230 	const Vehicle *v;
  1235 	const Vehicle *v;
  1231 	const Station *s;
  1236 	const Station *s;
  1274 	// Go through all vehicles and count the type of vehicles
  1279 	// Go through all vehicles and count the type of vehicles
  1275 	FOR_ALL_VEHICLES(v) {
  1280 	FOR_ALL_VEHICLES(v) {
  1276 		if (!IsValidPlayer(v->owner)) continue;
  1281 		if (!IsValidPlayer(v->owner)) continue;
  1277 
  1282 
  1278 		switch (v->type) {
  1283 		switch (v->type) {
  1279 			case VEH_Train:
  1284 			case VEH_TRAIN:
  1280 				if (IsFrontEngine(v)) _network_player_info[v->owner].num_vehicle[0]++;
  1285 				if (IsFrontEngine(v)) _network_player_info[v->owner].num_vehicle[0]++;
  1281 				break;
  1286 				break;
  1282 
  1287 
  1283 			case VEH_Road:
  1288 			case VEH_ROAD:
  1284 				if (v->cargo_type != CT_PASSENGERS) {
  1289 				if (v->cargo_type != CT_PASSENGERS) {
  1285 					_network_player_info[v->owner].num_vehicle[1]++;
  1290 					_network_player_info[v->owner].num_vehicle[1]++;
  1286 				} else {
  1291 				} else {
  1287 					_network_player_info[v->owner].num_vehicle[2]++;
  1292 					_network_player_info[v->owner].num_vehicle[2]++;
  1288 				}
  1293 				}
  1289 				break;
  1294 				break;
  1290 
  1295 
  1291 			case VEH_Aircraft:
  1296 			case VEH_AIRCRAFT:
  1292 				if (IsNormalAircraft(v)) _network_player_info[v->owner].num_vehicle[3]++;
  1297 				if (IsNormalAircraft(v)) _network_player_info[v->owner].num_vehicle[3]++;
  1293 				break;
  1298 				break;
  1294 
  1299 
  1295 			case VEH_Ship:
  1300 			case VEH_SHIP:
  1296 				_network_player_info[v->owner].num_vehicle[4]++;
  1301 				_network_player_info[v->owner].num_vehicle[4]++;
  1297 				break;
  1302 				break;
  1298 
  1303 
  1299 			case VEH_Special:
  1304 			case VEH_SPECIAL:
  1300 			case VEH_Disaster:
  1305 			case VEH_DISASTER:
  1301 				break;
  1306 				break;
  1302 		}
  1307 		}
  1303 	}
  1308 	}
  1304 
  1309 
  1305 	// Go through all stations and count the types of stations
  1310 	// Go through all stations and count the types of stations
  1348 		SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, ci);
  1353 		SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, ci);
  1349 	}
  1354 	}
  1350 }
  1355 }
  1351 
  1356 
  1352 /* Check if we want to restart the map */
  1357 /* Check if we want to restart the map */
  1353 static void NetworkCheckRestartMap(void)
  1358 static void NetworkCheckRestartMap()
  1354 {
  1359 {
  1355 	if (_network_restart_game_year != 0 && _cur_year >= _network_restart_game_year) {
  1360 	if (_network_restart_game_year != 0 && _cur_year >= _network_restart_game_year) {
  1356 		DEBUG(net, 0, "Auto-restarting map. Year %d reached", _cur_year);
  1361 		DEBUG(net, 0, "Auto-restarting map. Year %d reached", _cur_year);
  1357 
  1362 
  1358 		StartNewGameWithoutGUI(GENERATE_NEW_SEED);
  1363 		StartNewGameWithoutGUI(GENERATE_NEW_SEED);
  1362 /* Check if the server has autoclean_companies activated
  1367 /* Check if the server has autoclean_companies activated
  1363     Two things happen:
  1368     Two things happen:
  1364       1) If a company is not protected, it is closed after 1 year (for example)
  1369       1) If a company is not protected, it is closed after 1 year (for example)
  1365       2) If a company is protected, protection is disabled after 3 years (for example)
  1370       2) If a company is protected, protection is disabled after 3 years (for example)
  1366            (and item 1. happens a year later) */
  1371            (and item 1. happens a year later) */
  1367 static void NetworkAutoCleanCompanies(void)
  1372 static void NetworkAutoCleanCompanies()
  1368 {
  1373 {
  1369 	NetworkTCPSocketHandler *cs;
  1374 	NetworkTCPSocketHandler *cs;
  1370 	const NetworkClientInfo *ci;
  1375 	const NetworkClientInfo *ci;
  1371 	const Player *p;
  1376 	const Player *p;
  1372 	bool clients_in_company[MAX_PLAYERS];
  1377 	bool clients_in_company[MAX_PLAYERS];
  1531 			int lag = NetworkCalculateLag(cs);
  1536 			int lag = NetworkCalculateLag(cs);
  1532 			if (lag > _network_max_join_time) {
  1537 			if (lag > _network_max_join_time) {
  1533 				IConsolePrintF(_icolour_err,"Client #%d is dropped because it took longer than %d ticks for him to join", cs->index, _network_max_join_time);
  1538 				IConsolePrintF(_icolour_err,"Client #%d is dropped because it took longer than %d ticks for him to join", cs->index, _network_max_join_time);
  1534 				NetworkCloseClient(cs);
  1539 				NetworkCloseClient(cs);
  1535 			}
  1540 			}
       
  1541 		} else if (cs->status == STATUS_INACTIVE) {
       
  1542 			int lag = NetworkCalculateLag(cs);
       
  1543 			if (lag > 4 * DAY_TICKS) {
       
  1544 				IConsolePrintF(_icolour_err,"Client #%d is dropped because it took longer than %d ticks to start the joining process", cs->index, 4 * DAY_TICKS);
       
  1545 				NetworkCloseClient(cs);
       
  1546 			}
  1536 		}
  1547 		}
  1537 
  1548 
  1538 		if (cs->status >= STATUS_PRE_ACTIVE) {
  1549 		if (cs->status >= STATUS_PRE_ACTIVE) {
  1539 			// Check if we can send command, and if we have anything in the queue
  1550 			// Check if we can send command, and if we have anything in the queue
  1540 			NetworkHandleCommandQueue(cs);
  1551 			NetworkHandleCommandQueue(cs);
  1551 
  1562 
  1552 	/* See if we need to advertise */
  1563 	/* See if we need to advertise */
  1553 	NetworkUDPAdvertise();
  1564 	NetworkUDPAdvertise();
  1554 }
  1565 }
  1555 
  1566 
  1556 void NetworkServerYearlyLoop(void)
  1567 void NetworkServerYearlyLoop()
  1557 {
  1568 {
  1558 	NetworkCheckRestartMap();
  1569 	NetworkCheckRestartMap();
  1559 }
  1570 }
  1560 
  1571 
  1561 void NetworkServerMonthlyLoop(void)
  1572 void NetworkServerMonthlyLoop()
  1562 {
  1573 {
  1563 	NetworkAutoCleanCompanies();
  1574 	NetworkAutoCleanCompanies();
  1564 }
  1575 }
  1565 
  1576 
  1566 #endif /* ENABLE_NETWORK */
  1577 #endif /* ENABLE_NETWORK */