Imported more code from the old engine.
[peakengine.git] / engine / src / network / NetworkClient.cpp
blobb10306f66f9edccca7214fc9c7ae515f2c1a8f2e
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;
106 // Receive data
107 ENetEvent event;
108 while (enet_host_service(host, &event, 0) > 0)
110 switch (event.type)
112 case ENET_EVENT_TYPE_CONNECT:
113 conn = new Connection(peer);
114 LDEBUG("Client: Connected.\n");
115 break;
116 case ENET_EVENT_TYPE_RECEIVE:
118 //printf("Client: Received data.\n");
119 Buffer *buffer = new Buffer;
120 buffer->writeData(event.packet->data, event.packet->dataLength,
121 true);
122 conn->injectData(buffer);
123 break;
125 case ENET_EVENT_TYPE_DISCONNECT:
126 LDEBUG("Client: Disconnected.\n");
127 conn->destroy();
128 delete conn;
129 conn = 0;
130 return false;
131 break;
132 default:
133 break;
137 return true;