Repository navigation
Expand file tree
/
Copy pathRTSPClientBase.cs
More file actions
427 lines (353 loc) · 13.5 KB
/
Copy pathRTSPClientBase.cs
File metadata and controls
427 lines (353 loc) · 13.5 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace RTSPLibrary
{
public partial class RTSPClientBase : IDisposable
{
private System.Net.Sockets.TcpClient iTCP;
private int iCSeq = 1;
private BufferClass BC = new BufferClass();
private bool iCvtBase64Send = false;
private DateTime iMagicLastSendTime;
private int iRecvServerChannel;
private int iLastMagicDataLength;
public const string DESCRIBE_accept_type_sdp = "application/sdp";
public const string DESCRIBE_accept_type_rtsl = "application/rtsl";
public const string DESCRIBE_accept_type_mheg = "application/mheg";
public const string Method_DESCRIBE = "DESCRIBE";
public const string Method_SETUP = "SETUP";
public const string Method_PLAY = "PLAY";
public const string Method_OPTIONS = "OPTIONS";
public const string Method_ANNOUNCE = "ANNOUNCE";
public const string Method_PAUSE = "PAUSE";
public const string Method_TEARDOWN = "TEARDOWN";
public const string Method_GET_PARAMETER = "GET_PARAMETER";
public const string Method_SET_PARAMETER = "SET_PARAMETER";
public const string Method_REDIRECT = "REDIRECT";
public const string Method_RECORD = "RECORD";
public delegate void DisconnectEventHandler(RTSPClientBase sender);
public event DisconnectEventHandler Disconnect;
private HttpSpliter HS = new HttpSpliter();
public bool CvtBase64Send
{
get { return iCvtBase64Send; }
set { iCvtBase64Send = value; }
}
public System.Net.Sockets.TcpClient GetSourceTCP
{
get { return iTCP; }
}
public byte[] GetAndFlushBuffer()
{
byte[] RetValue = null;
lock (BC.iBuffer)
{
if (BC.iBuffer.Count > 0)
{
RetValue = BC.iBuffer.ToArray();
BC.iBuffer.Clear();
}
}
return RetValue;
}
public RTSPRequest CreateRequest(string Method, string URI)
{
RTSPRequest RetValue = null;
RetValue = new RTSPRequest(Method, URI, GetCSeq());
return RetValue;
}
public void CloseServer()
{
if (iTCP != null)
{
System.Net.Sockets.NetworkStream netStream = null;
System.Net.IPEndPoint IPEP = null;
string RemoteIP = null;
int RemotePort = 0;
try { IPEP = (System.Net.IPEndPoint)iTCP.Client.RemoteEndPoint; }
catch (Exception ex) { }
if (IPEP != null)
{
RemoteIP = IPEP.Address.ToString();
RemotePort = IPEP.Port;
}
if (iTCP.Connected)
{
try { netStream = iTCP.GetStream(); }
catch (Exception ex) { }
if (netStream != null)
{
try { netStream.Close(); }
catch (Exception ex) { }
}
}
try { iTCP.Close(); }
catch (Exception ex) { }
Console.WriteLine("RTSPClientBase Socket Close completed [" + RemoteIP + ":" + RemotePort + "]");
iTCP = null;
}
}
public void ConnectServer(string RemoteAddr, int RemotePort)
{
if (iTCP != null)
CloseServer();
iTCP = new System.Net.Sockets.TcpClient();
iTCP.NoDelay = true;
try { iTCP.Connect(RemoteAddr, RemotePort); }
catch (Exception ex)
{
try { iTCP.Close(); }
catch (Exception ex2) { }
iTCP = null;
throw ex;
}
}
public RTSPResponse SendAndWait(RTSPRequest RTSPReq)
{
bool SendSuccess = false;
RTSPResponse Resp = null;
SendSuccess = WriteRequest(RTSPReq);
if (SendSuccess)
Resp = WaitResponse(10000);
return Resp;
}
public bool WriteRequest(RTSPRequest Req)
{
byte[] DataArray = null;
List<byte> ReadBufferList = new List<byte>();
bool SendSuccess = false;
DataArray = Req.ToByteArray(iCvtBase64Send);
try
{
if (iTCP != null)
{
if (iTCP.Connected)
{
iTCP.GetStream().Write(DataArray, 0, DataArray.Length);
SendSuccess = true;
}
}
}
catch (Exception ex)
{
}
return SendSuccess;
}
public RTSPResponse WaitResponse(int WaitTimeout)
{
byte[] DataArray = null;
byte[] ReadBuffer = null;
List<byte> ReadBufferList = new List<byte>();
int ReadCount;
bool SendSuccess = false;
RTSPResponse Resp = null;
bool MustExit = false;
ReadBuffer = (byte[])Array.CreateInstance(typeof(byte), 1024);
iTCP.ReceiveTimeout = WaitTimeout;
while (true)
{
ReadCount = 0;
try
{
if (iTCP != null)
{
if (iTCP.Connected)
ReadCount = iTCP.GetStream().Read(ReadBuffer, 0, ReadBuffer.Length);
}
}
catch (Exception ex)
{
BC.Clear();
Debug.WriteLine("TimeOut:" + ex.Message);
}
if (ReadCount > 0)
{
DataArray = (byte[])Array.CreateInstance(typeof(byte), ReadCount);
Array.Copy(ReadBuffer, 0, DataArray, 0, DataArray.Length);
BC.iBuffer.AddRange(DataArray);
}
else
{
MustExit = true;
}
Resp = GetResponse();
if ((Resp != null) || (MustExit))
break;
}
return Resp;
}
private RTSPResponse GetResponse()
{
RTSPResponse RetValue = null;
int sIndex = 0;
int fIndex;
int LoopIndex1;
int LoopIndex2;
for (LoopIndex1 = 1; LoopIndex1 <= 100; LoopIndex1++)
{
if (BC.iHeaderCapture == false)
{
for (LoopIndex2 = 1; LoopIndex2 <= 100; LoopIndex2++)
{
fIndex = BC.iBuffer.IndexOf(13, sIndex);
if (fIndex != -1)
{
if (BC.iBuffer.Count >= (fIndex + 4))
{
if ((BC.iBuffer[fIndex] == 13) &&
(BC.iBuffer[fIndex + 1] == 10) &&
(BC.iBuffer[fIndex + 2] == 13) &&
(BC.iBuffer[fIndex + 3] == 10))
{
byte[] HeaderBytes = null;
string HeaderContent;
string[] HeaderStringList = null;
HeaderBytes = (byte[])Array.CreateInstance(typeof(byte), fIndex);
BC.iBuffer.CopyTo(0, HeaderBytes, 0, HeaderBytes.Length);
HeaderContent = Encoding.UTF8.GetString(HeaderBytes);
HeaderStringList = HS.SplitContent(HeaderContent);
if (HeaderStringList.Length > 0)
{
int TmpIndex;
for (int I = 1; I < HeaderStringList.Length; I++)
{
string EachStr = HeaderStringList[I];
if (string.IsNullOrWhiteSpace(EachStr.Trim()) == false)
{
string TmpCmd;
string TmpValue;
TmpIndex = EachStr.IndexOf(":");
if (TmpIndex != -1)
{
TmpCmd = EachStr.Substring(0, TmpIndex).Trim();
TmpValue = EachStr.Substring(TmpIndex + 1).Trim();
if (TmpCmd.ToUpper() == "Content-Length".ToUpper())
{
BC.iContentLength = Convert.ToInt32(TmpValue);
break;
}
}
}
}
BC.iHeaderCapture = true;
BC.iHeaderLength = fIndex + 4;
break;
}
else
// header = 0
break;
}
else
sIndex = fIndex + 1;
}
else
break;
}
else
break;
}
if (LoopIndex2 >= 100)
{
BC.iBuffer.Clear();
BC.Reset();
}
if (BC.iHeaderCapture == false)
break;
}
else if (BC.iContentLength != -1)
{
if (BC.iBuffer.Count >= BC.iHeaderLength + BC.iContentLength)
{
byte[] Content = null;
Content = (byte[])Array.CreateInstance(typeof(byte), BC.iHeaderLength + BC.iContentLength);
BC.iBuffer.CopyTo(0, Content, 0, Content.Length);
BC.iBuffer.RemoveRange(0, Content.Length);
RetValue = new RTSPResponse();
RetValue.Parsing(Content);
BC.Reset();
break;
}
else
break;
}
else
{
// 未設定 Content-Length
// 代表沒有內容, Header 收完即可
// Dim Header() As Byte
// Header = Array.CreateInstance(GetType(Byte), BC.iHeaderLength)
// BC.iBuffer.CopyTo(0, Header, 0, Header.Length)
// BC.iBuffer.RemoveRange(0, Header.Length)
// RetValue = New RTSPResponse
// RetValue.Parsing(Header)
RetValue = new RTSPResponse();
RetValue.Parsing(BC.iBuffer.ToArray());
BC.iBuffer.Clear();
BC.Reset();
break;
}
}
if (LoopIndex1 >= 100)
{
BC.iBuffer.Clear();
BC.Reset();
}
return RetValue;
}
public List<byte> GetBuffer()
{
return BC.iBuffer;
}
private int GetCSeq()
{
return System.Threading.Interlocked.Increment(ref iCSeq);
}
public partial class BufferClass
{
public List<byte> iBuffer = new List<byte>();
public bool iHeaderCapture = false;
public int iHeaderLength = -1;
public int iContentLength = -1;
public void Reset()
{
iHeaderCapture = false;
iContentLength = -1;
iHeaderLength = -1;
}
public void Clear()
{
Reset();
iBuffer.Clear();
}
}
#region Dispose
private bool disposedValue = false;
// IDisposable
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
if (iTCP != null)
{
try { iTCP.Close(); }
catch (Exception ex) { }
iTCP = null;
}
}
}
disposedValue = true;
}
#region IDisposable Support
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#endregion
}
}