Imported more code from the old engine.
[peakengine.git] / engine / include / network / BroadcastClient.h
blob55082823180b7f84c759b76fa94b6ea7ff7ad0af
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 #ifndef _BROADCASTCLIENT_H_
23 #define _BROADCASTCLIENT_H_
25 #include <enet/enet.h>
26 #include <string>
27 #include <vector>
29 #include "core/Address.h"
31 //tolua_begin
32 namespace peak
34 class Script;
36 /**
37 * \brief Socket which sends broadcast messages at a fixed interval to get the
38 * address of local network servers.
40 class BroadcastClient
42 public:
43 BroadcastClient();
44 ~BroadcastClient();
46 /**
47 * \brief Starts broadcasting.
48 * \param port Port to send messages at
50 void start(int port);
51 /**
52 * \brief Stops broadcasting
54 void stop(void);
56 /**
57 * \brief Clears server list.
59 void clearList(void);
61 /**
62 * \brief Returns the number of found servers.
64 int getServerCount(void);
65 /**
66 * \brief Returns the server info of one server.
67 * \param index Index of the server, can be in the range 0..getServerCount().
68 * \return Server reply
70 std::string getServerInfo(int index);
71 /**
72 * \brief Returns the address of one server.
73 * \param index Index of the server, can be in the range 0..getServerCount().
74 * \return Server address
76 Address getServerAddress(int index);
78 /**
79 * \brief Sets a callback function which gets called everytime the
80 * server list changes.
81 * \param script Lua script which defines the function
82 * \param function Function to call
84 void setCallback(Script *script, std::string function);
86 /**
87 * \brief Sends a broadcast message if needed, handles incoming responses.
88 * \param msecs Passed time since last call of this function
90 void doWork(float msecs = 0);
92 /**
93 * \brief Updates all broadcast clients.
94 * \param msecs Passed time since last call of this function
96 static void doAllWork(float msecs);
97 private:
98 //tolua_end
99 ENetSocket socket;
100 int port;
102 float updatetime;
104 Script *cbscript;
105 std::string cbfunc;
107 std::vector<std::string> serverinfo;
108 std::vector<Address> serveraddr;
110 static std::vector<BroadcastClient*> bcclients;
111 //tolua_begin
114 //tolua_end
116 #endif