独立开发的游戏服务器端敲完了(告别BUG!!!!) 写个博客(总结)出来
介绍(大概都知道) SocketAsyncEventArgs是.NET Framework 3.5出的
SocketAsyncEventArgs常用的成员
Buffer
BytesTransferred
SocketError
UserToken
BytesTransferred
SetBuffer
Completed
///只列出了主要方法~
Accept监听
bool b= socket.AcceptAsync(e);
if (!b)
{
//注意这里要上锁
lock(e)
{
ProcessAccept(e);
}
}
Receive监听
<pre name="code" class="csharp">bool b= userToken.connect.ReceiveAsync(userToken.ReceiveEventArgs);
if (!b)
{
lock (userToken) //这里也要加锁
{
ProcessReceive(userToken.ReceiveEventArgs);
}
}
<pre name="code" class="csharp">//判断网络消息接收是否成功
if (userToken.ReceiveEventArgs.BytesTransferred > 0 && userToken.ReceiveEventArgs.SocketError == SocketError.Success)
{
//...处理消息和再次开启消息监听
}
IO_Completed
<pre name="code" class="csharp">lock (userToken)
{
if (e.LastOperation == SocketAsyncOperation.Receive)
{
ProcessReceive(e);
}
else if (e.LastOperation == SocketAsyncOperation.Send)
{
ProcessSend(e);
}
}
Send
<pre name="code" class="csharp">sendSAEA.SetBuffer(buff, 0, buff.Length);
bool result = connect.SendAsync(sendSAEA);
if (!result)
{
sendProcess(sendSAEA);//这里只是个回调
}
//之前在插入数据的时候我也上锁了 否则会数据混乱 尴尬...
<img alt="得意" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/proud.gif" />