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