Merge branch 'master' of http://www-dev.cockos.com/wdl/WDL into updatewdl
[wdl/wdl-ol.git] / WDL / shm_msgreply.h
blob70234f65d15a06085eb2301f90e827d36b8f3516
1 #ifndef _WDL_SHM_MSGREPLY_
2 #define _WDL_SHM_MSGREPLY_
4 #include <time.h>
5 #include "shm_connection.h"
6 #include "mutex.h"
9 /*
10 4 byte message type
12 4 byte unique message ID (0 for no reply needed/wanted)
14 4 byte message length (following)
17 type: 0 reply
18 call specific return data
19 type: all others user defined
22 // type is user defined, however type=0 is reserved for reply
23 class SHM_MsgReplyConnection
26 public:
28 class WaitingMessage
30 public:
31 WaitingMessage() { }
32 ~WaitingMessage() { }
34 WaitingMessage *_next;
36 int m_msgid;
37 int m_msgtype;
39 WDL_HeapBuf m_msgdata;
43 SHM_MsgReplyConnection(int bufsize, int maxqueuesize, bool dir, const char *uniquestr=NULL, int timeout_sec=0, int extra_flags=0);
44 ~SHM_MsgReplyConnection();
46 // be sure to set these, and have OnRecv() Reply() to any nonzero msgID !
47 void *userData;
48 WaitingMessage *(*OnRecv)(SHM_MsgReplyConnection *con, WaitingMessage *msg);
49 bool (*IdleProc)(SHM_MsgReplyConnection *con); // return TRUE to abort (this will set the m_has_had_error to true / kill the connection)
50 // can return NULL To temporarily buffer msg, can return a chain of msgs too to return them to the spare list
52 // run as you wish, Send() will also run internally when waiting for reply
53 // note: the checkForReplyID etc are for INTERNAL USE ONLY :)
55 bool Run(bool runFull=true);
57 // returns <0 if no reply, otherwise lenght of replybuf used
58 // no retbuf = no wait for reply
59 int Send(int type, const void *msg, int msglen,
60 void *replybuf, int maxretbuflen, const int *forceMsgID=NULL,
61 const void *secondchunk=NULL, int secondchunklen=0, // allow sending two blocks as one message (for speed in certain instances)
62 WDL_HeapBuf *hbreplyout=NULL); // if hbreplyout is set it will get the full message (replybuf can be NULL then)
63 void Reply(int msgID, const void *msg, int msglen);
64 void Wait(HANDLE extraEvt=NULL);
66 const char *GetUniqueString() { return m_uniq; }
68 void ReturnSpares(WaitingMessage *msglist);
70 private:
71 bool RunInternal(int checkForReplyID=0, WaitingMessage **replyPtr=0); // nonzero on error
73 char m_uniq[256];
74 WDL_Mutex m_shmmutex;
75 WDL_SHM_Connection *m_shm;
78 WaitingMessage *m_waiting_replies;
79 WaitingMessage *m_spares;
81 int m_lastmsgid;
82 int m_maxqueuesize;
83 bool m_has_had_error;
86 #endif