forked from kerryjiang/SuperSocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUdpSocketSession.cs
More file actions
150 lines (124 loc) · 4.28 KB
/
Copy pathUdpSocketSession.cs
File metadata and controls
150 lines (124 loc) · 4.28 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using SuperSocket.Common;
using SuperSocket.ProtoBase;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using SuperSocket.SocketBase.Utils;
namespace SuperSocket.SocketEngine
{
class UdpSocketSession : SocketSession
{
private Socket m_ServerSocket;
private SocketAsyncEventArgs m_SocketEventArgSend;
public UdpSocketSession(Socket serverSocket, IPEndPoint remoteEndPoint)
: base(remoteEndPoint.ToString())
{
m_ServerSocket = serverSocket;
RemoteEndPoint = remoteEndPoint;
}
public UdpSocketSession(Socket serverSocket, IPEndPoint remoteEndPoint, string sessionID)
: base(sessionID)
{
m_ServerSocket = serverSocket;
RemoteEndPoint = remoteEndPoint;
}
public override void Initialize(IAppSession appSession)
{
base.Initialize(appSession);
if (!SyncSend)
{
//Initialize SocketAsyncEventArgs for sending
m_SocketEventArgSend = new SocketAsyncEventArgs();
m_SocketEventArgSend.Completed += new EventHandler<SocketAsyncEventArgs>(OnSendingCompleted);
}
}
public override IPEndPoint LocalEndPoint
{
get { return (IPEndPoint)m_ServerSocket.LocalEndPoint; }
}
/// <summary>
/// Updates the remote end point of the client.
/// </summary>
/// <param name="remoteEndPoint">The remote end point.</param>
internal void UpdateRemoteEndPoint(IPEndPoint remoteEndPoint)
{
this.RemoteEndPoint = remoteEndPoint;
}
public override void Start()
{
StartSession();
}
protected override void SendAsync(SendingQueue queue)
{
var e = m_SocketEventArgSend;
e.RemoteEndPoint = RemoteEndPoint;
e.UserToken = queue;
var item = queue[queue.Position];
e.SetBuffer(item.Array, item.Offset, item.Count);
if (!m_ServerSocket.SendToAsync(e))
OnSendingCompleted(this, e);
}
void OnSendingCompleted(object sender, SocketAsyncEventArgs e)
{
var queue = e.UserToken as SendingQueue;
if (e.SocketError != SocketError.Success)
{
var log = AppSession.Logger;
if (log.IsErrorEnabled)
log.Error(new SocketException((int)e.SocketError));
e.UserToken = null;
OnSendError(queue, CloseReason.SocketError);
return;
}
var newPos = queue.Position + 1;
if (newPos >= queue.Count)
{
e.UserToken = null;
OnSendingCompleted(queue);
return;
}
queue.Position = newPos;
SendAsync(queue);
}
protected override void SendSync(SendingQueue queue)
{
for (var i = 0; i < queue.Count; i++)
{
var item = queue[i];
m_ServerSocket.SendTo(item.Array, item.Offset, item.Count, SocketFlags.None, RemoteEndPoint);
}
OnSendingCompleted(queue);
}
public override void ApplySecureProtocol()
{
throw new NotSupportedException();
}
protected override bool TryValidateClosedBySocket(out Socket socket)
{
socket = null;
return false;
}
protected override void OnClosed(CloseReason reason)
{
if (m_SocketEventArgSend != null)
{
m_SocketEventArgSend.Completed += new EventHandler<SocketAsyncEventArgs>(OnSendingCompleted);
m_SocketEventArgSend.Dispose();
m_SocketEventArgSend = null;
}
base.OnClosed(reason);
}
protected override void ReturnBuffer(IList<KeyValuePair<ArraySegment<byte>, IBufferState>> buffers, int offset, int length)
{
//TODO:
}
}
}