/// <summary>
/// Create a connection to a remote endpoint
/// </summary>
public virtual NetConnection Connect(NetEndPoint remoteEndPoint, NetOutgoingMessage hailMessage)
{
if (remoteEndPoint == null)
{
throw new ArgumentNullException("remoteEndPoint");
}
if (m_configuration.DualStack)
{
remoteEndPoint = NetUtility.MapToIPv6(remoteEndPoint);
}
lock (m_connections)
{
if (m_status == NetPeerStatus.NotRunning)
{
throw new NetException("Must call Start() first");
}
if (m_connectionLookup.ContainsKey(remoteEndPoint))
{
throw new NetException("Already connected to that endpoint!");
}
NetConnection hs;
if (m_handshakes.TryGetValue(remoteEndPoint, out hs))
{
// already trying to connect to that endpoint; make another try
switch (hs.m_status)
{
case NetConnectionStatus.InitiatedConnect:
// send another connect
hs.m_connectRequested = true;
break;
case NetConnectionStatus.RespondedConnect:
// send another response
hs.SendConnectResponse(NetTime.Now, false);
break;
default:
// weird
LogWarning("Weird situation; Connect() already in progress to remote endpoint; but hs status is " + hs.m_status);
break;
}
return(hs);
}
NetConnection conn = new NetConnection(this, remoteEndPoint);
conn.SetStatus(NetConnectionStatus.InitiatedConnect, "user called connect");
conn.m_localHailMessage = hailMessage;
// handle on network thread
conn.m_connectRequested = true;
conn.m_connectionInitiator = true;
m_handshakes.Add(remoteEndPoint, conn);
return(conn);
}
}