-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathPacketSender.cs
190 lines (155 loc) · 4.19 KB
/
PacketSender.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
namespace KBEngine
{
using System;
using System.Net.Sockets;
using System.Net;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.Remoting.Messaging;
using MessageID = System.UInt16;
using MessageLength = System.UInt16;
/*
包发送模块(与服务端网络部分的名称对应)
处理网络数据的发送
*/
public class PacketSender
{
public delegate void AsyncSendMethod();
private byte[] _buffer;
int _wpos = 0; // 写入的数据位置
int _spos = 0; // 发送完毕的数据位置
object _sendingObj = new object();
Boolean _sending = false;
private NetworkInterface _networkInterface = null;
AsyncCallback _asyncCallback = null;
AsyncSendMethod _asyncSendMethod;
public PacketSender(NetworkInterface networkInterface)
{
_init(networkInterface);
}
~PacketSender()
{
Dbg.DEBUG_MSG("PacketSender::~PacketSender(), destroyed!");
}
void _init(NetworkInterface networkInterface)
{
_networkInterface = networkInterface;
_buffer = new byte[KBEngineApp.app.getInitArgs().SEND_BUFFER_MAX];
_asyncSendMethod = new AsyncSendMethod(this._asyncSend);
_asyncCallback = new AsyncCallback(_onSent);
_wpos = 0;
_spos = 0;
_sending = false;
}
public NetworkInterface networkInterface()
{
return _networkInterface;
}
public bool send(MemoryStream stream)
{
int dataLength = (int)stream.length();
if (dataLength <= 0)
return true;
Monitor.Enter(_sendingObj);
if (!_sending)
{
if (_wpos == _spos)
{
_wpos = 0;
_spos = 0;
}
}
int t_spos = _spos;
int space = 0;
int tt_wpos = _wpos % _buffer.Length;
int tt_spos = t_spos % _buffer.Length;
if(tt_wpos >= tt_spos)
space = _buffer.Length - tt_wpos + tt_spos - 1;
else
space = tt_spos - tt_wpos - 1;
if (dataLength > space)
{
Dbg.ERROR_MSG("PacketSender::send(): no space, Please adjust 'SEND_BUFFER_MAX'! data(" + dataLength
+ ") > space(" + space + "), wpos=" + _wpos + ", spos=" + t_spos);
return false;
}
int expect_total = tt_wpos + dataLength;
if(expect_total <= _buffer.Length)
{
Array.Copy(stream.data(), stream.rpos, _buffer, tt_wpos, dataLength);
}
else
{
int remain = _buffer.Length - tt_wpos;
Array.Copy(stream.data(), stream.rpos, _buffer, tt_wpos, remain);
Array.Copy(stream.data(), stream.rpos + remain, _buffer, 0, expect_total - _buffer.Length);
}
_wpos += dataLength;
if (!_sending)
{
_sending = true;
Monitor.Exit(_sendingObj);
_startSend();
}
else
{
Monitor.Exit(_sendingObj);
}
return true;
}
void _startSend()
{
// 由于socket用的是非阻塞式,因此在这里不能直接使用socket.send()方法
// 必须放到另一个线程中去做
_asyncSendMethod.BeginInvoke(_asyncCallback, _asyncSendMethod);
}
void _asyncSend()
{
if (_networkInterface == null || !_networkInterface.valid())
{
Dbg.WARNING_MSG("PacketSender::_asyncSend(): network interface invalid!");
return;
}
var socket = _networkInterface.sock();
while (true)
{
Monitor.Enter(_sendingObj);
int sendSize = _wpos - _spos;
int t_spos = _spos % _buffer.Length;
if (t_spos == 0)
t_spos = sendSize;
if (sendSize > _buffer.Length - t_spos)
sendSize = _buffer.Length - t_spos;
int bytesSent = 0;
try
{
bytesSent = socket.Send(_buffer, _spos % _buffer.Length, sendSize, 0);
}
catch (SocketException se)
{
Dbg.ERROR_MSG(string.Format("PacketSender::_asyncSend(): send data error, disconnect from '{0}'! error = '{1}'", socket.RemoteEndPoint, se));
Event.fireIn("_closeNetwork", new object[] { _networkInterface });
Monitor.Exit(_sendingObj);
return;
}
_spos += bytesSent;
// 所有数据发送完毕了
if (_spos == _wpos)
{
_sending = false;
Monitor.Exit(_sendingObj);
return;
}
Monitor.Exit(_sendingObj);
}
}
private static void _onSent(IAsyncResult ar)
{
AsyncSendMethod caller = (AsyncSendMethod)ar.AsyncState;
caller.EndInvoke(ar);
}
}
}