forked from kerryjiang/SuperSocket.ClientEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEasyClientBase.Net20.cs
139 lines (105 loc) · 3.54 KB
/
EasyClientBase.Net20.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using SuperSocket.ProtoBase;
using System.Net;
namespace SuperSocket.ClientEngine
{
public abstract class EasyClientBase : IBufferState
{
private IClientSession m_Session;
private AutoResetEvent m_ConnectEvent = new AutoResetEvent(false);
private bool m_Connected = false;
protected IPipelineProcessor PipeLineProcessor { get; set; }
public int ReceiveBufferSize { get; set; }
public EasyClientBase()
{
}
public bool IsConnected { get { return m_Connected; } }
public void BeginConnect(EndPoint serverEndPoint)
{
if (PipeLineProcessor == null)
throw new Exception("This client has not been initialized.");
m_Session = new AsyncTcpSession(serverEndPoint, 4096);
m_Session.Connected += new EventHandler(m_Session_Connected);
m_Session.Error += new EventHandler<ErrorEventArgs>(m_Session_Error);
m_Session.Closed += new EventHandler(m_Session_Closed);
m_Session.DataReceived += new EventHandler<DataEventArgs>(m_Session_DataReceived);
if (ReceiveBufferSize > 0)
m_Session.ReceiveBufferSize = ReceiveBufferSize;
m_Session.Connect();
}
public void Send(ArraySegment<byte> segment)
{
if (!m_Connected || m_Session == null)
throw new Exception("The socket is not connected.");
m_Session.Send(segment);
}
public void Send(List<ArraySegment<byte>> segments)
{
if (!m_Connected || m_Session == null)
throw new Exception("The socket is not connected.");
m_Session.Send(segments);
}
public void Close()
{
var session = m_Session;
if (session != null && m_Connected)
{
session.Close();
}
}
void m_Session_DataReceived(object sender, DataEventArgs e)
{
PipeLineProcessor.Process(new ArraySegment<byte>(e.Data, e.Offset, e.Length), this as IBufferState);
}
void m_Session_Error(object sender, ErrorEventArgs e)
{
if (!m_Connected)
{
m_ConnectEvent.Set();
}
OnError(e);
}
private void OnError(Exception e)
{
OnError(new ErrorEventArgs(e));
}
private void OnError(ErrorEventArgs args)
{
var handler = Error;
if (handler != null)
handler(this, args);
}
public event EventHandler<ErrorEventArgs> Error;
void m_Session_Closed(object sender, EventArgs e)
{
m_Connected = false;
var handler = Closed;
if (handler != null)
handler(this, EventArgs.Empty);
m_ConnectEvent.Set();
}
public event EventHandler Closed;
void m_Session_Connected(object sender, EventArgs e)
{
m_Connected = true;
m_ConnectEvent.Set();
var handler = Connected;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
public event EventHandler Connected;
int IBufferState.DecreaseReference()
{
return 0;
}
void IBufferState.IncreaseReference()
{
}
}
}