Skip to content
This repository has been archived by the owner on Aug 15, 2022. It is now read-only.

Refactor Connect() - give more control to developers #364

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class TCPClientBase : BaseTCP, IClient
protected NetworkingPlayer server = null;
public NetworkingPlayer ServerPlayer { get { return server; } }

public virtual void Connect(string host, ushort port = DEFAULT_PORT)
public virtual bool Connect(string host, ushort port = DEFAULT_PORT)
{
if (Disposed)
throw new ObjectDisposedException("TCPClient", "This object has been disposed and can not be used to connect, please use a new TCPClient");
Expand All @@ -57,11 +57,13 @@ public virtual void Connect(string host, ushort port = DEFAULT_PORT)
{
connectAttemptFailed(this);
}
return;
return false;
}
// If we got this far then the bind was successful
OnBindSuccessful();
Initialize(host, port);

return true;
}
protected virtual void Initialize(string host, ushort port, bool pendCreates = true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public class TCPClientWebsockets : TCPClientBase
[DllImport("__Internal")]
private static extern bool CheckSocketConnection();

public override void Connect(string host, ushort port = DEFAULT_PORT)
public override bool Connect(string host, ushort port = DEFAULT_PORT)
{
//Set the port
SetPort(port);

ForgeConnect(host, port);

return true;
}

public void CheckConnection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ private void CreateTheNetworkingPlayer(string host, ushort port)
}
}

private void BindAndConnect(ushort overrideBindingPort, ushort port, string natHost, string host, ushort natPort)
private bool BindAndConnect(ushort overrideBindingPort, ushort port, string natHost, string host, ushort natPort)
{
SetNetworkBindings(overrideBindingPort, port, natHost, host, natPort);
CreateTheNetworkingPlayer(host, port);
SetupConnectingState();
ThreadPool.QueueUserWorkItem(AttemptServerConnection);
return ThreadPool.QueueUserWorkItem(AttemptServerConnection);
}

/// <summary>
Expand All @@ -183,7 +183,7 @@ private void BindAndConnect(ushort overrideBindingPort, ushort port, string natH
/// <param name="natHost">The NAT server host address, if blank NAT will be skipped</param>
/// <param name="natPort">The port that the NAT server is hosting on</param>
/// <param name="pendCreates">Immidiately set the NetWorker::PendCreates to true</param>
public void Connect(string host, ushort port = DEFAULT_PORT, string natHost = "",
public bool Connect(string host, ushort port = DEFAULT_PORT, string natHost = "",
ushort natPort = NatHolePunch.DEFAULT_NAT_SERVER_PORT, bool pendCreates = false,
ushort overrideBindingPort = DEFAULT_PORT + 1)
{
Expand All @@ -199,7 +199,7 @@ private void BindAndConnect(ushort overrideBindingPort, ushort port, string natH

try
{
BindAndConnect(overrideBindingPort, port, natHost, host, natPort);
return BindAndConnect(overrideBindingPort, port, natHost, host, natPort);
}
catch (Exception e)
{
Expand Down