mirror of
https://github.com/islehorse/HISP.git
synced 2025-04-06 05:05:40 +12:00
104 lines
3 KiB
C#
104 lines
3 KiB
C#
using System;
|
|
using System.Net.Sockets;
|
|
|
|
namespace HISP.Server.Network
|
|
{
|
|
public class Hybrid : Transport
|
|
{
|
|
Transport actualTransport = null;
|
|
|
|
|
|
public override string Name
|
|
{
|
|
get
|
|
{
|
|
if(actualTransport == null)
|
|
return "TransportDeterminer";
|
|
else
|
|
return actualTransport.Name;
|
|
}
|
|
}
|
|
|
|
|
|
public override bool Disconnected
|
|
{
|
|
get
|
|
{
|
|
if (actualTransport == null)
|
|
return base.Disconnected;
|
|
else
|
|
return actualTransport.Disconnected;
|
|
}
|
|
}
|
|
|
|
public override string Ip
|
|
{
|
|
get
|
|
{
|
|
if (actualTransport == null)
|
|
return base.Ip;
|
|
else
|
|
return actualTransport.Ip;
|
|
}
|
|
}
|
|
|
|
|
|
public override void Disconnect()
|
|
{
|
|
if (actualTransport == null)
|
|
{
|
|
base.Disconnect();
|
|
}
|
|
else
|
|
{
|
|
actualTransport.Disconnect();
|
|
}
|
|
}
|
|
|
|
public override void ProcessReceivedPackets(int available, byte[] buffer)
|
|
{
|
|
|
|
if (ConfigReader.EnableWebSocket && WebSocket.IsStartOfHandshake(buffer))
|
|
{
|
|
Logger.InfoPrint(this.Ip + " Switching to WebSocket");
|
|
actualTransport = new WebSocket();
|
|
|
|
actualTransport.passObjects(this.socket, this.onReceiveCallback, this.onDisconnectCallback);
|
|
actualTransport.ProcessReceivedPackets(available, buffer);
|
|
actualTransport.Accept(base.socket, base.onReceiveCallback, base.onDisconnectCallback);
|
|
}
|
|
else
|
|
{
|
|
Logger.InfoPrint(this.Ip + " Switching to XmlSocket");
|
|
actualTransport = new XmlSocket();
|
|
|
|
actualTransport.passObjects(this.socket, this.onReceiveCallback, this.onDisconnectCallback);
|
|
actualTransport.ProcessReceivedPackets(available, buffer);
|
|
actualTransport.Accept(base.socket, base.onReceiveCallback, base.onDisconnectCallback);
|
|
}
|
|
}
|
|
|
|
internal override void receivePackets(object sender, SocketAsyncEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if(base.checkForError(e)) return;
|
|
ProcessReceivedPackets(e.BytesTransferred, e.Buffer);
|
|
if (base.checkForError(e)) return;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.ErrorPrint(ex.StackTrace);
|
|
try { this.Disconnect(); } catch (Exception) { };
|
|
};
|
|
}
|
|
|
|
public override void Send(byte[] data)
|
|
{
|
|
if(actualTransport == null)
|
|
base.Send(data);
|
|
else
|
|
actualTransport.Send(data);
|
|
}
|
|
}
|
|
}
|