TuioServer.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //using Ventuz.OSC;
  7. using OSC.NET;
  8. //using System.Runtime.InteropServices;
  9. using bbiwarg.Utility;
  10. namespace TUIO
  11. {
  12. class TuioServer
  13. {
  14. const int MAX_PACKET_SIZE = 65535 - 8;
  15. private OSCTransmitter transmitter = null;
  16. private String host = "127.0.0.1";
  17. private int port = 3333;
  18. private long sessionID = 0;
  19. private TuioTime currentFrameTime;
  20. private List<TuioCursor> cursorList;
  21. private List<TuioCursor> updatedCursorList;
  22. public TuioServer() { init(); }
  23. public TuioServer(int port)
  24. {
  25. this.port = port;
  26. init();
  27. }
  28. public TuioServer(String host, int port)
  29. {
  30. this.host = host;
  31. this.port = port;
  32. init();
  33. }
  34. private void init()
  35. {
  36. TuioTime.initSession();
  37. cursorList = new List<TuioCursor>();
  38. transmitter = new OSCTransmitter(host, port);
  39. }
  40. public void close()
  41. {
  42. transmitter.Close();
  43. }
  44. public TuioCursor addTuioCursor(float x, float y)
  45. {
  46. TuioCursor tcur = new TuioCursor(sessionID, cursorList.Count, x, y);
  47. cursorList.Add(tcur);
  48. updatedCursorList.Add(tcur);
  49. sessionID++;
  50. return tcur;
  51. }
  52. public void updateTuioCursor(TuioCursor tcur, float xp, float yp)
  53. {
  54. tcur.update(currentFrameTime, xp, yp);
  55. if (!updatedCursorList.Contains(tcur))
  56. updatedCursorList.Add(tcur);
  57. }
  58. public void updateTuioCursor(long s_id, float xp, float yp)
  59. {
  60. TuioCursor tcur;
  61. for (int i = 0; i < cursorList.Count; i++)
  62. {
  63. tcur = cursorList[i];
  64. if (tcur.getSessionID() == s_id)
  65. {
  66. updateTuioCursor(tcur, xp, yp);
  67. return;
  68. }
  69. }
  70. }
  71. public void removeTuioCursor(TuioCursor tcur)
  72. {
  73. cursorList.Remove(tcur);
  74. }
  75. public void initFrame()
  76. {
  77. currentFrameTime = TuioTime.getSessionTime();
  78. updatedCursorList = new List<TuioCursor>();
  79. }
  80. public void commitFrame()
  81. {
  82. sendMessage(updatedCursorList);
  83. }
  84. public void sendFullMessages()
  85. {
  86. sendMessage(cursorList);
  87. }
  88. private void sendMessage(List<TuioCursor> list)
  89. {
  90. OSCBundle packet = new OSCBundle();
  91. addAliveMessagesToBundle(packet);
  92. OSCMessage currentMessage;
  93. TuioCursor cursor;
  94. for (int i = 0; i < list.Count; i++)
  95. {
  96. cursor = cursorList[i];
  97. currentMessage = new OSCMessage("/tuio/2Dcur");
  98. currentMessage.Append("set");
  99. currentMessage.Append((Int32)cursor.getSessionID());
  100. currentMessage.Append(cursor.getX());
  101. currentMessage.Append(cursor.getY());
  102. currentMessage.Append(cursor.getXSpeed());
  103. currentMessage.Append(cursor.getYSpeed());
  104. currentMessage.Append(cursor.getMotionAccel());
  105. /*if (Marshal.SizeOf(packet) + Marshal.SizeOf(currentOscElement) >= MAX_PACKET_SIZE)
  106. {
  107. packet.AddElement(new OscElement("/tuio/2Dcur", new Object[] { "fseq", -1 }));
  108. udpwriter.Send(packet);
  109. packet = new OscBundle();
  110. addAliveMessagesToBundle(packet);
  111. }*/
  112. packet.Append(currentMessage);
  113. Logger.log("Send Cursor "+cursor.getSessionID()+" at time "+TuioTime.getSystemTime().ToString(), LogSubject.TUIOServer);
  114. }
  115. currentMessage = new OSCMessage("/tuio/2Dcur");
  116. currentMessage.Append("fseq");
  117. currentMessage.Append(-1);
  118. packet.Append(currentMessage);
  119. transmitter.Send(packet);
  120. }
  121. private void addAliveMessagesToBundle(OSCBundle packet)
  122. {
  123. OSCMessage mssg = new OSCMessage("/tuio/2Dcur");
  124. mssg.Append("alive");
  125. for (int i = 0; i < cursorList.Count; i++)
  126. {
  127. mssg.Append((Int32)cursorList[i].getSessionID());
  128. }
  129. packet.Append(mssg);
  130. }
  131. public List<TuioCursor> getTuioCursor()
  132. {
  133. return cursorList;
  134. }
  135. public TuioCursor getTuioCursor(long s_id)
  136. {
  137. for (int i = 0; i < cursorList.Count; i++)
  138. {
  139. if (cursorList[i].getSessionID() == s_id)
  140. return cursorList[i];
  141. }
  142. return null;
  143. }
  144. }
  145. }