- Partly implemented client side prediction (still unstable).
[peakengine.git] / engine / src / network / NetworkClient.cpp
blob7e26f1949387a98718d2defe54cac48606fb6bff
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "network/NetworkClient.h"
23 #include "network/Connection.h"
24 #include "core/Logger.h"
26 #include <stdio.h>
28 namespace peak
30 NetworkClient::NetworkClient()
32 host = 0;
34 NetworkClient::~NetworkClient()
38 Connection *NetworkClient::init(void)
40 return 0;
42 Connection *NetworkClient::init(Address addr)
44 // Create network host
45 ENetAddress address;
46 address.host = ENET_HOST_ANY;
47 address.port = ENET_PORT_ANY;
48 host = enet_host_create(NULL, 1, 0, 0);
49 if (!host)
51 return 0;
55 // Create network peer
56 //ENetAddress address;
57 enet_address_set_host(&address, addr.getAddressString(false).c_str());
58 address.port = addr.getPort();
59 LINFO("Client: Address: %X:%d\n", address.host, address.port);
60 peer = enet_host_connect(host, &address, 2);
61 if (!peer)
63 enet_host_destroy(host);
64 return 0;
66 conn = 0;
67 while (!conn)
69 doWork();
71 LINFO("Connected.\n");
72 return conn;
74 void NetworkClient::shutdown(void)
76 // Close connection
77 if (conn)
79 conn->destroy();
80 delete conn;
81 conn = 0;
83 // Delete network host
84 if (host)
86 enet_host_destroy(host);
87 host = 0;
91 bool NetworkClient::isConnected(void)
93 return (conn != 0);
95 Connection *NetworkClient::getConnection(void)
97 return conn;
100 bool NetworkClient::doWork(void)
102 if (!host)
103 return false;
105 // Receive data
106 ENetEvent event;
107 while (enet_host_service(host, &event, 0) > 0)
109 switch (event.type)
111 case ENET_EVENT_TYPE_CONNECT:
112 conn = new Connection(peer);
113 LDEBUG("Client: Connected.\n");
114 break;
115 case ENET_EVENT_TYPE_RECEIVE:
117 //printf("Client: Received data.\n");
118 Buffer *buffer = new Buffer;
119 buffer->writeData(event.packet->data, event.packet->dataLength,
120 true);
121 conn->injectData(buffer);
122 break;
124 case ENET_EVENT_TYPE_DISCONNECT:
125 LDEBUG("Client: Disconnected.\n");
126 conn->destroy();
127 delete conn;
128 conn = 0;
129 return false;
130 default:
131 break;
135 return true;