Exemplo n.º 1
0
        /// <summary>
        /// Starts listening to the specified end point and accepting incoming connections.
        /// </summary>
        /// <param name="endPoint">The end point.</param>
        public override void StartListening(object endPoint)
        {
            BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;

            this._closing = false;

            // LOG:
            if (binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0)
            {
                binaryLogWriter.WriteConnectionParameterEvent(LogCategory.Connection, "UdpConnectionManager.StartListening",
                                                              LogMessageType.ConnectionParameters, null, null, this.ITransportContext.IParameterProvider,
                                                              GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name, this.DbgConnectionId,
                                                              "UDP socket is being associated with the end point: {0}.", endPoint.ToString());
            }

            // get the ip end point
            IPEndPoint ipEndPoint = null;
            int        port;
            string     url;

            try
            {
                url        = (string)endPoint;
                url        = GenuineUtility.SplitToHostAndPort(url, out port);
                ipEndPoint = new IPEndPoint(GenuineUtility.ResolveIPAddress(url), port);
            }
            catch (Exception ex)
            {
                // LOG:
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0)
                {
                    binaryLogWriter.WriteEvent(LogCategory.Connection, "UdpConnectionManager.StartListening",
                                               LogMessageType.ListeningStarted, ex, null, null, null,
                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                               null, null, this.DbgConnectionId, 0, 0, 0, null, null, null, null,
                                               "The listening socket cannot be associated with the {0} local end point.", endPoint.ToString());
                }

                throw GenuineExceptions.Get_Server_IncorrectAddressToListen(endPoint as string);
            }

            lock (this)
            {
                if (this._socket != null)
                {
                    throw GenuineExceptions.Get_Server_EndPointIsAlreadyBeingListenedTo(this._socket.LocalEndPoint.ToString());
                }

                // initialize the socket
                this._socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                if (this.ITransportContext.IParameterProvider[GenuineParameter.UdpTtl] != null)
                {
                    this._socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, (int)this.ITransportContext.IParameterProvider[GenuineParameter.UdpTtl]);
                }

                // Receive buffer size
                this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, (int)this.ITransportContext.IParameterProvider[GenuineParameter.UdpReceiveBuffer]);
                this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);

                this._socket.Bind(ipEndPoint);

                // if it's an IP multicast sender
                if (this.ITransportContext.IParameterProvider[GenuineParameter.UdpMulticastTo] != null)
                {
                    try
                    {
                        url = (string)this.ITransportContext.IParameterProvider[GenuineParameter.UdpMulticastTo];
                        url = GenuineUtility.SplitToHostAndPort(url, out port);
                        this._multicastTo = new IPEndPoint(GenuineUtility.ResolveIPAddress(url), port);

                        this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
                        if (this.ITransportContext.IParameterProvider[GenuineParameter.UdpTtl] != null)
                        {
                            this._socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, (int)this.ITransportContext.IParameterProvider[GenuineParameter.UdpTtl]);
                        }
                    }
                    catch (Exception)
                    {
                        throw GenuineExceptions.Get_Channel_InvalidParameter("UdpMulticastTo");
                    }
                }

                // and join to the specified broadcast network
                if (this.ITransportContext.IParameterProvider[GenuineParameter.UdpJoinTo] != null)
                {
                    string joinTo = (string)this.ITransportContext.IParameterProvider[GenuineParameter.UdpJoinTo];
                    this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);

                    IPAddress       ipAddressToJoinTo = GenuineUtility.ResolveIPAddress(joinTo);
                    MulticastOption multicastOption   = new MulticastOption(ipAddressToJoinTo, ipEndPoint.Address);
                    this._socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, multicastOption);
                }

                // initiate receiving
                Thread receivingThread = new Thread(new ThreadStart(this.ReceiveSynchronously));
                receivingThread.IsBackground = true;
                receivingThread.Start();

                // LOG:
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0)
                {
                    binaryLogWriter.WriteEvent(LogCategory.Connection, "UdpConnectionManager.StartListening",
                                               LogMessageType.ConnectionEstablished, null, null, null, null,
                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                               null, null,
                                               this.DbgConnectionId, (int)GenuineConnectionType.Invocation, 0, 0, this.GetType().Name, this._socket.LocalEndPoint.ToString(), null, null,
                                               "The UDP socket is ready for action.");
                }
            }
        }
        /// <summary>
        /// Starts listening to the specified end point and accepting incoming connections.
        /// </summary>
        /// <param name="endPoint">The end point.</param>
        public override void StartListening(object endPoint)
        {
            BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;

            if (this._smAcceptConnectionClosure != null)
            {
                throw GenuineExceptions.Get_Server_EndPointIsAlreadyBeingListenedTo(this._smAcceptConnectionClosure.ShareName);
            }
            SharedMemoryConnection sharedMemoryConnection = null;

            using (new ReaderAutoLocker(this._disposeLock))
            {
                if (this._disposed)
                {
                    throw OperationException.WrapException(this._disposeReason);
                }
            }

            string shareName = endPoint as string;

            if (shareName == null || shareName.Length <= 0 || !shareName.StartsWith("gshmem"))
            {
                throw GenuineExceptions.Get_Server_IncorrectAddressToListen(shareName);
            }

            try
            {
                sharedMemoryConnection = new SharedMemoryConnection(this.ITransportContext, shareName, true, true);

                this._smAcceptConnectionClosure = new SMAcceptConnectionClosure(this.ITransportContext, sharedMemoryConnection, this, shareName);
                this.ITransportContext.IGenuineEventProvider.Fire(new GenuineEventArgs(GenuineEventType.GeneralListenerStarted, null, this.Local, endPoint));

                Thread thread = new Thread(new ThreadStart(this._smAcceptConnectionClosure.AcceptConnections));
                thread.IsBackground = true;
                thread.Start();

                // LOG:
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0)
                {
                    binaryLogWriter.WriteEvent(LogCategory.AcceptingConnection, "SharedMemoryConnectionManager.StartListening",
                                               LogMessageType.ListeningStarted, null, null, null, null,
                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                               null, null, -1,
                                               0, 0, 0, shareName, null, null, null,
                                               "\"{0}\" is now listened.", shareName);
                }
            }
            catch (Exception ex)
            {
                // LOG:
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.Connection] > 0)
                {
                    binaryLogWriter.WriteEvent(LogCategory.AcceptingConnection, "SharedMemoryConnectionManager.StartListening",
                                               LogMessageType.ListeningStarted, ex, null, null, null,
                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                               null, null, -1,
                                               0, 0, 0, shareName, null, null, null,
                                               "Listening to \"{0}\" cannot be started.", shareName);
                }

                if (sharedMemoryConnection != null)
                {
                    sharedMemoryConnection.ReleaseUnmanagedResources();
                }
                throw;
            }
        }