src/proto2/NetworkClient.cc
changeset 23 8d802b573cf0
parent 22 b70d30e1b0fe
child 24 b81cb670e6b2
equal deleted inserted replaced
22:b70d30e1b0fe 23:8d802b573cf0
     1 
     1 
     2 #include "NetworkClient.hh"
     2 #include "NetworkClient.hh"
     3 
     3 #include "Engine.hh"
     4 // XXX: replace logging
     4 #include "Logger.hh"
     5 #include <iostream>
       
     6 
     5 
     7 NetworkClient::NetworkClient (GameState &state, const CL_IPAddress &connect_to) : 
     6 NetworkClient::NetworkClient (GameState &state, const CL_IPAddress &connect_to) : 
     8 	NetworkCore(state), server(netsession.connect(connect_to)) {
     7 	NetworkCore(state), server(netsession.connect(connect_to)) {
     9 	
     8 	
    10 	// connect slots
     9 	// connect slots
    14 }
    13 }
    15 
    14 
    16 void NetworkClient::on_create_object (CL_NetObject_Client &obj, int msg_type, CL_NetPacket &pkt) {
    15 void NetworkClient::on_create_object (CL_NetObject_Client &obj, int msg_type, CL_NetPacket &pkt) {
    17 	switch (msg_type) {
    16 	switch (msg_type) {
    18 		case NETMSG_SERVER_HELLO:
    17 		case NETMSG_SERVER_HELLO:
    19 			std::cout << "INFO [client.on_create_object] NETMSG_SERVER_HELLO" << std::endl;
       
    20 
       
    21 			on_server_hello(obj, pkt);
    18 			on_server_hello(obj, pkt);
    22 
    19 
    23 			break;
    20 			break;
    24 		
    21 		
    25 		case NETMSG_PLAYER_INFO:
    22 		case NETMSG_PLAYER_INFO:
    26 			std::cout << "INFO [client.on_create_object] NETMSG_PLAYER_INFO" << std::endl;
       
    27 
       
    28 			on_player_info(obj, pkt);
    23 			on_player_info(obj, pkt);
    29 
    24 
    30 			break;
    25 			break;
    31 		
    26 		
    32 		case NETMSG_PLAYER_JOIN:
    27 		case NETMSG_PLAYER_JOIN:
    33 			std::cout << "INFO [client.on_create_object] NETMSG_PLAYER_JOIN" << std::endl;
       
    34 
       
    35 			on_player_join(obj, pkt);
    28 			on_player_join(obj, pkt);
    36 
    29 
    37 			break;
    30 			break;
    38 
    31 
    39 		default:
    32 		default:
    40 			std::cerr << "WARN [client.on_create_object] unknown msg_type=" << msg_type << std::endl; 
    33 			Engine::log(WARN, "client.on_create_object") << "unknown msg_type=" << msg_type << " for obj=" << obj;
    41 	}
    34 	}
    42 }
    35 }
    43 		
    36 		
    44 void NetworkClient::on_server_hello (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
    37 void NetworkClient::on_server_hello (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
    45 	// read the packet
    38 	// read the packet
    46 	uint32_t x = pkt.input.read_uint32();
    39 	uint32_t x = pkt.input.read_uint32();
    47 	uint32_t y = pkt.input.read_uint32();
    40 	uint32_t y = pkt.input.read_uint32();
       
    41 
       
    42 	Coordinate initial_position(x, y);
    48 	
    43 	
    49 	Coordinate initial_position(x, y);
    44 	Engine::log(INFO, "client.on_server_hello") << "obj=" << obj << ", pos=" << initial_position;
    50 
    45 
    51 	// create the LocalPlayer object
    46 	// create the LocalPlayer object
    52 	NetworkClientLocalPlayer *player = new NetworkClientLocalPlayer(*this, obj, initial_position);
    47 	NetworkClientLocalPlayer *player = new NetworkClientLocalPlayer(*this, obj, initial_position);
    53 
    48 
    54 	// inform state
    49 	// inform state
    57 		
    52 		
    58 void NetworkClient::on_player_info (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
    53 void NetworkClient::on_player_info (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
    59 	// read the packet
    54 	// read the packet
    60 	uint32_t x = pkt.input.read_uint32();
    55 	uint32_t x = pkt.input.read_uint32();
    61 	uint32_t y = pkt.input.read_uint32();
    56 	uint32_t y = pkt.input.read_uint32();
    62 
    57 	
    63 	Coordinate initial_position(x, y);
    58 	Coordinate initial_position(x, y);
       
    59 	
       
    60 	Engine::log(INFO, "client.on_player_info") << "obj=" << obj << ", pos=" << initial_position;
    64 
    61 
    65 	// create the LocalPlayer object
    62 	// create the LocalPlayer object
    66 	NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, initial_position);
    63 	NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, initial_position);
    67 
    64 
    68 	// inform state
    65 	// inform state
    75 	uint32_t x = pkt.input.read_uint32();
    72 	uint32_t x = pkt.input.read_uint32();
    76 	uint32_t y = pkt.input.read_uint32();
    73 	uint32_t y = pkt.input.read_uint32();
    77 	
    74 	
    78 	Coordinate initial_position(x, y);
    75 	Coordinate initial_position(x, y);
    79 	
    76 	
       
    77 	Engine::log(INFO, "client.on_player_join") << "obj=" << obj << ", pos=" << initial_position;
       
    78 	
    80 	// create the RemotePlayer object
    79 	// create the RemotePlayer object
    81 	NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, initial_position);
    80 	NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, initial_position);
    82 
    81 
    83 	// inform state
    82 	// inform state
    84 	state.newRemotePlayer(player);
    83 	state.newRemotePlayer(player);
    87 void NetworkClient::player_quit (NetworkClientRemotePlayer *player) {
    86 void NetworkClient::player_quit (NetworkClientRemotePlayer *player) {
    88 	// inform state
    87 	// inform state
    89 	state.removePlayer(player);
    88 	state.removePlayer(player);
    90 
    89 
    91 	// delete
    90 	// delete
    92 	delete player;
    91 	// XXX: leak because deleting the slot while it's being called breaks ClanLib
       
    92 	//	delete player;
    93 }
    93 }
    94 
    94 
    95 NetworkClientLocalPlayer::NetworkClientLocalPlayer (NetworkClient &client, CL_NetObject_Client &obj, Coordinate initial_position) :
    95 NetworkClientLocalPlayer::NetworkClientLocalPlayer (NetworkClient &client, CL_NetObject_Client &obj, Coordinate initial_position) :
    96 	LocalPlayer(initial_position, true), client(client), obj(obj) {
    96 	LocalPlayer(initial_position, true), client(client), obj(obj) {
    97 	
    97 	
    98 	// receive messages
    98 	// receive messages
    99 	slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientLocalPlayer::on_position);
    99 	slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientLocalPlayer::on_position);
   100 
       
   101 }
   100 }
   102 		
   101 		
   103 void NetworkClientLocalPlayer::move (PositionDelta d) {
   102 void NetworkClientLocalPlayer::move (PositionDelta d) {
   104 	CL_NetPacket pkt;
   103 	CL_NetPacket pkt;
   105 	pkt.output.write_uint32(d.dx);
   104 	pkt.output.write_uint32(d.dx);
   109 }
   108 }
   110 		
   109 		
   111 void NetworkClientLocalPlayer::on_position (CL_NetPacket &pkt) {
   110 void NetworkClientLocalPlayer::on_position (CL_NetPacket &pkt) {
   112 	uint32_t x = pkt.input.read_uint32();
   111 	uint32_t x = pkt.input.read_uint32();
   113 	uint32_t y = pkt.input.read_uint32();
   112 	uint32_t y = pkt.input.read_uint32();
       
   113 
       
   114 	Coordinate pos (x, y);
       
   115 
       
   116 	Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", pos=" << pos;
   114 	
   117 	
   115 	updatePosition(Coordinate(x, y));
   118 	updatePosition(pos);
   116 }
   119 }
   117 		
   120 		
   118 NetworkClientRemotePlayer::NetworkClientRemotePlayer (NetworkClient &client, CL_NetObject_Client &obj, Coordinate initial_position) :
   121 NetworkClientRemotePlayer::NetworkClientRemotePlayer (NetworkClient &client, CL_NetObject_Client &obj, Coordinate initial_position) :
   119 	RemotePlayer(initial_position, true), client(client), obj(obj) {
   122 	RemotePlayer(initial_position, true), client(client), obj(obj) {
   120 	
   123 	
   121 	// receive messages
   124 	// receive messages
   122 	slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientRemotePlayer::on_position);
   125 	slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientRemotePlayer::on_position);
       
   126 	slots.connect(obj.sig_received_message(NETMSG_PLAYER_QUIT), this, &NetworkClientRemotePlayer::on_quit);
   123 }
   127 }
   124 
   128 
   125 void NetworkClientRemotePlayer::on_position (CL_NetPacket &pkt) {
   129 void NetworkClientRemotePlayer::on_position (CL_NetPacket &pkt) {
   126 	uint32_t x = pkt.input.read_uint32();
   130 	uint32_t x = pkt.input.read_uint32();
   127 	uint32_t y = pkt.input.read_uint32();
   131 	uint32_t y = pkt.input.read_uint32();
   128 	
   132 	
   129 	updatePosition(Coordinate(x, y));
   133 	Coordinate pos (x, y);
       
   134 
       
   135 	Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", pos=" << pos;
       
   136 	
       
   137 	updatePosition(pos);
   130 }
   138 }
   131 
   139 
   132 void NetworkClientRemotePlayer::on_quit (CL_NetPacket &pkt) {
   140 void NetworkClientRemotePlayer::on_quit (CL_NetPacket &pkt) {
   133 	// pkt is empty
   141 	// pkt is empty
   134 	(void) pkt;
   142 	(void) pkt;
   135 
   143 
       
   144 	Engine::log(INFO, "client_player.on_quit") << "obj=" << obj;
       
   145 
   136 	client.player_quit(this);
   146 	client.player_quit(this);
   137 }
   147 }