Browse Source

合并Util到TCP通讯文件内

master
DESKTOP-B25GA9E\W35 2 years ago
parent
commit
6f7c9f5544
  1. 208
      Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs
  2. 184
      Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs
  3. 11
      Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs.meta
  4. 16
      Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs
  5. 4
      Assets/MsgTransmitTools/UDPClient/UDPClientView.cs
  6. 24
      Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs
  7. 15
      Assets/MsgTransmitTools/src/DataEventModel.cs

208
Assets/MsgTransmitTools/TCPClient/Script/Source/TCPEventModel.cs

@ -1,7 +1,10 @@ @@ -1,7 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
using QFrameworkCP;
using System;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net.NetworkInformation;
namespace JXSoft
{
@ -14,9 +17,8 @@ namespace JXSoft @@ -14,9 +17,8 @@ namespace JXSoft
public string myAddress = "";
protected override void OnInit()
{
myAddress = this.GetUtility<TCPUtility>().GetIP();
this.RegisterEvent<onLinkException>(e=> {
setTCPState(TCPLinkState.LinkTimeOut);
setLinkState((int)TCPLinkState.LinkTimeOut);
this.GetUtility<TCPUtility>().CloseTCPClient();
});
//注册重写发送事件
@ -31,13 +33,17 @@ namespace JXSoft @@ -31,13 +33,17 @@ namespace JXSoft
}
});
}
public void setTCPState(TCPLinkState state) {
this.tcpState = state;
this.SendEvent(new TCPStateChangedEvent(state));
public override void setLinkState(int linkStatus)
{
this.tcpState = (TCPLinkState)linkStatus;
this.SendEvent(new LinkStateChangedEvent(linkStatus));
}
public TCPLinkState getState() {
return this.tcpState;
public override int getLinkState()
{
return (int)this.tcpState;
}
/// <summary>
/// 设置对应IP,设置完毕后,自动进入链接状态(此时需要用户手动监听是否要进行链接服务器)
/// </summary>
@ -48,7 +54,7 @@ namespace JXSoft @@ -48,7 +54,7 @@ namespace JXSoft
this.tcpPort = port;
//此处可以加ip校验
Debug.LogWarning("此处未进行ip以及端口号校验,日后有需求可以增加");
setTCPState(TCPLinkState.Linking);
setLinkState((int)TCPLinkState.Linking);
}
/// <summary>
/// 与服务端建立链接
@ -60,11 +66,11 @@ namespace JXSoft @@ -60,11 +66,11 @@ namespace JXSoft
bool isSuccess = this.GetUtility<TCPUtility>().StartTCPClient(tcpAddress, tcpPort);
if (isSuccess) {
setTCPState(TCPLinkState.LinkSucess);
setLinkState((int)TCPLinkState.LinkSucess);
}
else
{
setTCPState(TCPLinkState.LinkFaild);
setLinkState((int)TCPLinkState.LinkFaild);
}
}
}
@ -80,10 +86,12 @@ namespace JXSoft @@ -80,10 +86,12 @@ namespace JXSoft
{
this.GetUtility<TCPUtility>().CloseTCPClient();
setTCPState(TCPLinkState.NoIp);
setLinkState((int)TCPLinkState.NoIp);
}
}
}
}
#region enum
@ -98,14 +106,6 @@ namespace JXSoft @@ -98,14 +106,6 @@ namespace JXSoft
#endregion
#region event
public struct TCPStateChangedEvent
{
public TCPLinkState state;
public TCPStateChangedEvent(TCPLinkState state_)
{
state = state_;
}
}
public struct onLinkException
{
public string exceptionMsg;
@ -115,4 +115,168 @@ namespace JXSoft @@ -115,4 +115,168 @@ namespace JXSoft
}
}
#endregion
#region util
public class TCPUtility : IUtility
{
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;
public TCPUtility()
{
Debug.LogWarning("使用无参数构造tcp时,需要手动开启tcp服务");
}
public TCPUtility(string tcpAddress, int tcpPort)
{
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
}

184
Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs

@ -1,184 +0,0 @@ @@ -1,184 +0,0 @@
using QFrameworkCP;
using UnityEngine;
using System;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Net.NetworkInformation;
namespace JXSoft
{
public class TCPUtility : IUtility
{
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;
public TCPUtility() {
Debug.LogWarning("使用无参数构造tcp时,需要手动开启tcp服务");
}
public TCPUtility(string tcpAddress, int tcpPort)
{
this.tcpAddress = tcpAddress;
this.tcpPort = tcpPort;
this.isTimeOut = false;
StartTCPClient();
}
public bool StartTCPClient()
{
isTimeOut = false;
if (!isOpenTCP)
{
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 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;
}
}
}

11
Assets/MsgTransmitTools/TCPClient/Script/Source/TCPUtility.cs.meta

@ -1,11 +0,0 @@ @@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 58c08e7452ec0014ab1aedcf80bddd95
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

16
Assets/MsgTransmitTools/TCPClient/Script/View/TCPClientView.cs

@ -16,7 +16,7 @@ namespace JXSoft { @@ -16,7 +16,7 @@ namespace JXSoft {
this.RegisterModel(new TCPEventModel());
}
}
public class TCPClientView : MonoBehaviour,IController
public class TCPClientView : MonoBehaviour,IController,ICanRegisterEvent
{
public UnityEvent onRecievedOpenDevice;
public UnityEvent onTCPLinkSuccess;
@ -39,27 +39,27 @@ namespace JXSoft { @@ -39,27 +39,27 @@ namespace JXSoft {
tcpPort = 20000;
deviceId = 1;
GetArchitecture().RegisterEvent<TCPStateChangedEvent>(e => {
if (e.state == TCPLinkState.Linking)
this.RegisterEvent<LinkStateChangedEvent>(e => {
if (e.linkState == (int)TCPLinkState.Linking)
{
Debug.Log("TCP开始链接");
this.GetModel<TCPEventModel>().linkServer();
}
if (e.state == TCPLinkState.LinkSucess)
if (e.linkState == (int)TCPLinkState.LinkSucess)
{
Debug.Log("TCP链接成功");
this.GetModel<TCPEventModel>().onReceive<LinkSuccessResponse>();
this.GetModel<TCPEventModel>().sendRequestCommand(new LinkTCPRequest(deviceId));
onTCPLinkSuccess.Invoke();
}
if (e.state == TCPLinkState.LinkFaild)
if (e.linkState == (int)TCPLinkState.LinkFaild)
{
Debug.Log("TCP连接失败,请联系设备服务管理员");
onTCPLinkFaild.Invoke();
}
});
GetArchitecture().RegisterEvent<ResponseMsgEvent>(e =>
this.RegisterEvent<ResponseMsgEvent>(e =>
{
if (e.res.GetType() == typeof(LinkSuccessResponse))
{
@ -69,7 +69,7 @@ namespace JXSoft { @@ -69,7 +69,7 @@ namespace JXSoft {
}
});
this.GetModel<TCPEventModel>().setTCPState(TCPLinkState.NoIp);
this.GetModel<TCPEventModel>().setLinkState((int)TCPLinkState.NoIp);
this.GetModel<TCPEventModel>().setIP(tcpAddress, tcpPort);
}
@ -80,7 +80,7 @@ namespace JXSoft { @@ -80,7 +80,7 @@ namespace JXSoft {
}
public IEnumerator waitTwoSecond() {
yield return new WaitForSeconds(2.0f);
this.GetModel<TCPEventModel>().setTCPState(TCPLinkState.Linking);
this.GetModel<TCPEventModel>().setLinkState((int)TCPLinkState.Linking);
}
public IArchitecture GetArchitecture()
{

4
Assets/MsgTransmitTools/UDPClient/UDPClientView.cs

@ -68,7 +68,7 @@ namespace JXSoft { @@ -68,7 +68,7 @@ namespace JXSoft {
}
});
this.GetModel<UDPEventModel>().setUDPState(UDPLinkState.NoIp);
this.GetModel<UDPEventModel>().setLinkState((int)UDPLinkState.NoIp);
this.GetModel<UDPEventModel>().setIP(UDPAddress, UDPPort);
}
@ -81,7 +81,7 @@ namespace JXSoft { @@ -81,7 +81,7 @@ namespace JXSoft {
public IEnumerator waitTwoSecond()
{
yield return new WaitForSeconds(2.0f);
this.GetModel<UDPEventModel>().setUDPState(UDPLinkState.Linking);
this.GetModel<UDPEventModel>().setLinkState((int)UDPLinkState.Linking);
}
public IArchitecture GetArchitecture()
{

24
Assets/MsgTransmitTools/UDPClient/UDPEventModel.cs

@ -13,7 +13,7 @@ namespace JXSoft { @@ -13,7 +13,7 @@ namespace JXSoft {
protected override void OnInit()
{
this.RegisterEvent<onUDPLinkException>(e => {
setUDPState(UDPLinkState.LinkTimeOut);
setLinkState((int)UDPLinkState.LinkTimeOut);
this.GetUtility<UDPUtility>().CloseUDPClient();
});
this.RegisterEvent<RequestMsgEvent>(e => {
@ -27,14 +27,16 @@ namespace JXSoft { @@ -27,14 +27,16 @@ namespace JXSoft {
}
});
}
public void setUDPState(UDPLinkState state)
public override void setLinkState(int linkStatus)
{
this.udpState = state;
this.SendEvent(new UDPStateChangedEvent(state));
this.udpState = (UDPLinkState)linkStatus;
this.SendEvent(new LinkStateChangedEvent(linkStatus));
}
public UDPLinkState getState()
public override int getLinkState()
{
return this.udpState;
return (int)this.udpState;
}
/// <summary>
/// 设置对应IP,设置完毕后,自动进入链接状态(此时需要用户手动监听是否要进行链接服务器)
@ -47,7 +49,7 @@ namespace JXSoft { @@ -47,7 +49,7 @@ namespace JXSoft {
this.udpPort = port;
//此处可以加ip校验
Debug.LogWarning("此处未进行ip以及端口号校验,日后有需求可以增加");
setUDPState(UDPLinkState.Linking);
setLinkState((int)UDPLinkState.Linking);
}
/// <summary>
/// 与服务端建立链接
@ -62,11 +64,11 @@ namespace JXSoft { @@ -62,11 +64,11 @@ namespace JXSoft {
bool isSuccess = this.GetUtility<UDPUtility>().StartUDPClient(udpAddress, udpPort);
if (isSuccess)
{
setUDPState(UDPLinkState.LinkSucess);
setLinkState((int)UDPLinkState.LinkSucess);
}
else
{
setUDPState(UDPLinkState.LinkFaild);
setLinkState((int)UDPLinkState.LinkFaild);
}
}
}
@ -82,10 +84,12 @@ namespace JXSoft { @@ -82,10 +84,12 @@ namespace JXSoft {
{
this.GetUtility<UDPUtility>().CloseUDPClient();
setUDPState(UDPLinkState.NoIp);
setLinkState((int)UDPLinkState.NoIp);
}
}
}
}
#region enum
public enum UDPLinkState

15
Assets/MsgTransmitTools/src/DataEventModel.cs

@ -63,14 +63,11 @@ namespace JXSoft { @@ -63,14 +63,11 @@ namespace JXSoft {
#endregion
#region LinkStatusManage
public virtual void setLinkStatus(int linkStatus) {
this.linkStatus = linkStatus;
this.SendEvent(new LinkStateChangedEvent(linkStatus));
}
public abstract void setLinkState(int linkStatus);
public abstract int getLinkState();
public virtual int getLinkStatus() {
return linkStatus;
}
//public abstract void nodifyLinkState<TLinkState>() where TLinkState:ILinkState;
//public abstract void registerLinkState();
#endregion
}
@ -145,6 +142,10 @@ namespace JXSoft { @@ -145,6 +142,10 @@ namespace JXSoft {
{
string toJson();
}
public interface ILinkState {
int getLinkStatus();
}
#endregion
#region AbstractClass

Loading…
Cancel
Save