Update to latest stuff from death

This commit is contained in:
Lucas Nicodemus 2016-12-06 20:34:12 -07:00
parent 678f6c7cf8
commit e4313b6433
No known key found for this signature in database
GPG key ID: CEE668CCE1BF2C7C
4 changed files with 192 additions and 4 deletions

View file

@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Terraria;
using Terraria.Localization;
using Terraria.Net;
using Terraria.Net.Sockets;
namespace TShockAPI.Sockets
{
public class LinuxTcpSocket : ISocket
{
private byte[] _packetBuffer = new byte[1024];
private int _packetBufferLength;
private List<object> _callbackBuffer = new List<object>();
private int _messagesInQueue;
private TcpClient _connection;
private TcpListener _listener;
private SocketConnectionAccepted _listenerCallback;
private RemoteAddress _remoteAddress;
private bool _isListening;
public int MessagesInQueue
{
get
{
return this._messagesInQueue;
}
}
public LinuxTcpSocket()
{
this._connection = new TcpClient();
this._connection.NoDelay = true;
}
public LinuxTcpSocket(TcpClient tcpClient)
{
this._connection = tcpClient;
this._connection.NoDelay = true;
IPEndPoint iPEndPoint = (IPEndPoint)tcpClient.Client.RemoteEndPoint;
this._remoteAddress = new TcpAddress(iPEndPoint.Address, iPEndPoint.Port);
}
void ISocket.Close()
{
this._remoteAddress = null;
this._connection.Close();
}
bool ISocket.IsConnected()
{
return this._connection != null && this._connection.Client != null && this._connection.Connected;
}
void ISocket.Connect(RemoteAddress address)
{
TcpAddress tcpAddress = (TcpAddress)address;
this._connection.Connect(tcpAddress.Address, tcpAddress.Port);
this._remoteAddress = address;
}
private void ReadCallback(IAsyncResult result)
{
Tuple<SocketReceiveCallback, object> tuple = (Tuple<SocketReceiveCallback, object>)result.AsyncState;
tuple.Item1(tuple.Item2, this._connection.GetStream().EndRead(result));
}
private void SendCallback(IAsyncResult result)
{
object[] expr_0B = (object[])result.AsyncState;
LegacyNetBufferPool.ReturnBuffer((byte[])expr_0B[1]);
Tuple<SocketSendCallback, object> tuple = (Tuple<SocketSendCallback, object>)expr_0B[0];
try
{
this._connection.GetStream().EndWrite(result);
tuple.Item1(tuple.Item2);
}
catch (Exception)
{
((ISocket)this).Close();
}
}
void ISocket.SendQueuedPackets()
{
}
void ISocket.AsyncSend(byte[] data, int offset, int size, SocketSendCallback callback, object state)
{
byte[] array = LegacyNetBufferPool.RequestBuffer(data, offset, size);
this._connection.GetStream().BeginWrite(array, 0, size, new AsyncCallback(this.SendCallback), new object[]
{
new Tuple<SocketSendCallback, object>(callback, state),
array
});
}
void ISocket.AsyncReceive(byte[] data, int offset, int size, SocketReceiveCallback callback, object state)
{
this._connection.GetStream().BeginRead(data, offset, size, new AsyncCallback(this.ReadCallback), new Tuple<SocketReceiveCallback, object>(callback, state));
}
bool ISocket.IsDataAvailable()
{
return this._connection.GetStream().DataAvailable;
}
RemoteAddress ISocket.GetRemoteAddress()
{
return this._remoteAddress;
}
bool ISocket.StartListening(SocketConnectionAccepted callback)
{
IPAddress any = IPAddress.Any;
string ipString;
if (Program.LaunchParameters.TryGetValue("-ip", out ipString) && !IPAddress.TryParse(ipString, out any))
{
any = IPAddress.Any;
}
this._isListening = true;
this._listenerCallback = callback;
if (this._listener == null)
{
this._listener = new TcpListener(any, Netplay.ListenPort);
}
try
{
this._listener.Start();
}
catch (Exception)
{
return false;
}
ThreadPool.QueueUserWorkItem(new WaitCallback(this.ListenLoop));
return true;
}
void ISocket.StopListening()
{
this._isListening = false;
}
private void ListenLoop(object unused)
{
while (this._isListening && !Netplay.disconnect)
{
try
{
Console.WriteLine("Creating linux socket");
ISocket socket = new LinuxTcpSocket(this._listener.AcceptTcpClient());
Console.WriteLine(Language.GetTextValue("Net.ClientConnecting", socket.GetRemoteAddress()));
this._listenerCallback(socket);
}
catch (Exception)
{
}
}
this._listener.Stop();
}
}
}

View file

@ -40,6 +40,7 @@ using TShockAPI.Hooks;
using TShockAPI.ServerSideCharacters;
using Terraria.Utilities;
using Microsoft.Xna.Framework;
using TShockAPI.Sockets;
namespace TShockAPI
{
@ -184,6 +185,19 @@ namespace TShockAPI
string logFilename;
string logPathSetupWarning;
OTAPI.Hooks.Net.Socket.Create = () =>
{
//Console.WriteLine($"Creating socket {nameof(LinuxTcpSocket)}");
return new LinuxTcpSocket();
//return new OTAPI.Sockets.PoolSocket();
//return new Terraria.Net.Sockets.TcpSocket();
};
OTAPI.Hooks.Player.Announce = (int playerId) =>
{
//TShock handles this
return OTAPI.HookResult.Cancel;
};
TerrariaApi.Reporting.CrashReporter.HeapshotRequesting += CrashReporter_HeapshotRequesting;
try

View file

@ -68,8 +68,8 @@
<HintPath>..\packages\MySql.Data.6.9.8\lib\net45\MySql.Data.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="OTAPI">
@ -95,6 +95,7 @@
<Compile Include="ILog.cs" />
<Compile Include="NetItem.cs" />
<Compile Include="PlayerData.cs" />
<Compile Include="Sockets\LinuxTcpSocket.cs" />
<Compile Include="SqlLog.cs" />
<Compile Include="TextLog.cs" />
<Compile Include="PaginationTools.cs" />

View file

@ -2,5 +2,5 @@
<packages>
<package id="BCrypt.Net" version="0.1.0" targetFramework="net45" />
<package id="MySql.Data" version="6.9.8" targetFramework="net45" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net45" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
</packages>