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"
30 NetworkClient::NetworkClient()
34 NetworkClient::~NetworkClient()
38 Connection
*NetworkClient::init(void)
42 Connection
*NetworkClient::init(Address addr
)
44 // Create network host
46 address
.host
= ENET_HOST_ANY
;
47 address
.port
= ENET_PORT_ANY
;
48 host
= enet_host_create(NULL
, 1, 0, 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);
63 enet_host_destroy(host
);
71 LINFO("Connected.\n");
74 void NetworkClient::shutdown(void)
83 // Delete network host
86 enet_host_destroy(host
);
91 bool NetworkClient::isConnected(void)
95 Connection
*NetworkClient::getConnection(void)
100 bool NetworkClient::doWork(void)
107 while (enet_host_service(host
, &event
, 0) > 0)
111 case ENET_EVENT_TYPE_CONNECT
:
112 conn
= new Connection(peer
);
113 LDEBUG("Client: Connected.\n");
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
,
121 conn
->injectData(buffer
);
124 case ENET_EVENT_TYPE_DISCONNECT
:
125 LDEBUG("Client: Disconnected.\n");