-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_handler.h
74 lines (65 loc) · 2.02 KB
/
message_handler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef MESSAGE_HANDLER_H
#define MESSAGE_HANDLER_H
#include <string>
#include <memory>
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <set>
class MessageHandler
{
public:
/*
* This is the constructor, no default is used
* @param const string ipaddress - the ipaddress to connect the socket to.
* @param const unsigned short - the port number to connect the socket to.
*/
MessageHandler( const std::string ipaddress, const unsigned short port );
// Destructor to handle any threads or pointers
~MessageHandler( void );
/*
* Function get_message is used to retrieve the current events that the
* other player has sent. It uses a set to keep the events unique between
* frames.
*
* @return const set get_message - A unique list of events from player 2
*/
const std::set<unsigned int> get_message( void );
/*
* Function send_message is used to send the events from our world to
* player 2. Again we use a set to include only unique keypresses.
*
* @param const set message - The message to send to player 2
*/
void send_message( const std::set<unsigned int> message );
/*
* Funcion stop_messages is used to signal that the program is done with
* messaging. The thread is looping to handle messages until this function
* is called, which just adjusts the flag accordingly.
*/
void stop_messages( void );
/*
* Function set_blocking changes the blocking state of the socket based
* on the parameter.
*
* @param bool set - the state of the socket blocking
*/
void set_blocking( bool set )
{
_socket.setBlocking( set );
};
private:
std::string _ipaddress;
unsigned short _port;
sf::Packet _packet_send;
sf::Packet _packet_receive;
sf::UdpSocket _socket;
sf::Mutex _send_mutex;
sf::Mutex _receive_mutex;
sf::Thread* _thread; // sfml threads need to be raw pointers
bool _quit;
std::set<unsigned int> _unique_message;
void handle_messages( void );
void init( void );
};
#endif /*MESSAGE_HANDLER_H*/