204 lines
4.0 KiB
C#
204 lines
4.0 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class GISocket
|
|
{
|
|
public static GISocket Instance { get; private set; }
|
|
|
|
private bool webSockets = false;
|
|
private GIWebSocket websocket;
|
|
|
|
#if !UNITY_WEBGL
|
|
private System.Net.Sockets.TcpClient socket;
|
|
private System.Net.Sockets.NetworkStream stream;
|
|
#endif
|
|
|
|
private List<byte> _buffer = new List<byte>();
|
|
public bool closed = true;
|
|
|
|
public GISocket(bool webSockets)
|
|
{
|
|
Instance = this;
|
|
this.webSockets = webSockets;
|
|
|
|
if (webSockets)
|
|
{
|
|
websocket = new GIWebSocket();
|
|
}
|
|
#if !UNITY_WEBGL
|
|
else
|
|
{
|
|
socket = new System.Net.Sockets.TcpClient();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
public bool connect(string serverIP)
|
|
{
|
|
if (webSockets)
|
|
{
|
|
websocket.connect(serverIP, 3194);
|
|
closed = false;
|
|
return true;
|
|
}
|
|
|
|
#if !UNITY_WEBGL
|
|
try
|
|
{
|
|
var ipAddress = System.Net.IPAddress.Parse(serverIP);
|
|
var endPoint = new System.Net.IPEndPoint(ipAddress, 3194);
|
|
socket.Connect(endPoint);
|
|
stream = socket.GetStream();
|
|
socket.ReceiveBufferSize = 100000;
|
|
closed = false;
|
|
return true;
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Debug.LogError("TCP Connection error: " + e.Message);
|
|
return false;
|
|
}
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (webSockets)
|
|
{
|
|
websocket.Update();
|
|
}
|
|
}
|
|
|
|
public void readInputStream(ref byte[] data, int size)
|
|
{
|
|
if (webSockets)
|
|
{
|
|
websocket.readInputStream(ref data, size);
|
|
return;
|
|
}
|
|
|
|
#if !UNITY_WEBGL
|
|
if (closed || stream == null) return;
|
|
|
|
int bytesReceived = 0;
|
|
int offset;
|
|
|
|
while (bytesReceived < size)
|
|
{
|
|
offset = stream.Read(data, bytesReceived, size - bytesReceived);
|
|
if (offset <= 0)
|
|
throw new System.IO.IOException("EOF");
|
|
bytesReceived += offset;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
public void pushOutStream()
|
|
{
|
|
if (webSockets)
|
|
{
|
|
websocket.send(_buffer.ToArray());
|
|
}
|
|
#if !UNITY_WEBGL
|
|
else
|
|
{
|
|
send(_buffer.ToArray());
|
|
}
|
|
#endif
|
|
_buffer.Clear();
|
|
}
|
|
|
|
public void clearOutStream()
|
|
{
|
|
_buffer.Clear();
|
|
}
|
|
|
|
public void addToOutStream(byte[] packet)
|
|
{
|
|
_buffer.AddRange(packet);
|
|
}
|
|
|
|
public int available()
|
|
{
|
|
if (webSockets)
|
|
{
|
|
return websocket.available();
|
|
}
|
|
|
|
#if !UNITY_WEBGL
|
|
return closed ? 0 : socket.Available;
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
public void flush()
|
|
{
|
|
#if !UNITY_WEBGL
|
|
stream?.Flush();
|
|
#endif
|
|
}
|
|
|
|
public void close()
|
|
{
|
|
if (webSockets)
|
|
{
|
|
websocket.close();
|
|
closed = true;
|
|
return;
|
|
}
|
|
|
|
#if !UNITY_WEBGL
|
|
try
|
|
{
|
|
stream?.Close();
|
|
socket?.Close();
|
|
}
|
|
catch { }
|
|
|
|
closed = true;
|
|
#endif
|
|
|
|
Debug.Log("Session closed.");
|
|
}
|
|
|
|
#if !UNITY_WEBGL
|
|
private void send(byte[] packet)
|
|
{
|
|
if (closed || stream == null) return;
|
|
|
|
short size = (short)(packet.Length - 1);
|
|
byte[] payload = new byte[packet.Length + 2];
|
|
payload[0] = (byte)(size >> 8);
|
|
payload[1] = (byte)size;
|
|
|
|
for (int i = 0; i < packet.Length; i++)
|
|
{
|
|
payload[i + 2] = packet[i];
|
|
}
|
|
|
|
try
|
|
{
|
|
stream.Write(payload, 0, payload.Length);
|
|
stream.Flush();
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Debug.LogError("Send error: " + e.Message);
|
|
close();
|
|
}
|
|
}
|
|
#endif
|
|
|
|
public bool isClosed()
|
|
{
|
|
if (webSockets)
|
|
{
|
|
return websocket == null || websocket.isClosed();
|
|
}
|
|
|
|
return closed;
|
|
}
|
|
} |