| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- using Azylee.Core.ThreadUtils.SleepUtils;
- using Azylee.Core.WindowsUtils.ConsoleUtils;
- using Azylee.Jsons;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- namespace Azylee.YeahWeb.SocketUtils.TcpUtils
- {
- public class TcppClient
- {
- const int ReceiveBufferSize = 1024;
- private string _IP = "";
- private int _Port = 52801;
- private TcpClient Client = null;
- private NetworkStream networkStream = null;
- TcpDelegate.ReceiveMessage ReceiveMessage;
- TcpDelegate.OnConnect OnConnect;
- TcpDelegate.OnDisconnect OnDisconnect;
- //public TcpDataConverter.Message ReceiveMessage;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="ip"></param>
- /// <param name="port"></param>
- public TcppClient(string ip, int port,
- TcpDelegate.ReceiveMessage receive,
- TcpDelegate.OnConnect connect,
- TcpDelegate.OnDisconnect disconnect)
- {
- this._IP = ip;
- this._Port = port;
- ReceiveMessage += receive;
- OnConnect += connect;
- OnDisconnect += disconnect;
- }
- #region 连接和关闭连接
- /// <summary>
- /// 连接到TcpServer(重连)
- /// </summary>
- public bool Connect()
- {
- try
- {
- if (this.Client == null || !this.Client.Connected)
- {
- this.Client = new TcpClient();
- //this.Read();
- IAsyncResult ar = this.Client.BeginConnect(this._IP, this._Port, acceptCallback, this.Client);
- bool isConnect = ar.AsyncWaitHandle.WaitOne(1000);
- if (isConnect) return true;
- }
- }
- catch { }
- return false;
- }
- /// <summary>
- /// 关闭连接
- /// </summary>
- public void Disconnect()
- {
- networkStream?.Close();
- Client?.Close();
- }
- #endregion
- #region 连接后的读写操作
- /// <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="type">类型</param>
- /// <param name="data">数据</param>
- public bool Write(int type, byte[] data)
- {
- return Write(new TcpDataModel() { Type = type, Data = data });
- }
- /// <summary>
- /// 发送数据
- /// </summary>
- /// <param name="type">类型</param>
- /// <param name="s">字符串</param>
- public bool Write(int type, string s)
- {
- return Write(new TcpDataModel() { Type = type, Data = Json.Object2Byte(s) });
- }
- /// <summary>
- /// 接受数据
- /// </summary>
- private void acceptCallback(IAsyncResult state)
- {
- try
- {
- this.Client = (TcpClient)state.AsyncState;
- this.Client.EndConnect(state);
- string host = this.Client.Client.RemoteEndPoint.ToString();
- ConnectTask(host, this.Client);
- }
- catch { }
- }
- 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);
- }
- #endregion
- }
- }
|