You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
299 lines
8.6 KiB
299 lines
8.6 KiB
2 years ago
|
using UnityEngine;
|
||
2 years ago
|
using QFrameworkCP;
|
||
2 years ago
|
using System;
|
||
|
using System.Text;
|
||
|
using System.Net.Sockets;
|
||
|
using System.Threading;
|
||
|
using System.Net.NetworkInformation;
|
||
2 years ago
|
using System.Text.RegularExpressions;
|
||
2 years ago
|
|
||
2 years ago
|
namespace JXSoft
|
||
2 years ago
|
{
|
||
|
//统筹管理TCP的命令池以及相关链接信息
|
||
2 years ago
|
public class TCPClientModel : DataEventModel
|
||
2 years ago
|
{
|
||
|
private TCPLinkState tcpState = TCPLinkState.NoIp;
|
||
|
private string tcpAddress = "";
|
||
|
private int tcpPort = 0;
|
||
|
public string myAddress = "";
|
||
|
protected override void OnInit()
|
||
|
{
|
||
|
this.RegisterEvent<onLinkException>(e=> {
|
||
2 years ago
|
setLinkState((int)TCPLinkState.LinkTimeOut);
|
||
2 years ago
|
this.GetUtility<TCPClientUtility>().CloseTCPClient();
|
||
2 years ago
|
});
|
||
2 years ago
|
//注册重写发送事件
|
||
|
this.RegisterEvent<RequestMsgEvent>(e => {
|
||
2 years ago
|
if (this.GetUtility<TCPClientUtility>().isOpenTCP)
|
||
2 years ago
|
{
|
||
2 years ago
|
this.GetUtility<TCPClientUtility>().sendData(e.req.toProtocolData());
|
||
2 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
Debug.LogWarning("请先开启TCP链接");
|
||
|
}
|
||
|
});
|
||
2 years ago
|
}
|
||
2 years ago
|
public override void setLinkState(int linkStatus)
|
||
|
{
|
||
|
this.tcpState = (TCPLinkState)linkStatus;
|
||
|
this.SendEvent(new LinkStateChangedEvent(linkStatus));
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
public override int getLinkState()
|
||
|
{
|
||
|
return (int)this.tcpState;
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
2 years ago
|
/// <summary>
|
||
|
/// 设置对应IP,设置完毕后,自动进入链接状态(此时需要用户手动监听是否要进行链接服务器)
|
||
|
/// </summary>
|
||
|
/// <param name="ip">ip地址</param>
|
||
|
/// <param name="port">端口号</param>
|
||
|
public void setIP(string ip,int port) {
|
||
|
this.tcpAddress = ip;
|
||
|
this.tcpPort = port;
|
||
2 years ago
|
string patten = @"(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])";
|
||
|
//ip校验
|
||
|
if (Regex.IsMatch(ip, patten) && port < 65535)
|
||
|
{
|
||
|
setLinkState((int)TCPLinkState.Linking);
|
||
|
}
|
||
|
else {
|
||
|
Debug.LogWarning("ip格式错误");
|
||
|
}
|
||
2 years ago
|
}
|
||
|
/// <summary>
|
||
|
/// 与服务端建立链接
|
||
|
/// </summary>
|
||
|
public void linkServer() {
|
||
|
if (tcpState == TCPLinkState.Linking)
|
||
|
{
|
||
2 years ago
|
if (!this.GetUtility<TCPClientUtility>().isOpenTCP) {
|
||
2 years ago
|
|
||
2 years ago
|
bool isSuccess = this.GetUtility<TCPClientUtility>().StartTCPClient(tcpAddress, tcpPort);
|
||
2 years ago
|
if (isSuccess) {
|
||
2 years ago
|
setLinkState((int)TCPLinkState.LinkSucess);
|
||
2 years ago
|
}
|
||
|
else
|
||
|
{
|
||
2 years ago
|
setLinkState((int)TCPLinkState.LinkFaild);
|
||
2 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 与服务端断开链接
|
||
|
/// </summary>
|
||
|
public void closeServer()
|
||
|
{
|
||
|
if (tcpState == TCPLinkState.LinkSucess)
|
||
|
{
|
||
2 years ago
|
if (this.GetUtility<TCPClientUtility>().isOpenTCP)
|
||
2 years ago
|
{
|
||
|
|
||
2 years ago
|
this.GetUtility<TCPClientUtility>().CloseTCPClient();
|
||
2 years ago
|
setLinkState((int)TCPLinkState.NoIp);
|
||
2 years ago
|
}
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
}
|
||
|
|
||
|
#region enum
|
||
|
public enum TCPLinkState
|
||
|
{
|
||
|
NoIp = 0,
|
||
|
Linking = 1,
|
||
|
LinkFaild = 2,
|
||
|
LinkSucess = 3,
|
||
|
LinkTimeOut = 4
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
#endregion
|
||
|
|
||
|
#region event
|
||
|
public struct onLinkException
|
||
|
{
|
||
|
public string exceptionMsg;
|
||
|
public onLinkException(string exceptionMsg_)
|
||
|
{
|
||
|
exceptionMsg = exceptionMsg_;
|
||
|
}
|
||
|
}
|
||
|
#endregion
|
||
2 years ago
|
|
||
|
#region util
|
||
2 years ago
|
public class TCPClientUtility : IUtility
|
||
2 years ago
|
{
|
||
|
public string tcpAddress;
|
||
|
public int tcpPort;
|
||
|
public TcpClient tcpClient;
|
||
|
public NetworkStream sendStream;
|
||
|
public bool isOpenTCP = false;
|
||
|
public bool isReceivedValue = false;
|
||
|
public string receivedData = "";
|
||
|
public string exceptionData = "";
|
||
|
|
||
|
public Thread reciveT;
|
||
|
public bool isTimeOut = false;
|
||
|
|
||
2 years ago
|
public TCPClientUtility()
|
||
2 years ago
|
{
|
||
|
Debug.LogWarning("使用无参数构造tcp时,需要手动开启tcp服务");
|
||
|
|
||
|
}
|
||
2 years ago
|
public TCPClientUtility(string tcpAddress, int tcpPort)
|
||
2 years ago
|
{
|
||
|
this.tcpAddress = tcpAddress;
|
||
|
this.tcpPort = tcpPort;
|
||
|
this.isTimeOut = false;
|
||
|
StartTCPClient();
|
||
|
}
|
||
|
public bool StartTCPClient()
|
||
|
{
|
||
|
return StartTCPClient(tcpAddress, tcpPort);
|
||
|
}
|
||
|
public bool StartTCPClient(string ip, int port)
|
||
|
{
|
||
|
isTimeOut = false;
|
||
|
if (!isOpenTCP)
|
||
|
{
|
||
|
tcpAddress = ip;
|
||
|
tcpPort = port;
|
||
|
try
|
||
|
{
|
||
|
tcpClient = new TcpClient(tcpAddress, tcpPort);
|
||
|
sendStream = tcpClient.GetStream();
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
exceptionData = e.ToString();
|
||
|
return false;
|
||
|
}
|
||
|
isOpenTCP = true;
|
||
|
reciveT = new Thread(RecciveMsg);
|
||
|
reciveT.IsBackground = true;
|
||
|
reciveT.Start();
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public void CloseTCPClient()
|
||
|
{
|
||
|
if (tcpClient.Connected)
|
||
|
{
|
||
|
sendStream.Close();
|
||
|
tcpClient.Close();
|
||
|
isOpenTCP = false;
|
||
|
reciveT.Abort();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void sendData(string data)
|
||
|
{
|
||
|
if (isOpenTCP == false)
|
||
|
return;
|
||
|
byte[] sendBytes = Encoding.Default.GetBytes(data);
|
||
|
sendStream.Write(sendBytes, 0, sendBytes.Length);
|
||
|
}
|
||
|
|
||
|
public void RecciveMsg()
|
||
|
{
|
||
|
byte[] receiveBuff = new byte[1024];
|
||
|
int reviceLength = 0;
|
||
|
string msg = "";
|
||
|
while (isOpenTCP)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
reviceLength = tcpClient.Client.Receive(receiveBuff);
|
||
|
msg = Encoding.UTF8.GetString(receiveBuff, 0, reviceLength);
|
||
|
if (msg != "")
|
||
|
{
|
||
|
receivedData = msg;
|
||
|
isReceivedValue = true;
|
||
|
}
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
Debug.LogWarning(e);
|
||
|
//断线发送异常
|
||
|
isTimeOut = true;
|
||
|
exceptionData = e.ToString();
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
Debug.Log("------------end While-------------");
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 线程接收到消息后
|
||
|
/// </summary>
|
||
|
/// <returns>接收到的消息</returns>
|
||
|
public string getReceivedValue()
|
||
|
{
|
||
|
if (isReceivedValue)
|
||
|
{
|
||
|
isReceivedValue = false;
|
||
|
return receivedData;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public bool getTimeOutState()
|
||
|
{
|
||
|
return isTimeOut;
|
||
|
}
|
||
|
|
||
|
public bool IsReceivedMsg()
|
||
|
{
|
||
|
return sendStream.Length != 0;
|
||
|
}
|
||
|
public bool IsConnected()
|
||
|
{
|
||
|
return tcpClient.Connected;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取本机IP
|
||
|
/// </summary>
|
||
|
/// <returns>string :ip地址</returns>
|
||
|
public string GetIP()
|
||
|
{
|
||
|
string output = "";
|
||
|
|
||
|
foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces())
|
||
|
{
|
||
|
NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211; //无线局域网适配器
|
||
|
|
||
|
if ((item.NetworkInterfaceType == _type1) && item.OperationalStatus == OperationalStatus.Up)
|
||
|
{
|
||
|
foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
|
||
|
{
|
||
|
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
|
||
|
{
|
||
|
output = ip.Address.ToString();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return output;
|
||
|
}
|
||
|
}
|
||
|
#endregion
|
||
2 years ago
|
}
|
||
|
|
||
|
namespace TCPLinkState {
|
||
|
public class StateNoIp : BaseLinkState.StateNoLink {
|
||
|
public override bool linkToServer()
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
2 years ago
|
}
|