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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using FObjBase;
using System.Net.Sockets;
namespace TCPManager
{
//客户端长时间不通讯,必须发送 Send_HeartBeat
public class TCPCConn : TCPConn
{
enum STATE
{
Connected,
Not_Connect,
Connecting,
Err_Connect
}
private STATE state;
private DateTime conn_delay_time;
private IPEndPoint ep;
public IPEndPoint RemoteEP
{
get
{
if (ep != null)
return new IPEndPoint(ep.Address, ep.Port);
else
return null;
}
set
{
ep = new IPEndPoint(value.Address, value.Port);
}
}
PollModule.PollHandler onpoll_func;
public TCPCConn(IPEndPoint ipep, ParsePacketHandler func)
{
RemoteEP = ipep;
onpoll_func = new PollModule.PollHandler(delegate()
{
OnPoll();
});
ParsePacket = func;
ReStart();
}
void ReStart()
{
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Blocking = false;
state = STATE.Not_Connect;
PollModule.Current.Poll_Config(
PollModule.POLL_CONFIG.ADD, onpoll_func, TimeSpan.FromMilliseconds(1));
}
protected override void Stop()
{
base.Stop();
PollModule.Current.Poll_Config(
PollModule.POLL_CONFIG.REMOVE, onpoll_func);
IsConnected = false;
}
public override int OnPoll()
{
if (!Enable)
return 0;
switch (state)
{
case STATE.Not_Connect:
try
{
sock.Connect(RemoteEP);
}
catch (SocketException e)
{
if (e.SocketErrorCode != SocketError.WouldBlock)
{
conn_delay_time = DateTime.Now;
state = STATE.Err_Connect;
break;
}
}
conn_delay_time = DateTime.Now;
state = STATE.Connecting;
break;
case STATE.Connecting:
if (sock.Connected)
{
state = STATE.Connected;
first_poll = true;
Init();
//连接成功
IsConnected = true;
}
else if (sock.Poll(0, SelectMode.SelectError))
{
state = STATE.Not_Connect;
}
break;
case STATE.Connected:
{
if (base.OnPoll() < 0)
{
Enable = true;
ReStart();
state = STATE.Err_Connect;
}
break;
}
case STATE.Err_Connect:
if ((DateTime.Now-conn_delay_time) > TimeSpan.FromMilliseconds(1000))
state = STATE.Not_Connect;
break;
}
return 0;
}
}
}