C#網路程式設計系列文章(三)之TcpListener實現非同步TCP伺服器
阿新 • • 發佈:2019-01-30
客戶端處理封裝類using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net; namespace NetFrame.Net.TCP.Listener.Asynchronous { /// <summary> /// TcpListener實現非同步TCP伺服器 /// </summary> public class AsyncTCPServer : IDisposable { #region Fields /// <summary> /// 伺服器程式允許的最大客戶端連線數 /// </summary> private int _maxClient; /// <summary> /// 當前的連線的客戶端數 /// </summary> private int _clientCount; /// <summary> /// 伺服器使用的非同步TcpListener /// </summary> private TcpListener _listener; /// <summary> /// 客戶端會話列表 /// </summary> private List<Object> _clients; private bool disposed = false; #endregion #region Properties /// <summary> /// 伺服器是否正在執行 /// </summary> public bool IsRunning { get; private set; } /// <summary> /// 監聽的IP地址 /// </summary> public IPAddress Address { get; private set; } /// <summary> /// 監聽的埠 /// </summary> public int Port { get; private set; } /// <summary> /// 通訊使用的編碼 /// </summary> public Encoding Encoding { get; set; } #endregion #region 建構函式 /// <summary> /// 非同步TCP伺服器 /// </summary> /// <param name="listenPort">監聽的埠</param> public AsyncTCPServer(int listenPort) : this(IPAddress.Any, listenPort) { } /// <summary> /// 非同步TCP伺服器 /// </summary> /// <param name="localEP">監聽的終結點</param> public AsyncTCPServer(IPEndPoint localEP) : this(localEP.Address, localEP.Port) { } /// <summary> /// 非同步TCP伺服器 /// </summary> /// <param name="localIPAddress">監聽的IP地址</param> /// <param name="listenPort">監聽的埠</param> public AsyncTCPServer(IPAddress localIPAddress, int listenPort) { Address = localIPAddress; Port = listenPort; this.Encoding = Encoding.Default; _clients = new List<Object>(); _listener = new TcpListener(Address, Port); _listener.AllowNatTraversal(true); } #endregion #region Method /// <summary> /// 啟動伺服器 /// </summary> public void Start() { if (!IsRunning) { IsRunning = true; _listener.Start(); _listener.BeginAcceptTcpClient( new AsyncCallback(HandleTcpClientAccepted), _listener); } } /// <summary> /// 啟動伺服器 /// </summary> /// <param name="backlog"> /// 伺服器所允許的掛起連線序列的最大長度 /// </param> public void Start(int backlog) { if (!IsRunning) { IsRunning = true; _listener.Start(backlog); _listener.BeginAcceptTcpClient( new AsyncCallback(HandleTcpClientAccepted), _listener); } } /// <summary> /// 停止伺服器 /// </summary> public void Stop() { if (IsRunning) { IsRunning = false; _listener.Stop(); lock (_clients) { //關閉所有客戶端連線 CloseAllClient(); } } } /// <summary> /// 處理客戶端連線的函式 /// </summary> /// <param name="ar"></param> private void HandleTcpClientAccepted(IAsyncResult ar) { if (IsRunning) { //TcpListener tcpListener = (TcpListener)ar.AsyncState; TcpClient client = _listener.EndAcceptTcpClient(ar); byte[] buffer = new byte[client.ReceiveBufferSize]; TCPClientState state = new TCPClientState(client, buffer); lock (_clients) { _clients.Add(state); RaiseClientConnected(state); } NetworkStream stream = state.NetworkStream; //開始非同步讀取資料 stream.BeginRead(state.Buffer, 0, state.Buffer.Length, HandleDataReceived, state); _listener.BeginAcceptTcpClient( new AsyncCallback(HandleTcpClientAccepted), ar.AsyncState); } } /// <summary> /// 資料接受回撥函式 /// </summary> /// <param name="ar"></param> private void HandleDataReceived(IAsyncResult ar) { if (IsRunning) { TCPClientState state = (TCPClientState)ar.AsyncState; NetworkStream stream = state.NetworkStream; int recv = 0; try { recv = stream.EndRead(ar); } catch { recv = 0; } if (recv == 0) { // connection has been closed lock (_clients) { _clients.Remove(state); //觸發客戶端連線斷開事件 RaiseClientDisconnected(state); return; } } // received byte and trigger event notification byte[] buff = new byte[recv]; Buffer.BlockCopy(state.Buffer, 0, buff, 0, recv); //觸發資料收到事件 RaiseDataReceived(state); // continue listening for tcp datagram packets stream.BeginRead(state.Buffer, 0, state.Buffer.Length, HandleDataReceived, state); } } /// <summary> /// 傳送資料 /// </summary> /// <param name="state">接收資料的客戶端會話</param> /// <param name="data">資料報文</param> public void Send(TCPClientState state, byte[] data) { RaisePrepareSend(state); Send(state.TcpClient, data); } /// <summary> /// 非同步傳送資料至指定的客戶端 /// </summary> /// <param name="client">客戶端</param> /// <param name="data">報文</param> public void Send(TcpClient client, byte[] data) { if (!IsRunning) throw new InvalidProgramException("This TCP Scoket server has not been started."); if (client == null) throw new ArgumentNullException("client"); if (data == null) throw new ArgumentNullException("data"); client.GetStream().BeginWrite(data, 0, data.Length, SendDataEnd, client); } /// <summary> /// 傳送資料完成處理函式 /// </summary> /// <param name="ar">目標客戶端Socket</param> private void SendDataEnd(IAsyncResult ar) { ((TcpClient)ar.AsyncState).GetStream().EndWrite(ar); RaiseCompletedSend(null); } #endregion #region 事件 /// <summary> /// 與客戶端的連線已建立事件 /// </summary> public event EventHandler<AsyncEventArgs> ClientConnected; /// <summary> /// 與客戶端的連線已斷開事件 /// </summary> public event EventHandler<AsyncEventArgs> ClientDisconnected; /// <summary> /// 觸發客戶端連線事件 /// </summary> /// <param name="state"></param> private void RaiseClientConnected(TCPClientState state) { if (ClientConnected != null) { ClientConnected(this, new AsyncEventArgs(state)); } } /// <summary> /// 觸發客戶端連線斷開事件 /// </summary> /// <param name="client"></param> private void RaiseClientDisconnected(TCPClientState state) { if (ClientDisconnected != null) { ClientDisconnected(this, new AsyncEventArgs("連線斷開")); } } /// <summary> /// 接收到資料事件 /// </summary> public event EventHandler<AsyncEventArgs> DataReceived; private void RaiseDataReceived(TCPClientState state) { if (DataReceived != null) { DataReceived(this, new AsyncEventArgs(state)); } } /// <summary> /// 傳送資料前的事件 /// </summary> public event EventHandler<AsyncEventArgs> PrepareSend; /// <summary> /// 觸發傳送資料前的事件 /// </summary> /// <param name="state"></param> private void RaisePrepareSend(TCPClientState state) { if (PrepareSend != null) { PrepareSend(this, new AsyncEventArgs(state)); } } /// <summary> /// 資料傳送完畢事件 /// </summary> public event EventHandler<AsyncEventArgs> CompletedSend; /// <summary> /// 觸發資料傳送完畢的事件 /// </summary> /// <param name="state"></param> private void RaiseCompletedSend(TCPClientState state) { if (CompletedSend != null) { CompletedSend(this, new AsyncEventArgs(state)); } } /// <summary> /// 網路錯誤事件 /// </summary> public event EventHandler<AsyncEventArgs> NetError; /// <summary> /// 觸發網路錯誤事件 /// </summary> /// <param name="state"></param> private void RaiseNetError(TCPClientState state) { if (NetError != null) { NetError(this, new AsyncEventArgs(state)); } } /// <summary> /// 異常事件 /// </summary> public event EventHandler<AsyncEventArgs> OtherException; /// <summary> /// 觸發異常事件 /// </summary> /// <param name="state"></param> private void RaiseOtherException(TCPClientState state, string descrip) { if (OtherException != null) { OtherException(this, new AsyncEventArgs(descrip, state)); } } private void RaiseOtherException(TCPClientState state) { RaiseOtherException(state, ""); } #endregion #region Close /// <summary> /// 關閉一個與客戶端之間的會話 /// </summary> /// <param name="state">需要關閉的客戶端會話物件</param> public void Close(TCPClientState state) { if (state != null) { state.Close(); _clients.Remove(state); _clientCount--; //TODO 觸發關閉事件 } } /// <summary> /// 關閉所有的客戶端會話,與所有的客戶端連線會斷開 /// </summary> public void CloseAllClient() { foreach (TCPClientState client in _clients) { Close(client); } _clientCount = 0; _clients.Clear(); } #endregion #region 釋放 /// <summary> /// Performs application-defined tasks associated with freeing, /// releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// <summary> /// Releases unmanaged and - optionally - managed resources /// </summary> /// <param name="disposing"><c>true</c> to release /// both managed and unmanaged resources; <c>false</c> /// to release only unmanaged resources.</param> protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { try { Stop(); if (_listener != null) { _listener = null; } } catch (SocketException) { //TODO RaiseOtherException(null); } } disposed = true; } } #endregion } }