You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
4.8 KiB
C#
166 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Sockets;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Rs.Framework
|
|
{
|
|
public class TcpClientHelper
|
|
{
|
|
private bool m_IsConnected = false;
|
|
private Socket m_Socket;
|
|
private string m_HostName;
|
|
private int m_Port;
|
|
public event Action<Socket> OnConnected;
|
|
public event Action<Socket> OnDisconnected;
|
|
public event Action<Socket, byte[]> DataRecived;
|
|
|
|
|
|
public bool IsConnected
|
|
{
|
|
get { return m_IsConnected; }
|
|
set { m_IsConnected = value; }
|
|
}
|
|
|
|
public TcpClientHelper()
|
|
{
|
|
|
|
}
|
|
|
|
public TcpClientHelper(string ip,int port)
|
|
{
|
|
m_HostName = ip;
|
|
m_Port = port;
|
|
}
|
|
|
|
public void Connect()
|
|
{
|
|
Connect(m_HostName, m_Port);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 链接服务的方法
|
|
/// </summary>
|
|
/// <param name="host"></param>
|
|
/// <param name="port"></param>
|
|
public void Connect(string host, int port)
|
|
{
|
|
m_HostName=host;
|
|
m_Port = port;
|
|
m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
Task.Run(() =>
|
|
{
|
|
while (!m_IsConnected)
|
|
{
|
|
try
|
|
{
|
|
m_Socket.Connect(host, port);
|
|
m_IsConnected = true;
|
|
OnConnected?.Invoke(m_Socket);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
m_Socket.Close();
|
|
m_Socket.Dispose();
|
|
m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
}
|
|
}
|
|
//开启接收数据线程
|
|
Thread tRecive = new Thread(new ParameterizedThreadStart(ReciveData));
|
|
tRecive.IsBackground = true;
|
|
tRecive.Start(m_Socket);
|
|
});
|
|
}
|
|
|
|
public void ReciveData(object socket)
|
|
{
|
|
var serverSocket = socket as Socket;
|
|
byte[] data = new byte[1024 * 1024];
|
|
while (m_IsConnected)
|
|
{
|
|
int len = 0;
|
|
try
|
|
{
|
|
len = serverSocket.Receive(data, 0, data.Length, SocketFlags.None);
|
|
if(len>0)
|
|
{
|
|
DataRecived?.Invoke(serverSocket, data.Take(len).ToArray());
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Debug(ex.Message);
|
|
//异常退出
|
|
if(serverSocket.Connected)
|
|
{
|
|
serverSocket.Shutdown(SocketShutdown.Both);
|
|
}
|
|
|
|
serverSocket.Close();
|
|
serverSocket.Dispose();
|
|
Connect(m_HostName,m_Port);//重新尝试去连接
|
|
m_IsConnected = false;
|
|
OnDisconnected?.Invoke(m_Socket);
|
|
return;
|
|
}
|
|
if (len <= 0)
|
|
{
|
|
serverSocket.Shutdown(SocketShutdown.Both);
|
|
serverSocket.Close();
|
|
serverSocket.Dispose();
|
|
m_IsConnected = false;
|
|
Connect(m_HostName, m_Port);//重新尝试去连接
|
|
OnDisconnected?.Invoke(m_Socket);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public int Send(string content)
|
|
{
|
|
byte[] sendData = Encoding.ASCII.GetBytes(content);
|
|
if(m_IsConnected)
|
|
{
|
|
return Send(sendData);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public int Send(string content,Encoding encode)
|
|
{
|
|
byte[] sendData = encode.GetBytes(content);
|
|
if (m_IsConnected)
|
|
{
|
|
return Send(sendData);
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public int Send(byte[] sendData)
|
|
{
|
|
if (m_IsConnected)
|
|
{
|
|
try
|
|
{
|
|
int len = m_Socket.Send(sendData);
|
|
return len;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
m_Socket.Close();
|
|
m_Socket.Dispose();
|
|
Connect(m_HostName, m_Port);
|
|
OnDisconnected?.Invoke(m_Socket);
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
}
|
|
}
|