Imported more code from the old engine.
[peakengine.git] / engine / src / network / BroadcastClient.cpp
blob0d3b8b47ea67178dac5edbb7003113716b5aa8ea
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/BroadcastClient.h"
23 //#include "core/Script.h"
24 #include "core/Logger.h"
26 #include <errno.h>
28 namespace peak
30 BroadcastClient::BroadcastClient()
32 port = 0;
33 bcclients.push_back(this);
34 //cbscript = 0;
36 BroadcastClient::~BroadcastClient()
38 for (unsigned int i = 0; i < bcclients.size(); i++)
40 if (bcclients[i] == this)
42 bcclients.erase(bcclients.begin() + i);
43 return;
48 void BroadcastClient::start(int port)
50 // Create raw socket
51 ENetAddress address;
52 address.host = ENET_HOST_ANY;
53 address.port = ENET_PORT_ANY;
54 socket = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM, &address);
55 // enet 1.2 needs ENET_SOCKOPT_NONBLOCK, enet 1.1 does not provide it.
56 #ifdef _ENET_1_2_
57 enet_socket_set_option(socket, ENET_SOCKOPT_NONBLOCK, 1);
58 #endif
59 /*if (enet_socket_set_option(socket, ENET_SOCKOPT_BROADCAST, 1) == -1)
61 perror("ENET_SOCKOPT_BROADCAST");
62 }*/
63 this->port = port;
64 updatetime = 1000;
65 LERROR("Started broadcast client (%d).\n", port);
67 void BroadcastClient::stop(void)
69 enet_socket_destroy(socket);
70 port = 0;
73 void BroadcastClient::clearList(void)
75 serverinfo.clear();
76 serveraddr.clear();
79 int BroadcastClient::getServerCount(void)
81 return serverinfo.size();
83 std::string BroadcastClient::getServerInfo(int index)
85 return serverinfo[index];
87 Address BroadcastClient::getServerAddress(int index)
89 return serveraddr[index];
92 void BroadcastClient::setCallback(Script *script, std::string function)
94 cbscript = script;
95 cbfunc = function;
98 void BroadcastClient::doWork(float msecs)
100 if (!port)
101 return;
104 //printf("BroadcastClient: doWork().\n");
106 ENetBuffer buffer;
107 buffer.data = malloc(1024);
108 buffer.dataLength = 1024;
109 ENetAddress remoteaddr;
110 // Broadcast address
111 ENetAddress bcaddr;
112 bcaddr.host = ENET_HOST_BROADCAST;
113 bcaddr.port = 14141;
114 enet_address_set_host(&bcaddr, "127.0.0.1");
116 // Message to be sent to all hosts
117 char msg[] = "PING";
118 ENetBuffer msgbuffer;
119 msgbuffer.data = msg;
120 msgbuffer.dataLength = 4;
123 // Broadcast
124 updatetime += msecs;
125 if (updatetime > 250)
127 LERROR("Sending.\n");
128 if (enet_socket_send(socket, &bcaddr, &msgbuffer, 1) == -1)
130 perror("Sending failed");
132 updatetime = 0;
136 // Receive answers
137 bool listchanged = false;
138 int length;
139 while ((length = enet_socket_receive(socket, &remoteaddr, &buffer, 1) > 0)
140 != 0)
142 if (length < 0)
144 LERROR("Socket error: %d\n", errno);
145 break;
147 if (length > 0)
149 LDEBUG("Got broadcast response.\n");
152 // Get address
153 char addrstr[16];
154 enet_address_get_host_ip(&remoteaddr, addrstr, 16);
155 Address addr(addrstr);
156 // FIXME: We need the proper port here!
157 addr.setPort(0);
160 // Look if server is already known
161 bool newaddr = true;
162 for (unsigned int i = 0; i < serveraddr.size(); i++)
164 if (serveraddr[i] == addr)
166 newaddr = false;
167 if (serverinfo[i] != (char*)buffer.data)
169 listchanged = true;
170 serverinfo[i] = (char*)buffer.data;
172 break;
177 // Add server to list
178 if (newaddr)
180 serveraddr.push_back(addr);
181 serverinfo.push_back((char*)buffer.data);
182 listchanged = true;
187 // Call calback function
188 /*if (listchanged && cbscript && (cbfunc != ""))
190 cbscript->callFunction(cbfunc);
193 free(buffer.data);
196 void BroadcastClient::doAllWork(float msecs)
198 for (unsigned int i = 0; i < bcclients.size(); i++)
200 bcclients[i]->doWork(msecs);
204 std::vector<BroadcastClient*> BroadcastClient::bcclients;