src/proto2/NetworkClient.cc
author terom
Sun, 09 Nov 2008 21:51:13 +0000
changeset 25 af75a1894a32
parent 24 b81cb670e6b2
child 26 5685602aeb9c
permissions -rw-r--r--
simple proto *almost* works
21
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     1
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     2
#include "NetworkClient.hh"
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
     3
#include "Engine.hh"
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
     4
#include "Logger.hh"
21
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     5
25
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
     6
#include <cassert>
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
     7
21
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
     8
NetworkClient::NetworkClient (GameState &state, const CL_IPAddress &connect_to) : 
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
     9
    NetworkCore(state), server(netsession.connect(connect_to)) {
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    10
    
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    11
    // connect slots
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    12
    slots.connect(netobjs.sig_create_object(), this, &NetworkClient::on_create_object);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    13
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    14
    // XXX: sig_disconnected
21
32c6cc55256a change proto2 network code to use CL_NetSession
terom
parents:
diff changeset
    15
}
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    16
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    17
void NetworkClient::on_create_object (CL_NetObject_Client &obj, int msg_type, CL_NetPacket &pkt) {
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    18
    switch (msg_type) {
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    19
        case NETMSG_SERVER_HELLO:
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    20
            on_server_hello(obj, pkt);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    21
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    22
            break;
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    23
        
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    24
        case NETMSG_PLAYER_INFO:
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    25
            on_player_info(obj, pkt);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    26
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    27
            break;
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    28
        
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    29
        case NETMSG_PLAYER_JOIN:
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    30
            on_player_join(obj, pkt);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    31
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    32
            break;
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    33
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    34
        default:
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    35
            Engine::log(WARN, "client.on_create_object") << "unknown msg_type=" << msg_type << " for obj=" << obj;
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    36
    }
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    37
}
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    38
        
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    39
void NetworkClient::on_server_hello (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    40
    // read the packet
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    41
    uint32_t x = pkt.input.read_uint32();
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    42
    uint32_t y = pkt.input.read_uint32();
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
    43
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    44
    Coordinate initial_position(x, y);
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    45
    
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    46
    Engine::log(INFO, "client.on_server_hello") << "obj=" << obj << ", pos=" << initial_position;
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    47
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    48
    // create the LocalPlayer object
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    49
    NetworkClientLocalPlayer *player = new NetworkClientLocalPlayer(*this, obj, initial_position);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    50
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    51
    // inform state
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    52
    state.newLocalPlayer(player);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    53
}
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    54
        
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    55
void NetworkClient::on_player_info (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    56
    // read the packet
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    57
    uint32_t x = pkt.input.read_uint32();
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    58
    uint32_t y = pkt.input.read_uint32();
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    59
    
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    60
    Coordinate initial_position(x, y);
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    61
    
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    62
    Engine::log(INFO, "client.on_player_info") << "obj=" << obj << ", pos=" << initial_position;
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    63
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    64
    // create the LocalPlayer object
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    65
    NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, initial_position);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    66
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    67
    // inform state
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    68
    state.newRemotePlayer(player);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    69
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    70
}
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    71
        
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    72
void NetworkClient::on_player_join (CL_NetObject_Client &obj, CL_NetPacket &pkt) {
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    73
    // read the packet
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    74
    uint32_t x = pkt.input.read_uint32();
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    75
    uint32_t y = pkt.input.read_uint32();
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    76
    
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    77
    Coordinate initial_position(x, y);
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    78
    
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    79
    Engine::log(INFO, "client.on_player_join") << "obj=" << obj << ", pos=" << initial_position;
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    80
    
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    81
    // create the RemotePlayer object
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    82
    NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, initial_position);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    83
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    84
    // inform state
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    85
    state.newRemotePlayer(player);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    86
}
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    87
        
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    88
void NetworkClient::player_quit (NetworkClientRemotePlayer *player) {
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    89
    // inform state
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    90
    state.removePlayer(player);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    91
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    92
    // delete
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    93
    // XXX: leak because deleting the slot while it's being called breaks ClanLib
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    94
    //  delete player;
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    95
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    96
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
    97
NetworkClientLocalPlayer::NetworkClientLocalPlayer (NetworkClient &client, CL_NetObject_Client &obj, Coordinate initial_position) :
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    98
    LocalPlayer(initial_position, true), client(client), obj(obj) {
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
    99
    
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   100
    // receive messages
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   101
    slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientLocalPlayer::on_position);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   102
}
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   103
        
25
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
   104
bool NetworkClientLocalPlayer::move (PositionDelta d) {
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
   105
    // always send move, in all cases
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   106
    CL_NetPacket pkt;
25
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
   107
    pkt.output.write_int32(d.dx);
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
   108
    pkt.output.write_int32(d.dy);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   109
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   110
    obj.send(NETMSG_CLIENT_MOVE, pkt, false);
25
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
   111
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
   112
    // return validity
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
   113
    return LocalPlayer::move(d);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   114
}
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   115
        
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   116
void NetworkClientLocalPlayer::on_position (CL_NetPacket &pkt) {
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   117
    uint32_t x = pkt.input.read_uint32();
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   118
    uint32_t y = pkt.input.read_uint32();
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   119
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   120
    Coordinate pos (x, y);
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   121
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   122
    Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", pos=" << pos;
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   123
    
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   124
    updatePosition(pos);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   125
}
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   126
        
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   127
NetworkClientRemotePlayer::NetworkClientRemotePlayer (NetworkClient &client, CL_NetObject_Client &obj, Coordinate initial_position) :
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   128
    RemotePlayer(initial_position, true), client(client), obj(obj) {
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   129
    
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   130
    // receive messages
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   131
    slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientRemotePlayer::on_position);
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   132
    slots.connect(obj.sig_received_message(NETMSG_PLAYER_QUIT), this, &NetworkClientRemotePlayer::on_quit);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   133
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   134
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   135
void NetworkClientRemotePlayer::on_position (CL_NetPacket &pkt) {
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   136
    uint32_t x = pkt.input.read_uint32();
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   137
    uint32_t y = pkt.input.read_uint32();
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   138
    
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   139
    Coordinate pos (x, y);
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   140
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   141
    Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", pos=" << pos;
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   142
    
25
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
   143
    assert(updatePosition(pos));
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   144
}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   145
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   146
void NetworkClientRemotePlayer::on_quit (CL_NetPacket &pkt) {
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   147
    // pkt is empty
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   148
    (void) pkt;
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   149
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   150
    Engine::log(INFO, "client_player.on_quit") << "obj=" << obj;
23
8d802b573cf0 fixed more network code, there's actually a high probability of it working now
terom
parents: 22
diff changeset
   151
24
b81cb670e6b2 the great :retab
terom
parents: 23
diff changeset
   152
    client.player_quit(this);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 21
diff changeset
   153
}