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"
30 BroadcastClient::BroadcastClient()
33 bcclients
.push_back(this);
36 BroadcastClient::~BroadcastClient()
38 for (unsigned int i
= 0; i
< bcclients
.size(); i
++)
40 if (bcclients
[i
] == this)
42 bcclients
.erase(bcclients
.begin() + i
);
48 void BroadcastClient::start(int port
)
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.
57 enet_socket_set_option(socket
, ENET_SOCKOPT_NONBLOCK
, 1);
59 /*if (enet_socket_set_option(socket, ENET_SOCKOPT_BROADCAST, 1) == -1)
61 perror("ENET_SOCKOPT_BROADCAST");
65 LERROR("Started broadcast client (%d).\n", port
);
67 void BroadcastClient::stop(void)
69 enet_socket_destroy(socket
);
73 void BroadcastClient::clearList(void)
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
)
98 void BroadcastClient::doWork(float msecs
)
104 //printf("BroadcastClient: doWork().\n");
107 buffer
.data
= malloc(1024);
108 buffer
.dataLength
= 1024;
109 ENetAddress remoteaddr
;
112 bcaddr
.host
= ENET_HOST_BROADCAST
;
114 enet_address_set_host(&bcaddr
, "127.0.0.1");
116 // Message to be sent to all hosts
118 ENetBuffer msgbuffer
;
119 msgbuffer
.data
= msg
;
120 msgbuffer
.dataLength
= 4;
125 if (updatetime
> 250)
127 LERROR("Sending.\n");
128 if (enet_socket_send(socket
, &bcaddr
, &msgbuffer
, 1) == -1)
130 perror("Sending failed");
137 bool listchanged
= false;
139 while ((length
= enet_socket_receive(socket
, &remoteaddr
, &buffer
, 1) > 0)
144 LERROR("Socket error: %d\n", errno
);
149 LDEBUG("Got broadcast response.\n");
154 enet_address_get_host_ip(&remoteaddr
, addrstr
, 16);
155 Address
addr(addrstr
);
156 // FIXME: We need the proper port here!
160 // Look if server is already known
162 for (unsigned int i
= 0; i
< serveraddr
.size(); i
++)
164 if (serveraddr
[i
] == addr
)
167 if (serverinfo
[i
] != (char*)buffer
.data
)
170 serverinfo
[i
] = (char*)buffer
.data
;
177 // Add server to list
180 serveraddr
.push_back(addr
);
181 serverinfo
.push_back((char*)buffer
.data
);
187 // Call calback function
188 /*if (listchanged && cbscript && (cbfunc != ""))
190 cbscript->callFunction(cbfunc);
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
;