123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //using Ventuz.OSC;
- using OSC.NET;
- //using System.Runtime.InteropServices;
- using bbiwarg.Utility;
- namespace TUIO
- {
- class TuioServer
- {
- const int MAX_PACKET_SIZE = 65535 - 8;
- private OSCTransmitter transmitter = null;
- private String host = "127.0.0.1";
- private int port = 3333;
- private long sessionID = 0;
- private TuioTime currentFrameTime;
- private List<TuioCursor> cursorList;
- private List<TuioCursor> updatedCursorList;
- public TuioServer() { init(); }
- public TuioServer(int port)
- {
- this.port = port;
- init();
- }
- public TuioServer(String host, int port)
- {
- this.host = host;
- this.port = port;
- init();
- }
- private void init()
- {
- TuioTime.initSession();
- cursorList = new List<TuioCursor>();
- transmitter = new OSCTransmitter(host, port);
- }
- public void close()
- {
- transmitter.Close();
- }
- public TuioCursor addTuioCursor(float x, float y)
- {
- TuioCursor tcur = new TuioCursor(sessionID, cursorList.Count, x, y);
- cursorList.Add(tcur);
- updatedCursorList.Add(tcur);
- sessionID++;
- return tcur;
- }
- public void updateTuioCursor(TuioCursor tcur, float xp, float yp)
- {
- tcur.update(currentFrameTime, xp, yp);
- if (!updatedCursorList.Contains(tcur))
- updatedCursorList.Add(tcur);
- }
- public void updateTuioCursor(long s_id, float xp, float yp)
- {
- TuioCursor tcur;
- for (int i = 0; i < cursorList.Count; i++)
- {
- tcur = cursorList[i];
- if (tcur.getSessionID() == s_id)
- {
- updateTuioCursor(tcur, xp, yp);
- return;
- }
- }
- }
- public void removeTuioCursor(TuioCursor tcur)
- {
- cursorList.Remove(tcur);
- }
- public void initFrame()
- {
- currentFrameTime = TuioTime.getSessionTime();
- updatedCursorList = new List<TuioCursor>();
- }
- public void commitFrame()
- {
- sendMessage(updatedCursorList);
- }
- public void sendFullMessages()
- {
- sendMessage(cursorList);
- }
- private void sendMessage(List<TuioCursor> list)
- {
- OSCBundle packet = new OSCBundle();
- addAliveMessagesToBundle(packet);
- OSCMessage currentMessage;
- TuioCursor cursor;
- for (int i = 0; i < list.Count; i++)
- {
- cursor = cursorList[i];
- currentMessage = new OSCMessage("/tuio/2Dcur");
- currentMessage.Append("set");
- currentMessage.Append((Int32)cursor.getSessionID());
- currentMessage.Append(cursor.getX());
- currentMessage.Append(cursor.getY());
- currentMessage.Append(cursor.getXSpeed());
- currentMessage.Append(cursor.getYSpeed());
- currentMessage.Append(cursor.getMotionAccel());
- /*if (Marshal.SizeOf(packet) + Marshal.SizeOf(currentOscElement) >= MAX_PACKET_SIZE)
- {
- packet.AddElement(new OscElement("/tuio/2Dcur", new Object[] { "fseq", -1 }));
- udpwriter.Send(packet);
- packet = new OscBundle();
- addAliveMessagesToBundle(packet);
- }*/
- packet.Append(currentMessage);
- Logger.log("Send Cursor "+cursor.getSessionID()+" at time "+TuioTime.getSystemTime().ToString(), LogSubject.TUIOServer);
- }
- currentMessage = new OSCMessage("/tuio/2Dcur");
- currentMessage.Append("fseq");
- currentMessage.Append(-1);
- packet.Append(currentMessage);
- transmitter.Send(packet);
- }
- private void addAliveMessagesToBundle(OSCBundle packet)
- {
- OSCMessage mssg = new OSCMessage("/tuio/2Dcur");
- mssg.Append("alive");
- for (int i = 0; i < cursorList.Count; i++)
- {
- mssg.Append((Int32)cursorList[i].getSessionID());
- }
- packet.Append(mssg);
- }
- public List<TuioCursor> getTuioCursor()
- {
- return cursorList;
- }
- public TuioCursor getTuioCursor(long s_id)
- {
- for (int i = 0; i < cursorList.Count; i++)
- {
- if (cursorList[i].getSessionID() == s_id)
- return cursorList[i];
- }
- return null;
- }
- }
- }
|