http://blog.csdn.net/sqldebug_fan/article/details/17557341
1、SocketAsyncEventArgs介紹
SocketAsyncEventArgs是微軟提供的高性能異步Socket實現類,主要為高性能網絡服務器應用程序而設計,主要是為了避免在在異步套接字 I/O 量非常大時發生重復的對象分配和同步。使用此類執行異步套接字操作的模式包含以下步驟:
1.分配一個新的 SocketAsyncEventArgs 上下文對象,或者從應用程序池中獲取一個空閑的此類對象。
2.將該上下文對象的屬性設置為要執行的操作(例如,完成回調方法、數據緩沖區、緩沖區偏移量以及要傳輸的最大數據量)。
3.調用適當的套接字方法 (xxxAsync) 以啟動異步操作。
4.如果異步套接字方法 (xxxAsync) 返回 true,則在回調中查詢上下文屬性來獲取完成狀態。
5.如果異步套接字方法 (xxxAsync) 返回 false,則說明操作是同步完成的。 可以查詢上下文屬性來獲取操作結果。
6.將該上下文重用於另一個操作,將它放回到應用程序池中,或者將它丟棄。
2、SocketAsyncEventArgs封裝
使用SocketAsyncEventArgs之前需要先建立一個Socket監聽對象,使用如下代碼:
然后開始接受連接,SocketAsyncEventArgs有連接時會通過Completed事件通知外面,所以接受連接的代碼如下:
- public void Start(IPEndPoint localEndPoint)
- {
- listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- listenSocket.Bind(localEndPoint);
- listenSocket.Listen(m_numConnections);
- Program.Logger.InfoFormat("Start listen socket {0} success", localEndPoint.ToString());
- //for (int i = 0; i < 64; i++) //不能循環投遞多次AcceptAsync,會造成只接收8000連接后不接收連接了
- StartAccept(null);
- m_daemonThread = new DaemonThread(this);
- }
接受連接響應事件代碼:
- public void StartAccept(SocketAsyncEventArgs acceptEventArgs)
- {
- if (acceptEventArgs == null)
- {
- acceptEventArgs = new SocketAsyncEventArgs();
- acceptEventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed);
- }
- else
- {
- acceptEventArgs.AcceptSocket = null; //釋放上次綁定的Socket,等待下一個Socket連接
- }
- m_maxNumberAcceptedClients.WaitOne(); //獲取信號量
- bool willRaiseEvent = listenSocket.AcceptAsync(acceptEventArgs);
- if (!willRaiseEvent)
- {
- ProcessAccept(acceptEventArgs);
- }
- }
- void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs acceptEventArgs)
- {
- try
- {
- ProcessAccept(acceptEventArgs);
- }
- catch (Exception E)
- {
- Program.Logger.ErrorFormat("Accept client {0} error, message: {1}", acceptEventArgs.AcceptSocket, E.Message);
- Program.Logger.Error(E.StackTrace);
- }
- }
接受連接后,從當前Socket緩沖池AsyncSocketUserTokenPool中獲取一個用戶對象AsyncSocketUserToken,AsyncSocketUserToken包含一個接收異步事件m_receiveEventArgs,一個發送異步事件m_sendEventArgs,接收數據緩沖區m_receiveBuffer,發送數據緩沖區m_sendBuffer,協議邏輯調用對象m_asyncSocketInvokeElement,建立服務對象后,需要實現接收和發送的事件響應函數:
- private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs)
- {
- Program.Logger.InfoFormat("Client connection accepted. Local Address: {0}, Remote Address: {1}",
- acceptEventArgs.AcceptSocket.LocalEndPoint, acceptEventArgs.AcceptSocket.RemoteEndPoint);
- AsyncSocketUserToken userToken = m_asyncSocketUserTokenPool.Pop();
- m_asyncSocketUserTokenList.Add(userToken); //添加到正在連接列表
- userToken.ConnectSocket = acceptEventArgs.AcceptSocket;
- userToken.ConnectDateTime = DateTime.Now;
- try
- {
- bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs); //投遞接收請求
- if (!willRaiseEvent)
- {
- lock (userToken)
- {
- ProcessReceive(userToken.ReceiveEventArgs);
- }
- }
- }
- catch (Exception E)
- {
- Program.Logger.ErrorFormat("Accept client {0} error, message: {1}", userToken.ConnectSocket, E.Message);
- Program.Logger.Error(E.StackTrace);
- }
- StartAccept(acceptEventArgs); //把當前異步事件釋放,等待下次連接
- }
在Completed事件中需要處理發送和接收的具體邏輯代碼,其中接收的邏輯實現如下:
- void IO_Completed(object sender, SocketAsyncEventArgs asyncEventArgs)
- {
- AsyncSocketUserToken userToken = asyncEventArgs.UserToken as AsyncSocketUserToken;
- userToken.ActiveDateTime = DateTime.Now;
- try
- {
- lock (userToken)
- {
- if (asyncEventArgs.LastOperation == SocketAsyncOperation.Receive)
- ProcessReceive(asyncEventArgs);
- else if (asyncEventArgs.LastOperation == SocketAsyncOperation.Send)
- ProcessSend(asyncEventArgs);
- else
- throw new ArgumentException("The last operation completed on the socket was not a receive or send");
- }
- }
- catch (Exception E)
- {
- Program.Logger.ErrorFormat("IO_Completed {0} error, message: {1}", userToken.ConnectSocket, E.Message);
- Program.Logger.Error(E.StackTrace);
- }
- }
由於我們制定的協議第一個字節是協議標識,因此在接收到第一個字節的時候需要綁定協議解析對象,具體代碼實現如下:
- private void ProcessReceive(SocketAsyncEventArgs receiveEventArgs)
- {
- AsyncSocketUserToken userToken = receiveEventArgs.UserToken as AsyncSocketUserToken;
- if (userToken.ConnectSocket == null)
- return;
- userToken.ActiveDateTime = DateTime.Now;
- if (userToken.ReceiveEventArgs.BytesTransferred > 0 && userToken.ReceiveEventArgs.SocketError == SocketError.Success)
- {
- int offset = userToken.ReceiveEventArgs.Offset;
- int count = userToken.ReceiveEventArgs.BytesTransferred;
- if ((userToken.AsyncSocketInvokeElement == null) & (userToken.ConnectSocket != null)) //存在Socket對象,並且沒有綁定協議對象,則進行協議對象綁定
- {
- BuildingSocketInvokeElement(userToken);
- offset = offset + 1;
- count = count - 1;
- }
- if (userToken.AsyncSocketInvokeElement == null) //如果沒有解析對象,提示非法連接並關閉連接
- {
- Program.Logger.WarnFormat("Illegal client connection. Local Address: {0}, Remote Address: {1}", userToken.ConnectSocket.LocalEndPoint,
- userToken.ConnectSocket.RemoteEndPoint);
- CloseClientSocket(userToken);
- }
- else
- {
- if (count > 0) //處理接收數據
- {
- if (!userToken.AsyncSocketInvokeElement.ProcessReceive(userToken.ReceiveEventArgs.Buffer, offset, count))
- { //如果處理數據返回失敗,則斷開連接
- CloseClientSocket(userToken);
- }
- else //否則投遞下次介紹數據請求
- {
- bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs); //投遞接收請求
- if (!willRaiseEvent)
- ProcessReceive(userToken.ReceiveEventArgs);
- }
- }
- else
- {
- bool willRaiseEvent = userToken.ConnectSocket.ReceiveAsync(userToken.ReceiveEventArgs); //投遞接收請求
- if (!willRaiseEvent)
- ProcessReceive(userToken.ReceiveEventArgs);
- }
- }
- }
- else
- {
- CloseClientSocket(userToken);
- }
- }
發送響應函數實現需要注意,我們是把發送數據放到一個列表中,當上一個發送事件完成響應Completed事件,這時我們需要檢測發送隊列中是否存在未發送的數據,如果存在則繼續發送。
- private void BuildingSocketInvokeElement(AsyncSocketUserToken userToken)
- {
- byte flag = userToken.ReceiveEventArgs.Buffer[userToken.ReceiveEventArgs.Offset];
- if (flag == (byte)SocketFlag.Upload)
- userToken.AsyncSocketInvokeElement = new UploadSocketProtocol(this, userToken);
- else if (flag == (byte)SocketFlag.Download)
- userToken.AsyncSocketInvokeElement = new DownloadSocketProtocol(this, userToken);
- else if (flag == (byte)SocketFlag.RemoteStream)
- userToken.AsyncSocketInvokeElement = new RemoteStreamSocketProtocol(this, userToken);
- else if (flag == (byte)SocketFlag.Throughput)
- userToken.AsyncSocketInvokeElement = new ThroughputSocketProtocol(this, userToken);
- else if (flag == (byte)SocketFlag.Control)
- userToken.AsyncSocketInvokeElement = new ControlSocketProtocol(this, userToken);
- else if (flag == (byte)SocketFlag.LogOutput)
- userToken.AsyncSocketInvokeElement = new LogOutputSocketProtocol(this, userToken);
- if (userToken.AsyncSocketInvokeElement != null)
- {
- Program.Logger.InfoFormat("Building socket invoke element {0}.Local Address: {1}, Remote Address: {2}",
- userToken.AsyncSocketInvokeElement, userToken.ConnectSocket.LocalEndPoint, userToken.ConnectSocket.RemoteEndPoint);
- }
- }
SendCompleted用於回調下次需要發送的數據,具體實現過程如下:
- private bool ProcessSend(SocketAsyncEventArgs sendEventArgs)
- {
- AsyncSocketUserToken userToken = sendEventArgs.UserToken as AsyncSocketUserToken;
- if (userToken.AsyncSocketInvokeElement == null)
- return false;
- userToken.ActiveDateTime = DateTime.Now;
- if (sendEventArgs.SocketError == SocketError.Success)
- return userToken.AsyncSocketInvokeElement.SendCompleted(); //調用子類回調函數
- else
- {
- CloseClientSocket(userToken);
- return false;
- }
- }
當一個SocketAsyncEventArgs斷開后,我們需要斷開對應的Socket連接,並釋放對應資源,具體實現函數如下:
- public virtual bool SendCompleted()
- {
- m_activeDT = DateTime.UtcNow;
- m_sendAsync = false;
- AsyncSendBufferManager asyncSendBufferManager = m_asyncSocketUserToken.SendBuffer;
- asyncSendBufferManager.ClearFirstPacket(); //清除已發送的包
- int offset = 0;
- int count = 0;
- if (asyncSendBufferManager.GetFirstPacket(ref offset, ref count))
- {
- m_sendAsync = true;
- return m_asyncSocketServer.SendAsyncEvent(m_asyncSocketUserToken.ConnectSocket, m_asyncSocketUserToken.SendEventArgs,
- asyncSendBufferManager.DynamicBufferManager.Buffer, offset, count);
- }
- else
- return SendCallback();
- }
- //發送回調函數,用於連續下發數據
- public virtual bool SendCallback()
- {
- return true;
- }
- public void CloseClientSocket(AsyncSocketUserToken userToken)
- {
- if (userToken.ConnectSocket == null)
- return;
- string socketInfo = string.Format("Local Address: {0} Remote Address: {1}", userToken.ConnectSocket.LocalEndPoint,
- userToken.ConnectSocket.RemoteEndPoint);
- Program.Logger.InfoFormat("Client connection disconnected. {0}", socketInfo);
- try
- {
- userToken.ConnectSocket.Shutdown(SocketShutdown.Both);
- }
- catch (Exception E)
- {
- Program.Logger.ErrorFormat("CloseClientSocket Disconnect client {0} error, message: {1}", socketInfo, E.Message);
- }
- userToken.ConnectSocket.Close();
- userToken.ConnectSocket = null; //釋放引用,並清理緩存,包括釋放協議對象等資源
- m_maxNumberAcceptedClients.Release();
- m_asyncSocketUserTokenPool.Push(userToken);
- m_asyncSocketUserTokenList.Remove(userToken);
- }
3、SocketAsyncEventArgs封裝和MSDN的不同點
MSDN在http://msdn.microsoft.com/zh-cn/library/system.NET.sockets.socketasynceventargs(v=vs.110).aspx實現了示例代碼,並實現了初步的池化處理,我們是在它的基礎上擴展實現了接收數據緩沖,發送數據隊列,並把發送SocketAsyncEventArgs和接收SocketAsyncEventArgs分開,並實現了協議解析單元,這樣做的好處是方便后續邏輯實現文件的上傳,下載和日志輸出。
DEMO下載地址:http://download.csdn.Net/detail/sqldebug_fan/7467745
免責聲明:此代碼只是為了演示C#完成端口編程,僅用於學習和研究,切勿用於商業用途。水平有限,C#也屬於初學,錯誤在所難免,歡迎指正和指導。郵箱地址:fansheng_hx@163.com。
