-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSCTestServer.cs
281 lines (249 loc) · 11.9 KB
/
OSCTestServer.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
Copyright (c) 2023 teiron
Released under the MIT license
https://github.com/teiron3/OSCTestServer/blob/main/LICENSE
*/
using System;
using System.Windows;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;
using System.Net;
using System.Net.Sockets;
class Program
{
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
class Form1:Form{
private Button ButtonStart;
private Button ButtonStop;
private Label LabelSendport;
private TextBox TextboxSendport;
private Label LabelDestport;
private TextBox TextboxDestport;
private Label LabelOutsideIP;
private TextBox TextboxOutsideIP;
private Label LabelThisAppSendPort;
private TextBox TextboxThisAppSendPort;
private Label LabelRemoveloopback;
private CheckBox CheckboxRemoveloopback;
private Label LabelRemoveoutside;
private CheckBox CheckboxRemoveoutside;
private UdpClient UdpSendSocket;
private UdpClient UdpDestSocket;
private IPEndPoint VirtualCastSendIP;
private IPEndPoint VirtualCastDestIP;
private IPEndPoint OutsideSendIP;
private IPEndPoint ThisAppSendIP;
public Form1(){
this.Size = new Size(460, 300);
this.Location = new Point(100, 100);
this.Text = "OSC転送アプリ";
///
int lableX = 10;
int textboxx = 220;
int locationy = 50;
int addy = 25;
UdpSendSocket = new UdpClient();
Size labelSize = new Size(200, 20);
Size labeloutsideSize = new Size(70, 20);
Size textboxSize = new Size(200, 20);
LabelThisAppSendPort = new Label();
LabelThisAppSendPort.Parent = this;
LabelThisAppSendPort.Size = labelSize;
LabelThisAppSendPort.Location = new Point(lableX, locationy);
LabelThisAppSendPort.Text = "このアプリからの送信ポート";
TextboxThisAppSendPort = new TextBox();
TextboxThisAppSendPort.Parent = this;
TextboxThisAppSendPort.Size = textboxSize;
TextboxThisAppSendPort.Location = new Point(textboxx, locationy);
TextboxThisAppSendPort.Text = "18101";
locationy += addy;
LabelSendport = new Label();
LabelSendport.Parent = this;
LabelSendport.Size = labelSize;
LabelSendport.Text = "バーチャルキャストで設定した送信ポート";
LabelSendport.Location = new Point(lableX, locationy);
TextboxSendport = new TextBox();
TextboxSendport.Parent = this;
TextboxSendport.Size = textboxSize;
TextboxSendport.Location = new Point(textboxx, locationy);
TextboxSendport.Text = "18100";
locationy += addy;
LabelDestport = new Label();
LabelDestport.Parent = this;
LabelDestport.Size = labelSize;
LabelDestport.Text = "バーチャルキャストで設定した受信ポート";
LabelDestport.Location = new Point(lableX, locationy);
TextboxDestport = new TextBox();
TextboxDestport.Parent = this;
TextboxDestport.Size = textboxSize;
TextboxDestport.Location = new Point(textboxx, locationy);
TextboxDestport.Text = "19100";
locationy += addy;
locationy += addy;
LabelOutsideIP = new Label();
LabelOutsideIP.Parent = this;
LabelOutsideIP.Size = labelSize;
LabelOutsideIP.Location = new Point(lableX, locationy);
LabelOutsideIP.Text = "送信先外部IPアドレス";
TextboxOutsideIP = new TextBox();
TextboxOutsideIP.Parent = this;
TextboxOutsideIP.Size = textboxSize;
TextboxOutsideIP.Location = new Point(textboxx, locationy);
TextboxOutsideIP.Text = "127.0.0.1";
locationy += addy;
locationy += addy;
LabelRemoveloopback = new Label();
LabelRemoveloopback.Parent = this;
LabelRemoveloopback.Size = labelSize;
LabelRemoveloopback.Location = new Point(lableX, locationy);
LabelRemoveloopback.Text = "アドレスから/loopbackを除く";
CheckboxRemoveloopback = new CheckBox();
CheckboxRemoveloopback.Parent = this;
CheckboxRemoveloopback.Location = new Point(textboxx, locationy);
CheckboxRemoveloopback.Checked = true;
locationy += addy;
LabelRemoveoutside = new Label();
LabelRemoveoutside.Parent = this;
LabelRemoveoutside.Size = labelSize;
LabelRemoveoutside.Location = new Point(lableX, locationy);
LabelRemoveoutside.Text = "アドレスから/outsideを除く";
CheckboxRemoveoutside = new CheckBox();
CheckboxRemoveoutside.Parent = this;
CheckboxRemoveoutside.Location = new Point(textboxx, locationy);
CheckboxRemoveoutside.Checked = true;
ButtonStart = new Button();
ButtonStart.Parent = this;
ButtonStart.Location = new Point(10, 10);
ButtonStart.Size = new Size(100, 30);
ButtonStart.Text = "start";
ButtonStart.Click += (obj, e) =>{
Button btn = (Button) obj;
try{
VirtualCastSendIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Int32.Parse(TextboxSendport.Text));
VirtualCastDestIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Int32.Parse(TextboxDestport.Text));
OutsideSendIP = new IPEndPoint(IPAddress.Parse(TextboxOutsideIP.Text), Int32.Parse(TextboxThisAppSendPort.Text));
ThisAppSendIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Int32.Parse(TextboxThisAppSendPort.Text));
UdpDestSocket = new UdpClient(VirtualCastSendIP);
}catch(Exception ex){
MessageBox.Show("どれかのアドレスまたはポートの設定が間違っています。\n(UDPの受信ポートが塞がってる可能性もあります。)");
return;
}
UdpDestSocket.BeginReceive(new AsyncCallback(ReceiveCallback), null);
btn.Enabled = false;
};
ButtonStop = new Button();
ButtonStop.Parent = this;
ButtonStop.Location = new Point(130, 10);
ButtonStop.Size = new Size(100, 30);
ButtonStop.Text = "stop";
ButtonStop.Click += (obj, e) =>{
UdpSendSocket.Send(new byte[] {0x71} , 1, VirtualCastSendIP);
if(!ButtonStart.Enabled){
ButtonStart.Enabled = true;
}
};
}
private void ReceiveCallback(IAsyncResult ar){
IPEndPoint refIP = null;
byte[] receiveBytes = UdpDestSocket.EndReceive(ar, ref refIP);
//受信の終了
//最初のデータが q の場合に受信を終了します
if(receiveBytes[0] == 0x71){
Console.WriteLine("stop process");
UdpDestSocket.Close();
return;
}
//OSCのアドレスの最初に /loopback/ (/loopback だけだとダメ)が入っている場合の処理
//バーチャルキャストの受信ポートにデータをそのまま戻します
byte[] loopbackarray = System.Text.Encoding.ASCII.GetBytes("/loopback/");
for(int count = 0; count < loopbackarray.Length; count++){
if(receiveBytes[count] != loopbackarray[count])break;
if(count == loopbackarray.Length - 1){
if(CheckboxRemoveloopback.Checked){
byte[] sendBytes = removeaddress(receiveBytes, "/loopback");
UdpSendSocket.Send(sendBytes, sendBytes.Length, VirtualCastDestIP);
}else{
UdpSendSocket.Send(receiveBytes, receiveBytes.Length, VirtualCastDestIP);
}
UdpDestSocket.BeginReceive(new AsyncCallback(ReceiveCallback), null);
return;
}
}
//OSCのアドレスの最初に /outside/ (/outside だけだとダメ)が入っている場合の処理
//外部のIPアドレスにデータをそのまま送信します
byte[] outsidearray = System.Text.Encoding.ASCII.GetBytes("/outside/");
for(int count = 0; count < outsidearray.Length; count++){
if(receiveBytes[count] != outsidearray[count])break;
if(count == outsidearray.Length - 1){
if(CheckboxRemoveoutside.Checked){
byte[] sendBytes = removeaddress(receiveBytes, "/outside");
UdpSendSocket.Send(sendBytes, sendBytes.Length, OutsideSendIP);
}else{
UdpSendSocket.Send(receiveBytes, receiveBytes.Length, OutsideSendIP);
}
UdpDestSocket.BeginReceive(new AsyncCallback(ReceiveCallback), null);
return;
}
}
//OSCのアドレスが /sendkeys の場合の処理
//string1個のみ場合だけSendkeysでアクティブウィンドウにキーを送信します
byte[] sendkeysarray = System.Text.Encoding.ASCII.GetBytes("/sendkeys");
for(int count = 0; count < sendkeysarray.Length; count++){
if(receiveBytes[count] != sendkeysarray[count])break;
if(count == sendkeysarray.Length - 1 && receiveBytes[count + 1] == 0){
int subcount = count + 1;
while((char)receiveBytes[++subcount] != ','){}
if((char)receiveBytes[subcount + 1] != 's' && receiveBytes[subcount + 2] != 0)break;
subcount += 2;
while(receiveBytes[++subcount] == 0){if(subcount + 1 == receiveBytes.Length)break;}
if(subcount + 1 == receiveBytes.Length)break;
int stringstartpoint = subcount;
while(receiveBytes[++subcount] != 0){if(subcount + 1 == receiveBytes.Length)break;}
int stringlength = subcount - stringstartpoint;
SendKeys.SendWait(System.Text.Encoding.ASCII.GetString((new ArraySegment<byte>(receiveBytes, stringstartpoint, stringlength)).ToArray()));
UdpDestSocket.BeginReceive(new AsyncCallback(ReceiveCallback), null);
return;
}
}
//上記以外のものはそのまま送信
UdpSendSocket.Send(receiveBytes, receiveBytes.Length, ThisAppSendIP);
UdpDestSocket.BeginReceive(new AsyncCallback(ReceiveCallback), null);
}
///OSCのアドレスから不要な部分を取り除く処理
private byte[] removeaddress(byte[] bytes, string removestring){
int bytesstartpoint = removestring.Length;
int bytesaddressendpoint = bytesstartpoint;
int addresslenght;
int padzerolength;
int datastartpoint;
byte[] returnbytes;
while(bytes[++bytesaddressendpoint] != 0);
--bytesaddressendpoint;
addresslenght = bytesaddressendpoint - bytesstartpoint + 1;
padzerolength = addresslenght % 4;
padzerolength = (padzerolength == 0) ? 4 :4 - padzerolength;
datastartpoint = bytesaddressendpoint + 1;
while(bytes[++datastartpoint] != (byte)',');
returnbytes = new byte[addresslenght + padzerolength + (bytes.Length - datastartpoint)];
int writepoint = 0;
while(writepoint + bytesstartpoint <= bytesaddressendpoint){
returnbytes[writepoint] = bytes[writepoint + bytesstartpoint];
writepoint++;
}
for(int writecount = 0; writecount < padzerolength; writecount++){
returnbytes[writepoint] = 0;
writepoint++;
}
for(int writecount = 0; writecount + datastartpoint < bytes.Length; writecount++){
returnbytes[writepoint] = bytes[writecount + datastartpoint];
writepoint++;
}
return returnbytes;
}
}