Ejemplo n.º 1
0
        private void StartListening()
        {
            if ((_stopping != Constants.True) &&
                Common.CompareAndSet(ref _status, RpcServerStatus.Stopped, RpcServerStatus.Starting))
            {
                var servingEP = _options.EndPoint;

                Socket listener = null;
                try
                {
                    var addressFamily = servingEP.AddressFamily;
                    if (addressFamily == AddressFamily.Unknown || addressFamily == AddressFamily.Unspecified)
                    {
                        addressFamily = IPAddress.Any.AddressFamily;
                    }

                    listener = new NativeSocket(addressFamily, SocketType.Stream, ProtocolType.Tcp)
                    {
                        Blocking = false
                    };
                    listener.Configure(_options.SendTimeoutMSec, _options.ReceiveTimeoutMSec, true, true);
                }
                catch (Exception)
                {
                    Interlocked.Exchange(ref _status, RpcServerStatus.Stopped);

                    using (listener) { }
                    throw;
                }

                Interlocked.Exchange(ref _listener, listener);
                try
                {
                    listener.Bind(servingEP);
                    listener.Listen(_options.ConcurrentConnections);

                    var localEP = (listener.LocalEndPoint as IPEndPoint) ??
                                  (IPEndPoint)listener.LocalEndPoint;

                    Interlocked.Exchange(ref _localEndPoint, localEP);
                }
                catch (Exception)
                {
                    Interlocked.Exchange(ref _status, RpcServerStatus.Stopped);

                    TryToCloseSocket(listener);
                    throw;
                }

                Interlocked.Exchange(ref _status, RpcServerStatus.Started);

                StartAccepting(NewAcceptEventArgs(), true);
            }
        }
Ejemplo n.º 2
0
        protected virtual Socket GetClientSocket()
        {
            var result = _socket;

            if (result == null || result.Disposed)
            {
                lock (_socketLock)
                {
                    result = _socket;
                    if (result?.Disposed ?? false)
                    {
                        Interlocked.Exchange(ref _socket, null);
                        result = null;
                    }

                    if (result == null)
                    {
                        var remoteEP      = _options.EndPoint;
                        var addressFamily = remoteEP.AddressFamily;

                        if (addressFamily == AddressFamily.Unknown ||
                            addressFamily == AddressFamily.Unspecified)
                        {
                            addressFamily = IPAddress.Any.AddressFamily;
                        }

                        result = new NativeSocket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
                        result.Configure(_options.SendTimeoutMSec, _options.ReceiveTimeoutMSec, true, true);

                        using (Interlocked.Exchange(ref _connection,
                                                    new RpcConnection(this, result, _options.Serializer, _options.BulkSendLength, HandleResponse, null)))
                        { }

                        Close(Interlocked.Exchange(ref _socket, result));
                    }
                }
            }
            return(result);
        }