-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTuioServer.cs
186 lines (146 loc) · 5.16 KB
/
TuioServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using Bespoke.Common.Osc;
namespace Tuio
{
/// <summary>
/// Simple, still uncomplete implementation of a TUIO server in C#.
///
/// Current shortcomings:
/// Object support missing.
/// Does not implement frame times.
/// Only supports external TUIO cursors.
/// Allways commits all cursors.
///
/// (c) 2010 by Dominik Schmidt ([email protected])
/// </summary>
public class TuioServer
{
#region constants
private const string _cursorAddressPattern = "/tuio/2Dcur";
private const string _objectAddressPattern = "/tuio/2Dobj";
#endregion
#region fields
private IPEndPoint _ipEndPoint;
private Dictionary<int, TuioCursor> _cursors;
private int _currentFrame;
#endregion
#region constructors
/// <summary>
/// Creates a new server with and endpoint at localhost, port 3333.
/// </summary>
public TuioServer() : this("127.0.0.1", 3333) { }
/// <summary>
/// Creates a new server.
/// </summary>
/// <param name="host">Endpoint host</param>
/// <param name="port">Endpoint port</param>
public TuioServer(string host, int port)
{
_cursors = new Dictionary<int, TuioCursor>();
_ipEndPoint = new IPEndPoint(IPAddress.Parse(host), port);
_currentFrame = 0;
}
#endregion
#region frame related methods
/// <summary>
/// Initialized a new frame and increases the frame counter.
/// </summary>
public void InitFrame()
{
_currentFrame++;
}
/// <summary>
/// Commits the current frame.
/// </summary>
public void CommitFrame()
{
GetFrameBundle().Send(_ipEndPoint);
}
#endregion
#region cursor related methods
/// <summary>
/// Adds a TUIO cursor. A new id, not used before, must be provided.
/// </summary>
/// <param name="id">New id</param>
/// <param name="location">Location</param>
public void AddTuioCursor(int id, PointF location)
{
lock(_cursors)
if(!_cursors.ContainsKey(id))
_cursors.Add(id, new TuioCursor(id, location));
}
/// <summary>
/// Updates a TUIO cursor. An id of an existing cursor must be provided.
/// </summary>
/// <param name="id">Id</param>
/// <param name="location">Location</param>
public void UpdateTuioCursor(int id, PointF location)
{
TuioCursor cursor;
if(_cursors.TryGetValue(id, out cursor))
cursor.Location = location;
}
/// <summary>
/// Deletes a TUIO cursor. An id of an existing cursor must be provided.
/// </summary>
/// <param name="id">Id</param>
public void DeleteTuioCursor(int id)
{
lock (_cursors)
_cursors.Remove(id);
}
#endregion
#region osc message assembly
private OscBundle GetFrameBundle()
{
OscBundle bundle = new OscBundle(_ipEndPoint);
bundle.Append(GetAliveMessage());
foreach (OscMessage msg in GetCursorMessages())
bundle.Append(msg);
bundle.Append(GetSequenceMessage());
return bundle;
}
private OscMessage GetAliveMessage()
{
OscMessage msg = new OscMessage(_ipEndPoint, _cursorAddressPattern);
msg.Append("alive");
lock (_cursors)
foreach (TuioCursor cursor in _cursors.Values)
msg.Append((Int32)cursor.Id);
return msg;
}
private OscMessage GetSequenceMessage()
{
OscMessage msg = new OscMessage(_ipEndPoint, _cursorAddressPattern);
msg.Append("fseq");
msg.Append((Int32)_currentFrame);
return msg;
}
private OscMessage GetCursorMessage(TuioCursor cursor)
{
OscMessage msg = new OscMessage(_ipEndPoint, _cursorAddressPattern);
msg.Append("set");
msg.Append((Int32)cursor.Id);
msg.Append(cursor.Location.X);
msg.Append(cursor.Location.Y);
msg.Append(cursor.Speed.X);
msg.Append(cursor.Speed.Y);
msg.Append(cursor.MotionAcceleration);
return msg;
}
private IEnumerable<OscMessage> GetCursorMessages()
{
List<OscMessage> msgs = new List<OscMessage>();
lock (_cursors)
foreach (TuioCursor cursor in _cursors.Values)
msgs.Add(GetCursorMessage(cursor));
return msgs.AsEnumerable();
}
#endregion
}
}