forked from Loriowar/IpHlpApidotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCPUDPConnection.cs
204 lines (186 loc) · 5.35 KB
/
TCPUDPConnection.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
using System;
using System.Net;
namespace IpHlpApidotnet
{
/// <summary>
/// Store information concerning single TCP/UDP connection
/// </summary>
public class TCPUDPConnection
{
private int _dwState;
public int iState
{
get { return _dwState; }
set
{
if (_dwState != value)
{
_dwState = value;
_State = Utils.StateToStr(value);
}
}
}
//private static TCPUDPConnections _conns = null;
public TCPUDPConnection(TCPUDPConnections conns)
{
//_conns = conns;
}
public static string GetHostName(IPEndPoint HostAddress)
{
return Utils.GetHostName(HostAddress);
}
private bool _IsResolveIP = true;
public bool IsResolveIP
{
get { return _IsResolveIP; }
set { _IsResolveIP = value; }
}
private Protocol _Protocol;
public Protocol Protocol
{
get { return _Protocol; }
set { _Protocol = value; }
}
private string _State = String.Empty;
public string State
{
get { return _State; }
}
private IPEndPoint _OldLocalHostName;
private IPEndPoint _OldRemoteHostName;
private string _LocalAddress = String.Empty;
private string _RemoteAddress = String.Empty;
private void SaveHostName(bool IsLocalHostName)
{
if (IsLocalHostName)
{
this._LocalAddress = GetHostName(this._Local);
this._OldLocalHostName = this._Local;
}
else
{
this._RemoteAddress = GetHostName(this._Remote);
this._OldRemoteHostName = this._Remote;
}
}
public string LocalAddress
{
get
{
if (this._OldLocalHostName == this._Local)
{
if (this._LocalAddress.Trim() == String.Empty)
{
this.SaveHostName(true);
}
}
else
{
this.SaveHostName(true);
}
return this._LocalAddress;
}
}
public string RemoteAddress
{
get
{
if (this._OldRemoteHostName == this._Remote)
{
if (this._RemoteAddress.Trim() == String.Empty)
{
this.SaveHostName(false);
}
}
else
{
this.SaveHostName(false);
}
return this._RemoteAddress;
}
}
// Return exist local address or "unknown" if address if empty
public string TryGetLocalAddress()
{
return (this._LocalAddress.Trim() == String.Empty) ? "unknown" : this._LocalAddress;
}
// Return exist remote address or "unknown" if address if empty
public string TryGetRemoteAddress()
{
return (this._RemoteAddress.Trim() == String.Empty) ? "unknown" : this._RemoteAddress;
}
private IPEndPoint _Local = null;
public IPEndPoint Local //LocalAddress
{
get { return this._Local; }
set
{
if (this._Local != value)
{
this._Local = value;
}
}
}
private IPEndPoint _Remote;
public IPEndPoint Remote //RemoteAddress
{
get { return this._Remote; }
set
{
if (this._Remote != value)
{
this._Remote = value;
}
}
}
private int _dwOwningPid;
public int PID
{
get { return this._dwOwningPid; }
set
{
if (this._dwOwningPid != value)
{
this._dwOwningPid = value;
}
}
}
private void SaveProcessID()
{
this._ProcessName = Utils.GetProcessNameByPID(this._dwOwningPid);
this._OldProcessID = this._dwOwningPid;
}
private int _OldProcessID = -1;
private string _ProcessName = String.Empty;
public string ProcessName
{
get
{
if (this._OldProcessID == this._dwOwningPid)
{
if (this._ProcessName.Trim() == String.Empty)
{
this.SaveProcessID();
}
}
else
{
this.SaveProcessID();
}
return this._ProcessName;
}
}
private DateTime _WasActiveAt = DateTime.MinValue;
public DateTime WasActiveAt
{
get { return _WasActiveAt; }
internal set { _WasActiveAt = value; }
}
private Object _Tag = null;
public Object Tag
{
get { return this._Tag; }
set { this._Tag = value; }
}
}
}