Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/message_manager.h @ 8184

Last change on this file since 8184 was 8147, checked in by bensch, 18 years ago

orxonox/trunk: merged the network branche back here
merged with command:
svn merge -r8070:HEAD https://svn.orxonox.net/orxonox/branches/network .
no conflicts

File size: 3.0 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};
34
35typedef bool (*MessageCallback)( MessageId messageId, byte * data, int dataLength, void * someData, int userId );
36
37enum RecieverType
38{
39  RT_ALL_NOT_ME = 1,   //!< message is sent to all users
40  RT_ALL_ME,           //!< message is sent to all users
41  RT_USER,             //!< message is only sent to reciever
42  RT_NOT_USER          //!< message is sent to all but reciever
43};
44
45//TODO implement priority handling
46enum MessagePriority
47{
48  MP_HIGHBANDWIDTH = 1,  //!< fast and reliable but uses a lot of bandwidth
49  MP_LOWBANDWIDTH,       //!< may take a long time to arrive. reliable
50  MP_UNRELIABLE          //!< unreliable. low bandwidth
51};
52
53struct NetworkMessage
54{
55  MessageId        messageId;
56  byte *           data;
57  int              length;
58  int              number;
59  MessagePriority  priority;
60};
61
62struct MessageUserQueue
63{
64  std::list<NetworkMessage> messages;
65  std::list<int>            toAck;
66  std::list<int>            recievedMessages;
67};
68
69typedef std::map<int,MessageUserQueue> MessageQueue;
70
71struct MessageHandler
72{
73  MessageCallback cb;
74  MessageId       messageId;
75  void *          someData;
76};
77
78typedef std::map<MessageId,MessageHandler> MessageHandlerMap;
79
80//! A class for sending messages over network
81class MessageManager : public Synchronizeable {
82 protected:
83   MessageManager();
84 public:
85   inline static MessageManager * getInstance(){ if (!singletonRef) singletonRef = new MessageManager();  return singletonRef; }
86   
87   virtual ~MessageManager();
88   
89   bool registerMessageHandler( MessageId messageId, MessageCallback cb, void * someData );
90   
91   void sendMessage( MessageId messageId, byte * data, int dataLength, RecieverType recieverType, int reciever, MessagePriority messagePriority );
92
93   virtual int getStateDiff( int userId, byte* data, int maxLength, int stateId, int fromStateId, int priorityTH );
94   virtual int setStateDiff( int userId, byte* data, int length, int stateId, int fromStateId );
95   virtual void cleanUpUser( int userId );
96   virtual void handleSentState( int userId, int stateId, int fromStateId ){}
97   virtual void handleRecvState( int userId, int stateId, int fromStateId ){}
98   
99   void initUser( int userId );
100
101
102 private:
103   static MessageManager *   singletonRef;
104   MessageQueue              messageQueue;           //!< stores messages to send
105   MessageHandlerMap         messageHandlerMap;      //!< contains handlers for messages
106
107   int                       newNumber;              //!< used to create unique message numbers
108   std::list<NetworkMessage> incomingMessabeBuffer; 
109
110};
111
112#endif /* _PROTO_CLASS_H */
Note: See TracBrowser for help on using the repository browser.