private void ReciveCallBack(IAsyncResult ar) { ZRemoteClient client = (ZRemoteClient)ar.AsyncState; if (client.NetWork.Connected) { try { NetworkStream ns = client.NetWork.GetStream(); int len = ns.EndRead(ar); if (len > 0) { ReciveData(client, len); ns.BeginRead(client.buffer, 0, client.buffer.Length, new AsyncCallback(ReciveCallBack), client); } else { RemoveClient(client); } } catch (Exception ex) { RemoveClient(client); } } }
internal void AddClient(ZRemoteClient client) { lock (dicClients) { dicClients.Add(client.IDKey, client); } }
/// <summary> /// 客户端连接初始化 /// </summary> /// <param name="o"></param> private void Acceptor(IAsyncResult ar) { TcpListener server = ar.AsyncState as TcpListener; try { TcpClient remoteTcp = server.EndAcceptTcpClient(ar); //初始化连接的客户端 ZRemoteClient newClient = new ZRemoteClient(this, remoteTcp); AddClient(newClient); if (OnRemoteAccepted != null) { OnRemoteAccepted(this, new ZTcpServerEventArgs(this, newClient, newClient.NetWork)); } server.BeginAcceptTcpClient(new AsyncCallback(Acceptor), server);//继续监听客户端连接 NetworkStream ns = newClient.NetWork.GetStream(); ns.BeginRead(newClient.buffer, 0, newClient.buffer.Length, new AsyncCallback(ReciveCallBack), newClient); } catch (ObjectDisposedException ex) { //监听被关闭 } catch (Exception ex) { } }
public ZRemoteClient GetRemoteClient(string IDKey) { lock (dicClients) { ZRemoteClient r = null; dicClients.TryGetValue(IDKey, out r); return(r); } }
private void ReciveData(ZRemoteClient client, int len) { byte[] ds = client.buffer; //如果接收数据大于最大数据接收长度,丢弃 if (client.BufferStream.Length + len > MaxReciveLength) { client.BufferStream.SetLength(0); } if (IsDataEscape) { for (int i = 0; i < len; i++) { if (ds[i] == EscapeCharDefine.StartChar) { client.BufferStream.SetLength(0); client.BufferStream.WriteByte(ds[i]); } else if (ds[i] == EscapeCharDefine.EndChar) { client.BufferStream.WriteByte(ds[i]); byte[] d = EscapeCharDefine.UnEscape(client.BufferStream.ToArray()); client.BufferStream.SetLength(0); client.BufferStream.Write(d, 0, d.Length); //发出数据接收事件 if (OnReciveData != null) { OnReciveData(this, new ZTcpServerEventArgs(this, client, client.NetWork)); } //发出指令收到事件 if (IsCmdInvoke && (OnCmdInvoke != null)) { string str = Encoding.GetString(d); NameValueCollection cmd = StringEscape.StrToCollection(str); OnCmdInvoke(this, new ZTcpServerCmdEventArgs(this, client, client.NetWork, cmd)); } client.BufferStream.SetLength(0); } else { client.BufferStream.WriteByte(ds[i]); } } } else { if (OnReciveData != null) { client.BufferStream.SetLength(0); client.BufferStream.Write(ds, 0, len); OnReciveData(this, new ZTcpServerEventArgs(this, client, client.NetWork)); } } }
internal void RemoveClient(ZRemoteClient client) { lock (dicClients) { dicClients.Remove(client.IDKey); client.Close(); } if (OnDisConnect != null) { OnDisConnect(this, new ZTcpServerEventArgs(this, client, client.NetWork)); } }
public ZTcpServerCmdEventArgs(ZTcpServer zserver, ZRemoteClient zclient, TcpClient client, NameValueCollection data) : base(zserver, zclient, client) { _Data = data; }
public ZTcpServerEventArgs(ZTcpServer zserver, ZRemoteClient zclient, TcpClient client) { _TcpServer = zserver; _ZClient = zclient; _Client = client; }