Repository navigation
Expand file tree
/
Copy pathEndPointExt.cs
More file actions
177 lines (149 loc) · 6.51 KB
/
Copy pathEndPointExt.cs
File metadata and controls
177 lines (149 loc) · 6.51 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
using Selenium.Core;
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
namespace Selenium.Internal {
class EndPointExt : IDisposable {
/// <summary>
/// Lock a new TCP end point that can be unlocked later.
/// </summary>
public static EndPointExt Create(IPAddress address, bool disableInheritence) {
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.NoDelay = true;
socket.ReceiveBufferSize = 0;
socket.SendBufferSize = 0;
socket.Bind(new IPEndPoint(address, 0));
//Disable inheritance to the child processes so the main process can close the
//socket once a child process is launched.
if (disableInheritence)
Native.SetHandleInformation(socket.Handle, 1, 0);
return new EndPointExt(socket, (IPEndPoint)socket.LocalEndPoint);
}
internal static EndPointExt Parse(string address) {
string[] parts = address.Split(':');
IPAddress ipAddress = IPAddress.Parse(parts[0]);
int ipPort = int.Parse(parts[1]);
IPEndPoint endPoint = new IPEndPoint(ipAddress, ipPort);
return new EndPointExt(null, endPoint);
}
Socket _socket;
IPEndPoint _ipEndPoint;
public EndPointExt(Socket socket, IPEndPoint endpoint) {
_socket = socket;
_ipEndPoint = endpoint;
}
public void Dispose() {
if (_socket == null)
return;
_socket.Close();
_socket = null;
}
public IPEndPoint IPEndPoint {
get {
return _ipEndPoint;
}
}
public override string ToString() {
return _ipEndPoint.ToString();
}
/// <summary>
/// Returns true if a given host:port is connectable, false otherwise
/// </summary>
/// <param name="endPoint">Endpoint holding the host ip and port</param>
/// <param name="timeout">Timeout in millisecond</param>
/// <param name="delay">Delay to retry in millisecond</param>
/// <returns>True if succeed, false otherwise</returns>
public bool WaitForConnectable(int timeout, int delay) {
_socket.Close();
var endtime = DateTime.UtcNow.AddMilliseconds(timeout);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.ReceiveTimeout = socket.SendTimeout = 1;
socket.NoDelay = true;
try {
while (true) {
try {
socket.Connect(_ipEndPoint);
return true;
} catch (SocketException) {
} catch (Exception ex) {
throw new SeleniumException(ex);
}
if (DateTime.UtcNow > endtime)
return false;
SysWaiter.Wait(delay);
}
} finally {
socket.Disconnect(false);
socket.Close(0);
}
}
/// <summary>
/// Waits for a local port to be listening on the Loopback or Any address.
/// </summary>
/// <param name="port">Port number</param>
/// <param name="timeout">Timeout in milliseconds</param>
/// <param name="delay">Time to wait in milliseconds to wait before checking again</param>
/// <returns></returns>
public bool WaitForListening(int timeout, int delay) {
_socket.Close();
DateTime endtime = DateTime.UtcNow.AddMilliseconds(timeout);
int portLE = _ipEndPoint.Port;
int portBE = (portLE & 0xFF) << 8 | (portLE & 0xFF00) >> 8; //Convert port to big endian
int bufferSize = 1024;
IntPtr buffer = Marshal.AllocHGlobal(bufferSize);
int[] tcpTable = new int[bufferSize / sizeof(int)];
try {
while (true) {
int result = 0;
while (122 == (result = Native.GetTcpTable(buffer, ref bufferSize, false))) {
Marshal.FreeHGlobal(buffer);
buffer = Marshal.AllocHGlobal(bufferSize);
tcpTable = new int[bufferSize / sizeof(int)];
}
if (result != 0)
throw new NetworkInformationException(result);
// Buffer MIB_TCPTABLE :
// int dwNumEntries
// int dwState //MIB_TCPROW entry 1
// int dwLocalAddr
// int dwLocalPort
// int dwRemoteAddr
// int dwRemotePort
// int dwState //MIB_TCPROW entry 2
// ...
Marshal.Copy(buffer, tcpTable, 0, bufferSize / sizeof(int));
int dwNumEntries = tcpTable[0];
int tcpTableLength = 1 + dwNumEntries * 5;
//loop over each entry to find the port (1 entry is 5 integers)
for (int i = 3; i < tcpTableLength; i += 5) {
int dwLocalPort = tcpTable[i];
if (dwLocalPort == portBE) {
int dwLocalAddr = tcpTable[i - 1];
if (dwLocalAddr == 0 || dwLocalAddr == 0x0100007f) { //Any or Loopback
int dwState = tcpTable[i - 2];
if (dwState == 2) // 2=listening
return true;
break;
}
}
}
if (DateTime.UtcNow > endtime)
return false;
SysWaiter.Wait(delay);
}
} finally {
Marshal.FreeHGlobal(buffer);
}
}
static class Native {
const string IPHLPAPI = "Iphlpapi.dll";
const string KERNEL32 = "kernel32.dll";
[DllImport(IPHLPAPI)]
internal extern static int GetTcpTable(IntPtr pTcpTable, ref int dwOutBufLen, bool order);
[DllImport(KERNEL32)]
internal static extern bool SetHandleInformation(IntPtr hObject, int dwMask, int dwFlags);
}
}
}