/// <summary> /// 断开指定连接 /// </summary> /// <param name="endpoint"></param> public void Disconnect(IPEndPoint endpoint) { SocketNetClient <TBuilder, TPackage> client = null; if (m_clients.TryRemove(endpoint, out client)) { client.Dispose(); } }
/// <summary> /// 获取指定连接,失败返回null /// </summary> /// <param name="endpoint"></param> /// <returns></returns> public NetworkConnection <TPackage> GetConnection(IPEndPoint endpoint) { NetworkConnection <TPackage> target = null; SocketNetClient <TBuilder, TPackage> client = null; if (m_clients.TryGetValue(endpoint, out client)) { target = new NetworkConnection <TPackage>(client.RemoteEndPoint, client); } return(target); }
private void PrivateSend(IPEndPoint clientEP, byte[] data, int offset, int length, bool isAsync, bool encode, object userTag) { if (m_state != (int)EnumNetworkStatus.Established) { throw new InvalidProgramException(string.Format("服务状态{0},无法发送", (EnumNetworkStatus)m_state)); } if (clientEP != null) { SocketNetClient <TBuilder, TPackage> client = null; if (m_clients.TryGetValue(clientEP, out client)) { if (isAsync) { client.BeginSend(data, offset, length, encode, userTag); } else { client.Send(data, offset, length, encode, userTag); } } else { throw new Exception(string.Format("客户端{0}不在线,无法发送", clientEP)); } } else { //发送给所有连接 foreach (var item in m_clients) { try { if (isAsync) { item.Value.BeginSend(data, offset, length, encode, userTag); } else { item.Value.Send(data, offset, length, encode, userTag); } } catch (Exception) { //注意异常没抛出 } } } }
/// <summary> /// 一个连接接收完成 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnAcceptCompleted(object sender, SocketAsyncEventArgs e) { if (m_state != (int)EnumNetworkStatus.Established) { return; } if (e.SocketError == SocketError.Success) { //和客户端关联的socket Socket socket = e.AcceptSocket; try { if (socket.Connected) { var client = new SocketNetClient <TBuilder, TPackage>(socket, EnumNetworkType.TCP, NetworkGlobal.TCP_RECEIVE_BUFFER_SIZE, NetworkGlobal.TCP_SOCKET_RECEIVE_SIZE, NetworkGlobal.TCP_SOCKET_SEND_SIZE); client.OnSocketStatusChangedEvent += Client_OnClientSocketStatusChangedEvent; client.OnDataReceivedEvent += Client_OnDataReceivedEvent; client.OnExceptionHappenedEvent += Client_OnExceptionHappenedEvent; //更新上线 m_clients.AddOrUpdate((IPEndPoint)socket.RemoteEndPoint, client, (v, k) => { return(client); }); //启动收收数 client.StartReceive(); //上线通知 if (OnClientSocketStatusChangedEvent != null) { var args = new SocketStatusChangedArgs <TPackage>(EnumNetworkStatus.Established, client); OnClientSocketStatusChangedEvent(this, args); } } } catch (SocketException ex) { if (OnExceptionHappenedEvent != null) { var args = new ExceptionHappenedArgs((IPEndPoint)socket.RemoteEndPoint, "处理客户端连接上线失败", ex); OnExceptionHappenedEvent(this, args); } } } //投递下一个接受请求 StartAcceptConnection(e); }
private void Client_OnClientSocketStatusChangedEvent(object arg1, SocketStatusChangedArgs <TPackage> arg2) { switch (arg2.Status) { case EnumNetworkStatus.Shutdown: { SocketNetClient <TBuilder, TPackage> client = null; m_clients.TryRemove(arg2.Target.RemoteEndPoint, out client); } break; default: break; } if (OnClientSocketStatusChangedEvent != null) { OnClientSocketStatusChangedEvent(this, arg2); } }