Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.older/src/lib/network/message_manager.h @ 10616

Last change on this file since 10616 was 9008, checked in by bensch, 19 years ago

orxonox/trunk: merged the network bak to the trunk
merged with command:
svn merge -r8804:HEAD https://svn.orxonox.net/orxonox/branches/multi_player_map .

conflicts all resolved in favour of the branche

File size: 3.1 KB
Line 
1/*!
2 * @file message_manager.h
3 * @brief Definition of MessageManager
4*/
5
6#ifndef _MESSAGE_MANAGER_H
7#define _MESSAGE_MANAGER_H
8
9#include "synchronizeable.h"
10
11#include <map>
12#include <list>
13
14/*
15  Protocol:
16    int nacks
17    int acks[1..nacks]
18    int nmsg
19    (
20      int length
21      int number
22      int MessageId
23      byte * data
24    )[1..nmsg]
25*/
26
27
28enum MessageId
29{
30  TESTMESSAGEID = 1,
31  MSGID_DELETESYNCHRONIZEABLE,
32  MSGID_PREFEREDTEAM,
33  MSGID_CHANGENICKNAME,
34  MSGID_CHATMESSAGE,
35  MSGID_RESPAWN
36};
37
38typedef bool (*MessageCallback)( MessageId messageId, byte * data, int dataLength, void * someData, int userId );
39
40enum RecieverType
41{
42  RT_ALL_NOT_ME = 1,   //!< message is sent to all users
43  RT_ALL_ME,           //!< message is sent to all users
44  RT_USER,             //!< message is only sent to reciever
45  RT_NOT_USER,         //!< message is sent to all but reciever
46  RT_SERVER            //!< message is sent to server only
47};
48
49//TODO implement priority handling
50enum MessagePriority
51{
52  MP_HIGHBANDWIDTH = 1,  //!< fast and reliable but uses a lot of bandwidth
53  MP_LOWBANDWIDTH,       //!< may take a long time to arrive. reliable
54  MP_UNRELIABLE          //!< unreliable. low bandwidth
55};
56
57struct NetworkMessage
58{
59  MessageId        messageId;
60  byte *           data;
61  int              length;
62  int              number;
63  MessagePriority  priority;
64};
65
66struct MessageUserQueue
67{
68  std::list<NetworkMessage> messages;
69  std::list<int>            toAck;
70  std::list<int>            recievedMessages;
71};
72
73typedef std::map<int,MessageUserQueue> MessageQueue;
74
75struct MessageHandler
76{
77  MessageCallback cb;
78  MessageId       messageId;
79  void *          someData;
80};
81
82typedef std::map<MessageId,MessageHandler> MessageHandlerMap;
83
84//! A class for sending messages over network
85class MessageManager : public Synchronizeable {
86 protected:
87   MessageManager();
88 public:
89   inline static MessageManager * getInstance(){ if (!singletonRef) singletonRef = new MessageManager();  return singletonRef; }
90   
91   virtual ~MessageManager();
92   
93   bool registerMessageHandler( MessageId messageId, MessageCallback cb, void * someData );
94   
95   void sendMessage( MessageId messageId, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority );
96
97   virtual int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH );
98   virtual int setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId );
99   virtual void cleanUpUser( int userId );
100   virtual void handleSentState( int userId, int stateId, int fromStateId ){}
101   virtual void handleRecvState( int userId, int stateId, int fromStateId ){}
102   
103   void initUser( int userId );
104
105
106 private:
107   static MessageManager *   singletonRef;
108   MessageQueue              messageQueue;           //!< stores messages to send
109   MessageHandlerMap         messageHandlerMap;      //!< contains handlers for messages
110
111   int                       newNumber;              //!< used to create unique message numbers
112   std::list<NetworkMessage> incomingMessageBuffer; 
113
114};
115
116#endif /* _PROTO_CLASS_H */
Note: See TracBrowser for help on using the repository browser.