forked from miyu/Dargon.Transport
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDtpNodeSessionFrameProcessor.cs
124 lines (111 loc) · 4.45 KB
/
DtpNodeSessionFrameProcessor.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Logger = Dargon.Transport.__DummyLoggerThisIsHorrible;
namespace Dargon.Transport
{
public class DtpNodeSessionFrameProcessor
{
private readonly DtpNode m_node;
private readonly DtpNodeSession m_session;
private readonly Action<DtpNodeSessionFrameProcessor, byte[]> m_onFrameProcessed;
private readonly Thread m_thread;
private readonly Semaphore m_semaphore = new Semaphore(0, Int32.MaxValue);
private readonly ConcurrentQueue<byte[]> m_frameQueue = new ConcurrentQueue<byte[]>();
public DtpNodeSessionFrameProcessor(
DtpNode node,
DtpNodeSession session,
Action<DtpNodeSessionFrameProcessor, byte[]> onFrameProcessed)
{
m_node = node;
m_session = session;
m_onFrameProcessed = onFrameProcessed;
m_thread = new Thread(ThreadStart) { IsBackground = true };
m_thread.Start();
}
private void ThreadStart()
{
var id = Thread.CurrentThread.ManagedThreadId;
while (m_node.IsAlive && m_session.IsAlive)
{
if (!m_semaphore.WaitOne(10000))
continue;
byte[] assignedFrame = null;
while (!m_frameQueue.TryDequeue(out assignedFrame)) ;
Logger.L(LoggerLevel.Info, "Frame Processor " + id + " got frame of buffer size " + assignedFrame.Length);
using (var ms = new MemoryStream(assignedFrame))
using (var reader = new BinaryReader(ms))
{
UInt32 frameSize = reader.ReadUInt32();
UInt32 transactionId = reader.ReadUInt32();
Logger.L(LoggerLevel.Info, " => Frame Size: " + frameSize);
bool isLit = m_session.IsLocallyInitializedTransaction(transactionId);
Logger.L(LoggerLevel.Info, " => Is LIT?: " + isLit);
if (isLit)
{
var handler = m_session.GetLocallyInitializedTransactionHandler(transactionId);
if (handler == null)
throw new KeyNotFoundException("Referenced nonexistent LIT " + transactionId);
handler.ProcessMessage(
m_session,
new TransactionMessage(
transactionId,
assignedFrame,
8,
(int)(frameSize - 8)
)
);
}
else // riTransaction:
{
var opcode = frameSize > 8 ? assignedFrame[8] : (byte)0;
// GoCRITH returns true if the handler was created, false if it existed
var handler = m_session.GetRemotelyInitializedTransactionHandler(transactionId, opcode);
if (handler == null)
{
Logger.L(LoggerLevel.Info, " => Handler Nonexistant! Opcode: " + opcode);
handler = m_session.CreateAndRegisterRITransactionHandler(transactionId, opcode);
handler.ProcessInitialMessage(
m_session,
new TransactionInitialMessage(
transactionId,
opcode,
assignedFrame,
9,
(int)(frameSize - 9)
)
);
}
else
{
Logger.L(LoggerLevel.Info, " => Handler Existant!");
handler.ProcessMessage(
m_session,
new TransactionMessage(
transactionId,
assignedFrame,
8,
(int)(frameSize - 8)
)
);
}
}
} // using
m_onFrameProcessed(this, assignedFrame);
} // while
}
/// <summary>
/// Assigns the given frame content to this frame processor.
/// </summary>
internal void EnqueueFrame(byte[] frame)
{
if (frame == null)
throw new ArgumentNullException("frame");
m_frameQueue.Enqueue(frame);
Thread.MemoryBarrier();
m_semaphore.Release();
}
}
}