-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConnectionManager.cs
150 lines (128 loc) · 5.92 KB
/
ConnectionManager.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EIPNET.EIP;
using EIPNET.CIP;
namespace EIPNET
{
/// <summary>
/// Represents services of the Connection Manager Class Object
/// </summary>
public static class ConnectionManager
{
#if MONO
public static bool ForwardOpen(SessionInfo si, byte[] Path)
{
return ForwardOpen(si, Path, 0);
}
#endif
#if MONO
public static bool ForwardOpen(SessionInfo si, byte[] Path, short O2T_Params)
#else
/// <summary>
/// Forward Open Request
/// </summary>
/// <param name="si">Session Info</param>
/// <param name="Path">Path to the target</param>
/// <param name="O2T_Params">O2T_Params, leave as zero to automatically select</param>
/// <returns>True if the ForwardOpen request was successful, false otherwise</returns>
public static bool ForwardOpen(SessionInfo si, byte[] Path, short O2T_Params = 0)
#endif
{
ForwardOpenRequest fwdRequest = new ForwardOpenRequest(si.ConnectionParameters);
fwdRequest.ConnectionTimeoutMultiplier = 0;
if (O2T_Params == 0)
{
fwdRequest.O2T_ConnectionParameters = unchecked((short)(NetworkConnectionParams.Owned | //NA
NetworkConnectionParams.Point2Point | //Point2Point
NetworkConnectionParams.HighPriority | //LowPriority
NetworkConnectionParams.Variable | //Variable
508)); //508 uS
fwdRequest.T2O_ConnectionParameters = fwdRequest.O2T_ConnectionParameters;
}
else
{
fwdRequest.O2T_ConnectionParameters = O2T_Params;
fwdRequest.T2O_ConnectionParameters = O2T_Params;
}
//fwdRequest.O2T_RPI = 10000000;
fwdRequest.O2T_RPI = 2000;
fwdRequest.T2O_RPI = fwdRequest.O2T_RPI;
fwdRequest.TransportTrigger = NetworkConnectionParams.CM_Transp_IsServer | //CM_Transp_IsServer
3 |
NetworkConnectionParams.CM_Trig_App;
fwdRequest.TransportTrigger = 0xA3;
fwdRequest.Connection_Path = Path;
EncapsReply response = MessageRouter.SendMR_Request(si, (byte)ConnectionManagerService.ForwardOpen, CommonPaths.ConnectionManager,
fwdRequest.Pack());
if (response == null)
return false;
//Need to check the last codes...
si.LastSessionError = (int)response.Status;
//We actually need to find out if this is a success or failure...
ForwardOpenReply fReply = response;
if (fReply is ForwardOpenReply_Fail)
return false;
//Need to copy some data into the connection parameters...
ForwardOpenReply_Success fSucc = (ForwardOpenReply_Success)fReply;
ConnectionParameters cParams = si.ConnectionParameters;
cParams.O2T_CID = fSucc.O2T_ConnectionId;
cParams.T2O_CID = fSucc.T2O_ConnectionId;
cParams.O2T_API = fSucc.O2T_API;
cParams.T2O_API = fSucc.T2O_API;
si.ConnectionParameters = cParams;
if (response.Status == 0)
return true;
return false;
}
#if MONO
public static bool ConnectOverControlNet(SessionInfo si, byte[] Path)
{
return ConnectOverControlNet(si, Path, 0);
}
#endif
#if MONO
public static bool ConnectOverControlNet(SessionInfo si, byte[] Path, short O2T_Params)
#else
/// <summary>
/// Connects to a device over ControlNet
/// </summary>
/// <param name="si">Session Info</param>
/// <param name="Path">Path to the target</param>
/// <param name="O2T_Params">O2T_Params, leave as zero to automatically select</param>
/// <returns>True if the request was successful, false otherwise</returns>
public static bool ConnectOverControlNet(SessionInfo si, byte[] Path, short O2T_Params = 0)
#endif
{
byte[] tPath = new byte[Path.Length + CommonPaths.Router.Length];
Buffer.BlockCopy(Path, 0, tPath, 0, Path.Length);
Buffer.BlockCopy(CommonPaths.Router, 0, tPath, Path.Length, CommonPaths.Router.Length);
return ForwardOpen(si, tPath, O2T_Params);
}
/// <summary>
/// Closes a connection that was open with ForwardOpen or ConnectOverControlNet
/// </summary>
/// <param name="si">Session Info</param>
/// <param name="Path">Path to the target</param>
/// <returns>True if the request was successful, false otherwise</returns>
public static bool ForwardClose(SessionInfo si, byte[] Path)
{
ForwardCloseRequest request = new ForwardCloseRequest();
request.ConnectionSerialNumber = si.ConnectionParameters.ConnectionSerialNumber;
request.VendorId = si.ConnectionParameters.VendorID;
request.OriginatorSerialNumber = si.ConnectionParameters.OriginatorSerialNumber;
byte[] tPath = new byte[Path.Length + CommonPaths.Router.Length];
Buffer.BlockCopy(Path, 0, tPath, 0, Path.Length);
Buffer.BlockCopy(CommonPaths.Router, 0, tPath, Path.Length, CommonPaths.Router.Length);
request.Path = tPath;
EncapsReply response = MessageRouter.SendMR_Request(si, (byte)ConnectionManagerService.ForwardClose, CommonPaths.ConnectionManager,
request.Pack());
if (response == null)
return false;
if (response.Status == 0)
return true;
return false;
}
}
}