/// <summary> /// 建立Tcp连接后处理过程 /// </summary> /// <param name="iar">异步Socket</param> protected virtual void Connected(IAsyncResult iar) { Socket socket = (Socket)iar.AsyncState; socket.EndConnect(iar); //创建新的会话 _session = new Session(socket); _isConnected = true; //触发连接建立事件 if (ConnectedServer != null) { ConnectedServer(this, new NetEventArgs(_session)); } //建立连接后应该立即接收数据 _session.ClientSocket.BeginReceive(_recvDataBuffer, 0, DefaultBufferSize, SocketFlags.None, new AsyncCallback(RecvData), socket); }
object System.ICloneable.Clone() { Session newSession = new Session(_cliSock); newSession.Datagram = _datagram; newSession.TypeOfExit = _exitType; return newSession; }
/// <summary> /// 关闭连接 /// </summary> public virtual void Close() { if (!_isConnected) { return; } _session.Close(); _session = null; _isConnected = false; }
/// <summary> /// 构造函数 /// </summary> /// <param name="client">客户端会话</param> public NetEventArgs(Session client) { if (null == client) { throw (new ArgumentNullException()); } _client = client; }
/// <summary> /// 关闭一个客户端Socket,首先需要关闭Session /// </summary> /// <param name="client">目标Socket对象</param> /// <param name="exitType">客户端退出的类型</param> protected virtual void CloseClient(Socket client, Session.ExitType exitType) { Debug.Assert(client != null); //查找该客户端是否存在,如果不存在,抛出异常 Session closeClient = FindSession(client); closeClient.TypeOfExit = exitType; if (closeClient != null) { CloseSession(closeClient); } else { throw (new ApplicationException("需要关闭的Socket对象不存在")); } }
/// <summary> /// 客户端连接处理函数 /// </summary> /// <param name="iar">欲建立服务器连接的Socket对象</param> protected virtual void AcceptConn(IAsyncResult iar) { //如果服务器停止了服务,就不能再接收新的客户端 if (!_isRun) { return; } //接受一个客户端的连接请求 Socket oldserver = (Socket)iar.AsyncState; Socket client = oldserver.EndAccept(iar); //检查是否达到最大的允许的客户端数目 if (_clientCount == _maxClient) { //服务器已满,发出通知 if (ServerFull != null) { ServerFull(this, new NetEventArgs(new Session(client))); } } else { Session newSession = new Session(client); _sessionTable.Add(newSession.ID, newSession); //客户端引用计数+1 _clientCount++; //开始接受来自该客户端的数据 client.BeginReceive(_recvDataBuffer, 0, _recvDataBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveData), client); //新的客户段连接,发出通知 if (ClientConn != null) { ClientConn(this, new NetEventArgs(newSession)); } } //继续接受客户端 _svrSock.BeginAccept(new AsyncCallback(AcceptConn), _svrSock); }
/// <summary> /// 发送数据 /// </summary> /// <param name="recvDataClient">接收数据的客户端会话</param> /// <param name="datagram">数据报文</param> public virtual void Send(Session recvDataClient, string datagram) { //获得数据编码 byte[] data = _coder.GetEncodingBytes(datagram); recvDataClient.ClientSocket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendDataEnd), recvDataClient.ClientSocket); }
/// <summary> /// 关闭一个与客户端之间的会话 /// </summary> /// <param name="closeClient">需要关闭的客户端会话对象</param> public virtual void CloseSession(Session closeClient) { Debug.Assert(closeClient != null); if (closeClient != null) { closeClient.Datagram = null; _sessionTable.Remove(closeClient.ID); _clientCount--; //客户端强制关闭链接 if (ClientClose != null) { ClientClose(this, new NetEventArgs(closeClient)); } closeClient.Close(); } }