/// <summary> /// Create a new connection object /// </summary> /// <param name="connectionInfo">ConnecitonInfo corresponding to the new connection</param> /// <param name="defaultSendReceiveOptions">The SendReceiveOptions which should be used as connection defaults</param> protected Connection(ConnectionInfo connectionInfo, SendReceiveOptions defaultSendReceiveOptions) { SendTimesMSPerKBCache = new CommsMath(); dataBuffer = new byte[NetworkComms.ReceiveBufferSizeBytes]; packetBuilder = new PacketBuilder(); //Intialise the sequence counter using the global value //Subsequent values on this connection are guaranteed to be sequential packetSequenceCounter = Interlocked.Increment(ref NetworkComms.totalPacketSendCount); ConnectionInfo = connectionInfo; if (defaultSendReceiveOptions != null) { ConnectionDefaultSendReceiveOptions = defaultSendReceiveOptions; } else { ConnectionDefaultSendReceiveOptions = NetworkComms.DefaultSendReceiveOptions; } if (NetworkComms.commsShutdown) { throw new ConnectionSetupException("Attempting to create new connection after global comms shutdown has been initiated."); } if (ConnectionInfo.ConnectionType == ConnectionType.Undefined || ConnectionInfo.RemoteEndPoint == null) { throw new ConnectionSetupException("ConnectionType and RemoteEndPoint must be defined within provided ConnectionInfo."); } //If a connection already exists with this info then we can throw an exception here to prevent duplicates if (NetworkComms.ConnectionExists(connectionInfo.RemoteEndPoint, connectionInfo.ConnectionType)) { throw new ConnectionSetupException("A connection already exists with " + ConnectionInfo); } //We add a reference in the constructor to ensure any duplicate connection problems are picked up here NetworkComms.AddConnectionByReferenceEndPoint(this); }