private void ConnectTask(string host, TcpClient client) { Task.Factory.StartNew(() => { OnConnect?.Invoke(host);//委托:已连接 while (client.Connected) { try { TcpDataModel model = TcpStreamHelper.Read(client); if (model != null) { if (model.Type == int.MaxValue) { //返回心跳 Write(new TcpDataModel() { Type = int.MaxValue }); } else { ReceiveMessage(host, model);//委托:接收消息 } } } catch { } //Sleep.S(1); } client.Close(); OnDisconnect?.Invoke(host);//委托:断开连接 }); //lstn.BeginAcceptTcpClient(new AsyncCallback(acceptCallback), lstn); }
/// <summary> /// 发送数据 /// </summary> /// <param name="model">数据模型</param> public void Write(TcpDataModel model) { if (this.Client != null && this.Client.Connected) { bool flag = TcpStreamHelper.Write(Client, model); } }
public static int Byte2Model(List <byte> data, out TcpDataModel model) { model = null; if (data.Count > HeadLength && data[0] == 111 && data[1] == 222 && data[2] == 66 && data[3] == 66) { int msgCode = BitConverter.ToInt32(new byte[] { data[4], data[5], data[6], data[7] }, 0); int msgBodyLength = BitConverter.ToInt32(new byte[] { data[8], data[9], data[10], data[11] }, 0); if (data.Count >= HeadLength + msgBodyLength) { byte[] body = data.GetRange(HeadLength, msgBodyLength).ToArray(); string bodyToGBK = Encoding.GetEncoding("GBK").GetString(body); //ReceiveByteContent(body); Cons.Log(bodyToGBK); //Send(ReceiveByte.GetRange(0, 6).ToArray()); data.RemoveRange(0, 6 + msgBodyLength); //ReceiveMessage?.Invoke(msgCode, body); } } else { data.Clear(); //Socket.Send(new byte[] { 0 }); } return(0); }
/// <summary> /// 发送数据 /// </summary> /// <param name="model">数据模型</param> public bool Write(TcpDataModel model) { bool flag = false; if (this.Client != null && this.Client.Connected) { flag = TcpStreamHelper.Write(Client, model); } return(flag); }
/// <summary> /// 发送数据 /// </summary> /// <param name="host">主机地址</param> /// <param name="model">数据模型</param> public void Write(string host, TcpDataModel model) { var dictionary = Clients_Get(host); if (dictionary != null && dictionary.Client != null) { if (dictionary.Client.Connected) { bool flag = TcpStreamHelper.Write(dictionary.Client, model); } } }
private void ConnectTask(string host, TcpClient client) { TcpClientInfo clientInfo = TcpClientManager.GetInfoByHost(host); DateTime HeartbeatTime = DateTime.Now; //发送心跳 Task.Factory.StartNew(() => { while (client.Connected) { TcpDataModel model = new TcpDataModel() { Type = int.MaxValue }; TcpStreamHelper.Write(client, model); Sleep.S(5); //if (DateTime.Now.AddSeconds(-10) > HeartbeatTime) // client.Close(); Sleep.S(5); } }); //接收消息 Task.Factory.StartNew(() => { OnConnectAction?.Invoke(clientInfo);//委托:已连接 while (client.Connected) { try { TcpDataModel model = TcpStreamHelper.Read(client); if (model != null) { if (model.Type == int.MaxValue) { //过滤心跳 HeartbeatTime = DateTime.Now; } else { TcpClientManager.UpdateDownloadFlowCount(host, model.Data.Length); OnReceiveAction(clientInfo, model);//委托:接收消息 } } } catch { } //Sleep.S(1); } client.Close(); TcpClientManager.RemoveByHost(host); OnDisconnectAction?.Invoke(clientInfo);//委托:断开连接 }); }
/// <summary> /// 发送数据 /// </summary> /// <param name="host">主机地址</param> /// <param name="model">数据模型</param> public void Write(string host, TcpDataModel model) { var dictionary = TcpClientManager.GetInfoByHost(host); if (dictionary != null && dictionary.Client != null) { if (dictionary.Client.Connected) { TcpClientManager.UpdateUploadFlowCount(host, model.Data.Length); bool flag = TcpStreamHelper.Write(dictionary.Client, model); } } }
private void ConnectTask(string host, TcpClient client) { DateTime HeartbeatTime = DateTime.Now; //发送心跳 Task.Factory.StartNew(() => { while (client.Connected) { TcpDataModel model = new TcpDataModel() { Type = int.MaxValue }; TcpStreamHelper.Write(client, model); Sleep.S(5); //if (DateTime.Now.AddSeconds(-10) > HeartbeatTime) // client.Close(); Sleep.S(5); } }); //接收消息 Task.Factory.StartNew(() => { OnConnect?.Invoke(host);//委托:已连接 while (client.Connected) { try { TcpDataModel model = TcpStreamHelper.Read(client); if (model != null) { if (model.Type == int.MaxValue) { //过滤心跳 HeartbeatTime = DateTime.Now; } else { ReceiveMessage(host, model);//委托:接收消息 } } } catch { } //Sleep.S(1); } client.Close(); Clients_Del(host); OnDisconnect?.Invoke(host);//委托:断开连接 }); }
/// <summary> /// 流 读取 /// </summary> /// <param name="stream"></param> /// <returns></returns> public static TcpDataModel Read(TcpClient client) { TcpDataModel data = null; try { if (client != null && client.GetStream() != null) { //内容长度 byte[] data_length = new byte[4]; int read = client.GetStream().Read(data_length, 0, 4); int length = BitConverter.ToInt32(data_length, 0) - 4; if (read > 0 && length > 0) { //内容头部标志 byte[] data_head = new byte[4]; client.GetStream().Read(data_head, 0, 4); bool head = data_head[0] == 111 && data_head[1] == 222 && data_head[2] == 66 && data_head[3] == 66; if (head) { //读取内容 byte[] buffer = new byte[length]; int bf_read = 0; while (bf_read < length) { //循环读取内容,防止断包 bf_read += client.GetStream().Read(buffer, bf_read, length - bf_read); if (bf_read < length) { int x = bf_read; } } //解析内容 data = TcpDataModel.ToModel(buffer); } } else { if (read == 0) { client?.Close(); } } } } catch { } return(data); }
/// <summary> /// 转换为模型 /// </summary> /// <param name="bytes"></param> /// <returns></returns> public static TcpDataModel ToModel(byte[] bytes) { TcpDataModel model = null; try { int type = BitConverter.ToInt32(bytes, 0); int length = BitConverter.ToInt32(bytes, 4); byte[] data_byte = bytes.Skip(8).Take(length).ToArray(); model = new TcpDataModel(type, data_byte); } catch { } return(model); }
private void ConnectTask(TcpClient client) { Task.Factory.StartNew(() => { OnConnectAction?.Invoke();//委托:已连接 while (client.Connected) { try { TcpDataModel model = TcpStreamHelper.Read(client); if (model != null) { if (model.Type == int.MaxValue) { //返回心跳 Write(new TcpDataModel(int.MaxValue)); } else { //优先调用默认接收消息方法Action OnReceiveAction?.Invoke(model); //调用同步处理委托方法 if (Ls.Ok(SyncFunction)) { for (var i = 0; i < SyncFunction.Count; i++) { bool flag = SyncFunction.TryDequeue(out Tuple <int, Action <TcpDataModel> > fun); if (flag) { Task.Factory.StartNew(() => { fun.Item2?.Invoke(model); }); } } } } } } catch { } //Sleep.S(1); } client.Close(); OnDisconnectAction?.Invoke();//委托:断开连接 }); //lstn.BeginAcceptTcpClient(new AsyncCallback(acceptCallback), lstn); }
/// <summary> /// 发送数据(action禁止使用阻塞操作,必须新建task线程操作) /// </summary> /// <param name="model">数据模型</param> /// <param name="actionType">事件驱动处理类型</param> /// <param name="action">事件驱动处理方法</param> public bool Write(TcpDataModel model, int?actionType = 0, Action <TcpDataModel> action = null) { bool flag = false; if (Client != null && Client.Connected) { flag = TcpStreamHelper.Write(Client, model); } if (flag) { if (actionType != null && action != null) { int type = actionType.GetValueOrDefault(); SyncFunction.Enqueue(new Tuple <int, Action <TcpDataModel> >(type, action)); } } return(flag); }
/// <summary> /// 流 写入 /// </summary> /// <param name="stream"></param> /// <param name="model"></param> /// <returns></returns> public static bool Write(TcpClient client, TcpDataModel model) { try { if (client != null && client.GetStream() != null) { byte[] md_byte = model.ToByte(); byte[] length_byte = BitConverter.GetBytes((int)(md_byte.Length + 4)); List <byte> data = new List <byte>(); data.AddRange(length_byte); //长度 data.AddRange(new byte[] { 111, 222, 66, 66 }); //标志 data.AddRange(md_byte); //内容 client.GetStream().Write(data.ToArray(), 0, data.Count); //写出内容 return(true); } } catch { } return(false); }