langpackedit v0.13 -- from 8f9f0878
[wdl.git] / WDL / shm_msgreply.h
blob3863a5e8016588f0aa6ab04dbbcc474c61a19f1d
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 OnRecv2() Reply() to any nonzero msgID !
47 void *userData;
48 WaitingMessage *(*OnRecv2)(SHM_MsgReplyConnection *con, WaitingMessage *msg); // now called while locked!
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 void Lock() { m_shmmutex.Enter(); }
71 void Unlock() { m_shmmutex.Leave(); }
73 private:
74 bool RunInternal(int checkForReplyID=0, WaitingMessage **replyPtr=0); // nonzero on error
76 char m_uniq[256];
77 WDL_Mutex m_shmmutex;
78 WDL_SHM_Connection *m_shm;
81 WaitingMessage *m_waiting_replies;
82 WaitingMessage *m_spares;
84 int m_lastmsgid;
85 int m_maxqueuesize;
86 bool m_has_had_error;
89 #endif