You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
3.1KB

  1. #pragma once
  2. #ifdef OSC_ENABLE
  3. #define NUM_SCENES (8)
  4. #include <map>
  5. #include <memory.h>
  6. #include <windows.h>
  7. struct OSCParam
  8. {
  9. char paramType;
  10. int paramLen;
  11. union
  12. {
  13. float f;
  14. double d;
  15. unsigned char midi[4];
  16. int32_t i32;
  17. int64_t i64;
  18. uint64_t time;
  19. const char *s;
  20. };
  21. };
  22. #pragma pack(push,1)
  23. struct __attribute__((aligned(1), packed)) OSCMsg
  24. {
  25. public:
  26. int32_t scene;
  27. char address[40];
  28. float value;
  29. void set(int scn, const char *addr, float v)
  30. {
  31. memset(address, 0, sizeof(address));
  32. strcpy(address, addr);
  33. scene = scn;
  34. value = v;
  35. }
  36. };
  37. #pragma pack(pop)
  38. #include "oscCircBuff.hpp"
  39. class oscCommunicator
  40. {
  41. public:
  42. oscCommunicator(int scene_num)
  43. {
  44. hMapFile = NULL;
  45. pmemory = NULL;
  46. pCommonMemory = NULL;
  47. rdBuffer = wrBuffer = NULL;
  48. m_myScene = scene_num;
  49. }
  50. ~oscCommunicator()
  51. {
  52. deinit();
  53. }
  54. void Clear()
  55. {
  56. if(pmemory != NULL)
  57. {
  58. rdBuffer->Clear();
  59. wrBuffer->Clear();
  60. }
  61. }
  62. bool Connected() { return pmemory != NULL; }
  63. bool Open()
  64. {
  65. if(pmemory == NULL)
  66. {
  67. try
  68. {
  69. init();
  70. } catch(_exception ex)
  71. {
  72. deinit();
  73. }
  74. }
  75. return pmemory != NULL;
  76. }
  77. void Write(OSCMsg *msg)
  78. {
  79. if(checkServer())
  80. {
  81. wrBuffer->WriteChunk(msg);
  82. }
  83. }
  84. bool Read(OSCMsg *msg)
  85. {
  86. if(checkServer())
  87. return rdBuffer->ReadChunk(msg);
  88. return false;
  89. }
  90. private:
  91. void *pmemory;
  92. uint32_t *pCommonMemory;
  93. oscCircBuffer *rdBuffer;
  94. oscCircBuffer *wrBuffer;
  95. HANDLE hMapFile;
  96. int m_myScene;
  97. void init()
  98. {
  99. int b_l = OSCBUFFER_SIZE * sizeof(OSCMsg) + oscCircBuffer::bufferOverhead();
  100. int rawsize = NUM_SCENES * 2 * b_l + sizeof(uint32_t) /*flag SERVER_POS*/;
  101. hMapFile = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE, TRUE, "OSC_mem");
  102. if(hMapFile != NULL)
  103. {
  104. #ifdef DEBUG
  105. info("file mapping opened, buff size = %i", b_l);
  106. #endif
  107. void *p = MapViewOfFile(hMapFile, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, rawsize);
  108. #ifdef DEBUG
  109. if(p == NULL)
  110. info("MapViewOfFile failed");
  111. else
  112. info("MapViewOfFile success, my scene is %i", m_myScene);
  113. #endif
  114. if(p != NULL)
  115. {
  116. int offset = b_l * 2 * (m_myScene - 1);
  117. #ifdef DEBUG
  118. info("wrBuffer[%i] @ offset = %i", m_myScene - 1, START_OF_BUFFER + offset);
  119. info("rdBuffer[%i] @ offset = %i", m_myScene - 1, START_OF_BUFFER + b_l + offset);
  120. #endif
  121. wrBuffer = new oscCircBuffer(b_l, p, START_OF_BUFFER + offset);
  122. rdBuffer = new oscCircBuffer(b_l, p, START_OF_BUFFER + b_l + offset);
  123. }
  124. pmemory = p;
  125. pCommonMemory = (uint32_t *)p;
  126. #ifdef DEBUG
  127. info("Opened OK");
  128. #endif
  129. }
  130. }
  131. bool checkServer()
  132. {
  133. bool rv = false;
  134. if(pCommonMemory != NULL)
  135. {
  136. uint32_t server = *pCommonMemory;
  137. if(server == 0)
  138. {
  139. #ifdef DEBUG
  140. info("*** connection lost ***");
  141. #endif
  142. deinit();
  143. } else
  144. rv = true;
  145. }
  146. return rv;
  147. }
  148. void deinit()
  149. {
  150. void *p = pmemory;
  151. pmemory = NULL;
  152. pCommonMemory = NULL;
  153. if(p != NULL)
  154. UnmapViewOfFile(p);
  155. if(hMapFile != NULL)
  156. CloseHandle(hMapFile);
  157. hMapFile = NULL;
  158. if(rdBuffer != NULL)
  159. {
  160. delete rdBuffer;
  161. rdBuffer = NULL;
  162. }
  163. if(wrBuffer != NULL)
  164. {
  165. delete wrBuffer;
  166. wrBuffer = NULL;
  167. }
  168. }
  169. };
  170. #endif //OSC