Files
JJBB/Assets/Project/Script/GameLogic/NetWork/SocketAPI/Socket.cs

93 lines
2.3 KiB
C#
Raw Normal View History

2024-08-23 15:49:34 +08:00
/********************************************************************************
* Socket.cs
* \NetWork\SocketAPI\Socket.cs
*
* 2013-11-29
*
* Socket类
*
*
*********************************************************************************/
#if UNITY_WP8
using UnityPortSocket;
#else
using System.Net.Sockets;
#endif
using System.Net;
using SPacket.Socket_API;
namespace SPacket.SocketInstance
{
public class SocketInstance
{
private string m_host;
private int m_port;
private Socket m_SocketID;
public bool IsValid
{
get { return m_SocketID != null; }
}
public bool IsConnected
{
get { return m_SocketID != null && m_SocketID.Connected; }
}
public uint Send(byte[] buf, int nLen, SocketFlags flags = SocketFlags.None)
{
return SocketAPI.Send(m_SocketID, buf, (uint) nLen, flags);
}
public uint receive(byte[] buf, int nLen, uint flags = 0)
{
return SocketAPI.Recv(m_SocketID, buf, (uint) nLen, flags);
}
public void close()
{
if (IsValid)
{
SocketAPI.Close(m_SocketID);
m_SocketID = null;
}
}
public EndPoint GetLocalEndPoint()
{
return m_SocketID == null ? null : m_SocketID.LocalEndPoint;
}
public uint available()
{
return SocketAPI.available(m_SocketID);
}
public string connect(string IP, int port)
{
m_host = IP;
m_port = port;
var results = "";
m_SocketID = SocketAPI.Connect(m_host, m_port, ref results);
return results;
}
public bool IsCanSend()
{
if (m_SocketID != null)
if (m_SocketID.Poll(0, SelectMode.SelectWrite))
return true;
return false;
}
public bool IsCanReceive()
{
if (m_SocketID != null)
if (m_SocketID.Poll(0, SelectMode.SelectRead))
return true;
return false;
}
}
}