313 lines
9.8 KiB
C#
313 lines
9.8 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Text;
|
||
using UnityEngine;
|
||
|
||
namespace GeneralTools
|
||
{
|
||
public class SocketBase
|
||
{
|
||
protected string socketName = "";
|
||
protected static int socketCount = 0;
|
||
protected bool socketClose = false;
|
||
protected Queue<SocketMsg> receiveMsgQueue = new Queue<SocketMsg>();
|
||
protected char separator;
|
||
protected DebugMsgMode debugMsgMode;
|
||
protected Encoding encoding;
|
||
protected int reveiceByteSize;
|
||
protected float reConnectedTime = 0;
|
||
protected float nowTime = 0;
|
||
protected event Action<string> receiveStringListeners;
|
||
protected event Action<byte[]> receiveByteListeners;
|
||
protected event Action<SocketMsg> receiveMsgListeners;
|
||
public event Action onSocketQuit;
|
||
public SocketBase()
|
||
{
|
||
encoding = Encoding.UTF8;
|
||
reveiceByteSize = 1024;
|
||
//默认按照#分割收到消息
|
||
separator = '#';
|
||
//给一个默认的名字
|
||
socketCount += 1;
|
||
socketName = "Socket_" + socketCount;
|
||
receiveMsgListeners += DefultDebugMsg;
|
||
}
|
||
//设置参数
|
||
public virtual SocketBase SetSocketName(string _socketName)
|
||
{
|
||
socketName = _socketName;
|
||
return this;
|
||
}
|
||
public string GetSocketName()
|
||
{
|
||
return socketName;
|
||
}
|
||
/// <summary>
|
||
/// 设置编码格式
|
||
/// </summary>
|
||
/// <param name="_encoding"></param>
|
||
/// <returns></returns>
|
||
public virtual SocketBase SetMsgEncoding(Encoding _encoding)
|
||
{
|
||
encoding = _encoding;
|
||
//设置完成后清除消息
|
||
ClearAllQueue();
|
||
return this;
|
||
}
|
||
/// <summary>
|
||
/// 设置分割符,值为char.MinValue时不分割
|
||
/// </summary>
|
||
/// <param name="_separator"></param>
|
||
/// <returns></returns>
|
||
public virtual SocketBase SetSeparator(char _separator)
|
||
{
|
||
separator = _separator;
|
||
//设置完成后清除消息
|
||
ClearAllQueue();
|
||
return this;
|
||
}
|
||
/// <summary>
|
||
/// 设置Debug消息模式
|
||
/// </summary>
|
||
/// <param name="mode"></param>
|
||
/// <returns></returns>
|
||
public virtual SocketBase SetDebugMsgMode(DebugMsgMode mode)
|
||
{
|
||
debugMsgMode = mode;
|
||
return this;
|
||
}
|
||
public SocketBase SetReconnectedTime(float time)
|
||
{
|
||
reConnectedTime = time;
|
||
return this;
|
||
}
|
||
public SocketBase SetNowTime(float _nowTime)
|
||
{
|
||
nowTime = _nowTime;
|
||
return this;
|
||
}
|
||
public SocketBase SetReceiveByteSize(int size)
|
||
{
|
||
reveiceByteSize = size;
|
||
return this;
|
||
}
|
||
/// <summary>
|
||
/// 添加接收消息事件
|
||
/// </summary>
|
||
/// <param name="receiveAction"></param>
|
||
public SocketBase AddReceiveEvent(Action<string> receiveAction)
|
||
{
|
||
if (receiveAction != null)
|
||
receiveStringListeners += receiveAction;
|
||
return this;
|
||
}
|
||
public SocketBase AddReceiveEvent(Action<byte[]> receiveAction)
|
||
{
|
||
if (receiveAction != null)
|
||
receiveByteListeners += receiveAction;
|
||
return this;
|
||
}
|
||
public SocketBase AddReceiveEvent(Action<SocketMsg> receiveAction)
|
||
{
|
||
if (receiveAction != null)
|
||
receiveMsgListeners += receiveAction;
|
||
return this;
|
||
}
|
||
/// <summary>
|
||
/// 移除接收消息事件
|
||
/// </summary>
|
||
/// <param name="receiveAction"></param>
|
||
public SocketBase RemoveReceiveEvent(Action<string> receiveAction)
|
||
{
|
||
if (receiveAction != null)
|
||
receiveStringListeners -= receiveAction;
|
||
return this;
|
||
}
|
||
public SocketBase RemoveReceiveEvent(Action<byte[]> receiveAction)
|
||
{
|
||
if (receiveAction != null)
|
||
receiveByteListeners -= receiveAction;
|
||
return this;
|
||
}
|
||
public SocketBase RemoveReceiveEvent(Action<SocketMsg> receiveAction)
|
||
{
|
||
if (receiveAction != null)
|
||
receiveMsgListeners -= receiveAction;
|
||
return this;
|
||
}
|
||
//移除所有事件
|
||
public SocketBase RemoveAllReceiveEvent()
|
||
{
|
||
receiveMsgListeners = null;
|
||
receiveByteListeners = null;
|
||
receiveStringListeners = null;
|
||
return this;
|
||
}
|
||
//获得接收到的消息
|
||
protected virtual List<SocketMsg> GetReceiveMsg(int count = -1)
|
||
{
|
||
int getCount = 0;
|
||
List<SocketMsg> temp = new List<SocketMsg>();
|
||
lock (receiveMsgQueue)
|
||
{
|
||
while (receiveMsgQueue.Count > 0)
|
||
{
|
||
SocketMsg newMsg = receiveMsgQueue.Dequeue();
|
||
if (newMsg == null)
|
||
continue;
|
||
temp.Add(newMsg);
|
||
getCount++;
|
||
if (getCount == count)
|
||
break;
|
||
}
|
||
}
|
||
return temp;
|
||
}
|
||
public void DoReceiveEvent()
|
||
{
|
||
List<SocketMsg> msg = GetReceiveMsg();
|
||
foreach (var item in msg)
|
||
{
|
||
if (receiveMsgListeners != null)
|
||
{
|
||
receiveMsgListeners(item);
|
||
}
|
||
if (receiveStringListeners != null)
|
||
{
|
||
string receiveMsg = item.msg;
|
||
string[] msgArr = receiveMsg.Split(separator);
|
||
for (int i = 0; i < msgArr.Length; i++)
|
||
{
|
||
if (!string.IsNullOrEmpty(msgArr[i]))
|
||
receiveStringListeners(msgArr[i]);
|
||
}
|
||
}
|
||
if (receiveByteListeners != null)
|
||
{
|
||
receiveByteListeners(item.bytes);
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 添加接收消息事件
|
||
/// </summary>
|
||
/// <param name="receiveAction"></param>
|
||
protected void DefultDebugMsg(SocketMsg msg)
|
||
{
|
||
if (ConfigHelper.debugSocketMsgMode == 0)
|
||
return;
|
||
if (debugMsgMode == DebugMsgMode.none)
|
||
return;
|
||
string debugStr = "";
|
||
if (debugMsgMode == DebugMsgMode.stringMsg)
|
||
{
|
||
debugStr = msg.msg;
|
||
}
|
||
else if (debugMsgMode == DebugMsgMode.byteMsgDec)
|
||
{
|
||
foreach (var b in msg.bytes)
|
||
{
|
||
debugStr += b.ToString() + " ";
|
||
}
|
||
}
|
||
else if (debugMsgMode == DebugMsgMode.byteMsgHex)
|
||
{
|
||
foreach (var b in msg.bytes)
|
||
{
|
||
debugStr += b.ToString("X2") + " ";
|
||
}
|
||
}
|
||
if (ConfigHelper.debugSocketMsgMode == 1)
|
||
{
|
||
Debug.Log("Receive:" + debugStr);
|
||
}
|
||
else if (ConfigHelper.debugSocketMsgMode == 2)
|
||
{
|
||
Debug.Log("Receive:" + msg.createTime.ToString("F2") + ":" + msg.socketName + ":" + msg.remoteIpEndPoint + ":" + debugStr);
|
||
}
|
||
}
|
||
protected void ClearAllQueue()
|
||
{
|
||
receiveMsgQueue.Clear();
|
||
}
|
||
public virtual void SocketQuit()
|
||
{
|
||
socketClose = true;
|
||
onSocketQuit?.Invoke();
|
||
}
|
||
protected static string Bytes2String(byte[] bytes, Encoding _encoding)
|
||
{
|
||
if (_encoding == null)
|
||
_encoding = Encoding.UTF8;
|
||
return _encoding.GetString(bytes);
|
||
}
|
||
protected static byte[] String2Bytes(string str, Encoding _encoding)
|
||
{
|
||
if (_encoding == null)
|
||
_encoding = Encoding.UTF8;
|
||
return _encoding.GetBytes(str);
|
||
}
|
||
protected virtual void RecordReceiveMsg(byte[] receiveByte, int length, EndPoint _localEndPoint, EndPoint _remoteEndPoint)
|
||
{
|
||
byte[] newByte = receiveByte.Skip(0).Take(length).ToArray();
|
||
SocketMsg newMsg = new SocketMsg();
|
||
newMsg.createTime = nowTime;
|
||
newMsg.localIpEndPoint = (IPEndPoint)_localEndPoint;
|
||
newMsg.remoteIpEndPoint = (IPEndPoint)_remoteEndPoint;
|
||
newMsg.socketName = socketName;
|
||
newMsg.bytes = newByte;
|
||
newMsg.msg = Bytes2String(newByte, encoding);
|
||
lock (receiveMsgQueue)
|
||
{
|
||
receiveMsgQueue.Enqueue(newMsg);
|
||
}
|
||
}
|
||
//发送消息
|
||
public virtual void SendMsg(string strMsg)
|
||
{
|
||
|
||
}
|
||
public virtual void SendMsg(byte[] byteMsg)
|
||
{
|
||
|
||
}
|
||
public virtual void SendMsg(IPEndPoint remote, string strMsg)
|
||
{
|
||
|
||
}
|
||
public virtual void SendMsg(IPEndPoint remote, byte[] byteMsg)
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
public class SocketMsg
|
||
{
|
||
public float createTime;
|
||
public string socketName;
|
||
public IPEndPoint localIpEndPoint;
|
||
public IPEndPoint remoteIpEndPoint;
|
||
public Encoding encoding;
|
||
public byte[] bytes;
|
||
public string msg;
|
||
|
||
public SocketMsg()
|
||
{
|
||
localIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||
remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||
socketName = "";
|
||
encoding = Encoding.UTF8;
|
||
bytes = new byte[0];
|
||
}
|
||
}
|
||
public enum DebugMsgMode
|
||
{
|
||
stringMsg = 0,
|
||
byteMsgDec = 1,
|
||
byteMsgHex = 2,
|
||
none = 99,
|
||
}
|
||
} |