forked from kerryjiang/SuperSocket.ClientEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEasyClientBase.cs
153 lines (118 loc) · 4.13 KB
/
EasyClientBase.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.ProtoBase;
using System.Net;
namespace SuperSocket.ClientEngine
{
public abstract class EasyClientBase : IBufferState
{
private IClientSession m_Session;
private TaskCompletionSource<bool> m_ConnectTaskSource;
private TaskCompletionSource<bool> m_CloseTaskSource;
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 Task<bool> ConnectAsync(EndPoint serverEndPoint)
{
if (PipeLineProcessor == null)
throw new Exception("This client has not been initialized.");
m_ConnectTaskSource = new TaskCompletionSource<bool>();
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();
return m_ConnectTaskSource.Task;
}
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 Task<bool> Close()
{
var session = m_Session;
if(session != null && m_Connected)
{
var closeTaskSrc = new TaskCompletionSource<bool>();
session.Close();
m_CloseTaskSource = closeTaskSrc;
return closeTaskSrc.Task;
}
return null;
}
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_ConnectTaskSource.SetResult(false);
m_ConnectTaskSource = null;
}
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);
if(m_CloseTaskSource != null)
{
m_CloseTaskSource.SetResult(true);
m_CloseTaskSource = null;
}
}
public event EventHandler Closed;
void m_Session_Connected(object sender, EventArgs e)
{
m_Connected = true;
m_ConnectTaskSource.SetResult(true);
m_ConnectTaskSource = null;
var handler = Connected;
if(handler != null)
{
handler(this, EventArgs.Empty);
}
}
public event EventHandler Connected;
int IBufferState.DecreaseReference()
{
return 0;
}
void IBufferState.IncreaseReference()
{
}
}
}