108 lines
3.1 KiB
C#
108 lines
3.1 KiB
C#
|
|
using System.Net;
|
|||
|
|
using System.Net.Sockets;
|
|||
|
|
using System.Text;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
public class NetAsyncMgr : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
private static NetAsyncMgr instance;
|
|||
|
|
public static NetAsyncMgr Instance => instance;
|
|||
|
|
//<2F>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD> Socket
|
|||
|
|
private Socket socket;
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>õ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
private byte[] cacheBytes = new byte[1024];
|
|||
|
|
[HideInInspector]
|
|||
|
|
public string receMsg;
|
|||
|
|
void Awake()
|
|||
|
|
{
|
|||
|
|
instance = this;
|
|||
|
|
}
|
|||
|
|
//<2F><><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD>
|
|||
|
|
public void Connect(string ip, int port)
|
|||
|
|
{
|
|||
|
|
if (socket != null && socket.Connected)
|
|||
|
|
return;
|
|||
|
|
|
|||
|
|
IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(ip), port);
|
|||
|
|
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|||
|
|
|
|||
|
|
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
|||
|
|
args.RemoteEndPoint = ipPoint;
|
|||
|
|
args.Completed += (socket, args) =>
|
|||
|
|
{
|
|||
|
|
if(args.SocketError == SocketError.Success)
|
|||
|
|
{
|
|||
|
|
Debug.Log("<22><><EFBFBD>ӳɹ<D3B3>");
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs();
|
|||
|
|
receiveArgs.SetBuffer(cacheBytes, 0, cacheBytes.Length);
|
|||
|
|
receiveArgs.Completed += ReceiveCallBack;
|
|||
|
|
this.socket.ReceiveAsync(receiveArgs);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Debug.Log("<22><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>" + args.SocketError);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
socket.ConnectAsync(args);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD>ɵĻص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
private void ReceiveCallBack(object obj, SocketAsyncEventArgs args)
|
|||
|
|
{
|
|||
|
|
if(args.SocketError == SocketError.Success)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ Ŀǰ<C4BF>õ<EFBFBD><C3B5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
Debug.Log(Encoding.UTF8.GetString(args.Buffer, 0, args.BytesTransferred));
|
|||
|
|
receMsg = Encoding.UTF8.GetString(args.Buffer, 0, args.BytesTransferred);
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
args.SetBuffer(0, args.Buffer.Length);
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
if (this.socket != null && this.socket.Connected)
|
|||
|
|
socket.ReceiveAsync(args);
|
|||
|
|
else
|
|||
|
|
Close();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>" + args.SocketError);
|
|||
|
|
//<2F>رտͻ<D5BF><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
Close();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Close()
|
|||
|
|
{
|
|||
|
|
if(socket != null)
|
|||
|
|
{
|
|||
|
|
socket.Shutdown(SocketShutdown.Both);
|
|||
|
|
socket.Disconnect(false);
|
|||
|
|
socket.Close();
|
|||
|
|
socket = null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Send(string str)
|
|||
|
|
{
|
|||
|
|
if(this.socket != null && this.socket.Connected)
|
|||
|
|
{
|
|||
|
|
byte[] bytes = Encoding.UTF8.GetBytes(str);
|
|||
|
|
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
|
|||
|
|
args.SetBuffer(bytes, 0, bytes.Length);
|
|||
|
|
args.Completed += (socket, args) =>
|
|||
|
|
{
|
|||
|
|
if (args.SocketError != SocketError.Success)
|
|||
|
|
{
|
|||
|
|
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣʧ<CFA2><CAA7>" + args.SocketError);
|
|||
|
|
Close();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
};
|
|||
|
|
this.socket.SendAsync(args);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Close();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|