forked from miyu/Dargon.Transport
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDtpNode.cs
97 lines (82 loc) · 3.2 KB
/
DtpNode.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
using ItzWarty.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Net;
using System.Net.Sockets;
using ItzWarty;
namespace Dargon.Transport
{
/// <summary>
/// A node in a Dargon Service Protocol Extended graph
/// </summary>
public class DtpNode : IDtpNode
{
private readonly IClientSource clientSource;
private readonly ConcurrentSet<DtpNodeSession> m_sessions = new ConcurrentSet<DtpNodeSession>();
private readonly List<IInstructionSet> m_instructionSets = new List<IInstructionSet>();
private bool m_isAlive = true;
public event ClientConnectedEventHandler ClientConnected;
protected internal DtpNode(IClientSource clientSource, IEnumerable<IInstructionSet> instructionSets)
{
this.clientSource = clientSource;
clientSource.SetAcceptCallback(AcceptCallback);
m_instructionSets.Add(new DefaultInstructionSet());
if (instructionSets != null)
{
foreach (var instructionSet in instructionSets)
m_instructionSets.Add(instructionSet);
}
}
private void AcceptCallback(Stream stream)
{
var session = new DtpNodeSession(this, stream, NodeRole.Server);
m_sessions.TryAdd(session);
OnClientConnected(new ClientConnectedEventArgs(session));
}
/// <summary>
/// Connect to another DSPEx node
/// </summary>
/// <param name="pipeName">
/// If null, connects to the default DSPEx pipe ("dargon" aka dargon daemon)
/// </param>
/// <returns></returns>
public IDSPExSession Connect(string pipeName)
{
var connection = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous | PipeOptions.WriteThrough);
connection.Connect();
var session = new DtpNodeSession(this, connection, NodeRole.Client);
m_sessions.TryAdd(session);
return session;
}
public IDSPExSession Connect(int port)
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new IPEndPoint(IPAddress.Loopback, port));
var session = new DtpNodeSession(this, new NetworkStream(socket), NodeRole.Client);
m_sessions.TryAdd(session);
return session;
}
public bool TryCreateRemotelyInitializedTransactionHandler(byte opcode, uint transactionId, out RemotelyInitializedTransactionHandler handler)
{
foreach (var instructionSet in m_instructionSets) {
if (instructionSet.TryCreateRemotelyInitializedTransactionHandler(opcode, transactionId, out handler))
return true;
}
handler = null;
return false;
}
public bool IsAlive { get { return m_isAlive; } set { m_isAlive = value; } }
protected virtual void OnClientConnected(ClientConnectedEventArgs e)
{
ClientConnectedEventHandler handler = ClientConnected;
if (handler != null) handler(this, e);
}
public void Shutdown()
{
m_isAlive = false;
clientSource.Shutdown();
m_sessions.ForEach(s => s.Dispose());
}
}
}