Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8035 was 7954, checked in by patrick, 19 years ago

trunk: merged the network branche back to trunk.

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