Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions ServerPortals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
using ServerPortals.Tiles;
using System;
using System.Threading;
using System.Net.NetworkInformation;
using System.Text;
using System.Net.Sockets;
using Ionic.Zlib;
using System.Reflection;
using System.Windows.Input;
Expand Down Expand Up @@ -102,37 +101,31 @@ public void SetServerInfo(string serverIP, int serverPort, string serverName = "

private void ConnectToServerIP(object threadContext)
{
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;

string data = "a";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 1000;

PingReply reply = null;
try
{
reply = pingSender.Send(Netplay.ServerIP, timeout, buffer, options);
using (TcpClient client = new TcpClient())
{
var connectTask = client.ConnectAsync(Netplay.ServerIP, Netplay.ListenPort);
if (!connectTask.Wait(1000) || !client.Connected)
{
Main.NewText("Could not reach destination server!");
ServerPortalTileEntity.ServerSelectLock = false;
return;
}
}
}
catch
{
Main.NewText("Could not ping destination server!");
Main.NewText("Could not reach destination server!");
ServerPortalTileEntity.ServerSelectLock = false;
return;
}

if (reply.Status == IPStatus.Success)
WorldGen.SaveAndQuit(() =>
{
WorldGen.SaveAndQuit(() =>
{
Main.menuMode = 10;
Netplay.StartTcpClient();
});
}
else
{
Main.NewText("Ping to server timed out!");
}
Main.menuMode = 10;
Netplay.StartTcpClient();
});

ServerPortalTileEntity.ServerSelectLock = false;
}
Expand Down Expand Up @@ -303,4 +296,4 @@ public enum MessageType : byte
ClientSendPortalPlacement
}
}
}
}
41 changes: 17 additions & 24 deletions Tiles/ServerPortalTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
Expand Down Expand Up @@ -104,37 +103,31 @@ public override void MouseOverFar(int i, int j)

private void ConnectToServerIP(object threadContext)
{
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;

string data = "a";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 1000;

PingReply reply = null;
try
{
reply = pingSender.Send(Netplay.ServerIP, timeout, buffer, options);
using (TcpClient client = new TcpClient())
{
var connectTask = client.ConnectAsync(Netplay.ServerIP, Netplay.ListenPort);
if (!connectTask.Wait(1000) || !client.Connected)
{
Main.NewText("Could not reach destination server!");
ServerPortalTileEntity.ServerSelectLock = false;
return;
}
}
}
catch
{
Main.NewText("Could not ping destination server!");
Main.NewText("Could not reach destination server!");
ServerPortalTileEntity.ServerSelectLock = false;
return;
}

if (reply.Status == IPStatus.Success)
{
WorldGen.SaveAndQuit(() =>
{
Main.menuMode = 10;
Netplay.StartTcpClient();
});
}
else
WorldGen.SaveAndQuit(() =>
{
Main.NewText("Ping to server timed out!");
}
Main.menuMode = 10;
Netplay.StartTcpClient();
});

ServerPortalTileEntity.ServerSelectLock = false;
}
Expand Down