forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocketCreate.cs
119 lines (109 loc) · 4.06 KB
/
SocketCreate.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using CYQ.Data.Tool;
namespace CYQ.Data.Cache
{
internal static class SocketCreate
{
//static Dictionary<string, Queue<Socket>> SocketQueue = new Dictionary<string, Queue<Socket>>();
//public static Socket Get(string host)
//{
// if (SocketQueue.ContainsKey(host) && SocketQueue[host].Count > 0)
// {
// return SocketQueue[host].Dequeue();
// }
// return New(host);
//}
public static Socket New(string host)
{
IPEndPoint endPoint = GetEndPoint(host);
//Set up the socket.
Socket socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, true);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 5000);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);
//socket.ReceiveTimeout = sendReceiveTimeout;
//socket.SendTimeout = sendReceiveTimeout;
//socket.SendBufferSize = 1024 * 1024;
//Do not use Nagle's Algorithm
//socket.NoDelay = true;
//Establish connection asynchronously to enable connect timeout.
IAsyncResult result = socket.BeginConnect(endPoint, null, null);
bool success = result.AsyncWaitHandle.WaitOne(3000, false);
if (!success)
{
try { socket.Close(); }
catch { }
throw new SocketException();
}
socket.EndConnect(result);
return socket;
}
//private static void OnEndConnect(IAsyncResult iar)
//{
// Socket socket = (Socket)iar.AsyncState;
// socket.EndConnect(iar);
//}
private static IPEndPoint GetEndPoint(string host)
{
//Parse port, default to 11211.
int port = 11211;
if (host.Contains(":"))
{
string[] split = host.Split(new char[] { ':' });
if (!Int32.TryParse(split[1], out port))
{
throw new ArgumentException("Unable to parse host: " + host);
}
host = split[0];
}
//Parse host string.
IPAddress address;
if (!IPAddress.TryParse(host, out address))
{
//See if we can resolve it as a hostname
try
{
address = Dns.GetHostEntry(host).AddressList[0];
}
catch (Exception e)
{
throw new ArgumentException("Unable to resolve host: " + host, e);
}
}
return new IPEndPoint(address, port);
}
///// <summary>
///// ÌáÇ°×¼±¸Socket
///// </summary>
//public static void ReadyForSocket(object list)
//{
// MList<string> hostList = list as MList<string>;
// int count =15;
// foreach (string host in hostList.List)
// {
// if (!SocketQueue.ContainsKey(host))
// {
// SocketQueue.Add(host, new Queue<Socket>());
// }
// for (int i = 0; i < count; i++)
// {
// SocketQueue[host].Enqueue(New(host));
// }
// }
//}
//public static void ClearSocketPool()
//{
// foreach (KeyValuePair<string,Queue<Socket>> item in SocketQueue)
// {
// while (SocketQueue[item.Key].Count > 0)
// {
// SocketQueue[item.Key].Dequeue().Close();
// }
// }
//}
}
}