Ejemplo n.º 1
0
        /// <summary>
        /// Create a new connection object
        /// </summary>
        /// <param name="connectionInfo">ConnectionInfo 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)
        {
            //If the application layer protocol is disabled the serialiser must be NullSerializer
            //and no data processors are allowed.
            if (connectionInfo.ApplicationLayerProtocol == ApplicationLayerProtocolStatus.Disabled)
            {
                if (defaultSendReceiveOptions.Options.ContainsKey("ReceiveConfirmationRequired"))
                {
                    throw new ArgumentException("Attempted to create an unmanaged connection when the provided send receive" +
                                                " options specified the ReceiveConfirmationRequired option. Please provide compatible send receive options in order to successfully" +
                                                " instantiate this unmanaged connection.", "defaultSendReceiveOptions");
                }

                if (defaultSendReceiveOptions.DataSerializer != DPSManager.GetDataSerializer <NullSerializer>())
                {
                    throw new ArgumentException("Attempted to create an unmanaged connection when the provided send receive" +
                                                " options serialiser was not NullSerializer. Please provide compatible send receive options in order to successfully" +
                                                " instantiate this unmanaged connection.", "defaultSendReceiveOptions");
                }

                if (defaultSendReceiveOptions.DataProcessors.Count > 0)
                {
                    throw new ArgumentException("Attempted to create an unmanaged connection when the provided send receive" +
                                                " options contains data processors. Data processors may not be used with unmanaged connections." +
                                                " Please provide compatible send receive options in order to successfully instantiate this unmanaged connection.", "defaultSendReceiveOptions");
                }
            }

            SendTimesMSPerKBCache = new CommsMath();
            packetBuilder         = new PacketBuilder();

            //Initialise 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;
            }

            //Add any listener specific packet handlers if required
            if (connectionInfo.ConnectionListener != null)
            {
                connectionInfo.ConnectionListener.AddListenerPacketHandlersToConnection(this);
            }

            if (NetworkComms.commsShutdown)
            {
                throw new ConnectionSetupException("Attempting to create new connection after global NetworkComms.Net 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.LocalEndPoint, connectionInfo.ConnectionType, connectionInfo.ApplicationLayerProtocol))
            {
                throw new ConnectionSetupException("A " + connectionInfo.ConnectionType.ToString() + " connection already exists with info " + connectionInfo);
            }

            //We add a reference in the constructor to ensure any duplicate connection problems are picked up here
            NetworkComms.AddConnectionReferenceByRemoteEndPoint(this);
        }