Beispiel #1
0
        public TcpConnection(Socket socket, SocketSetting setting, IBufferPool receiveDataBufferPool, Action <ITcpConnection, byte[]> messageArrivedHandler, Action <ITcpConnection, SocketError> connectionClosedHandler)
        {
            Ensure.NotNull(socket, "socket");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");
            Ensure.NotNull(connectionClosedHandler, "connectionClosedHandler");

            _socket  = socket;
            _setting = setting;
            _receiveDataBufferPool   = receiveDataBufferPool;
            _localEndPoint           = socket.LocalEndPoint;
            _remotingEndPoint        = socket.RemoteEndPoint;
            _messageArrivedHandler   = messageArrivedHandler;
            _connectionClosedHandler = connectionClosedHandler;

            _sendSocketArgs = new SocketAsyncEventArgs();
            _sendSocketArgs.AcceptSocket = _socket;
            _sendSocketArgs.Completed   += OnSendAsyncCompleted;

            _receiveSocketArgs = new SocketAsyncEventArgs();
            _receiveSocketArgs.AcceptSocket = socket;
            _receiveSocketArgs.Completed   += OnReceiveAsyncCompleted;

            _logger = ObjectContainer.Resolve <ILoggerFactory>().Create(GetType().FullName);
            _framer = ObjectContainer.Resolve <IMessageFramer>();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            TryReceive();
            TrySend();
        }
Beispiel #2
0
 public ProducerSetting()
 {
     BrokerAddress = new IPEndPoint(SocketUtils.GetLocalIPV4(), 5000);
     BrokerAdminAddress = new IPEndPoint(SocketUtils.GetLocalIPV4(), 5002);
     SocketSetting = new SocketSetting();
     UpdateTopicQueueCountInterval = 1000 * 5;
 }
Beispiel #3
0
        public TcpConnection(Socket socket, SocketSetting setting, IBufferPool receiveDataBufferPool, Action<ITcpConnection, byte[]> messageArrivedHandler, Action<ITcpConnection, SocketError> connectionClosedHandler)
        {
            Ensure.NotNull(socket, "socket");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");
            Ensure.NotNull(connectionClosedHandler, "connectionClosedHandler");

            _socket = socket;
            _setting = setting;
            _receiveDataBufferPool = receiveDataBufferPool;
            _localEndPoint = socket.LocalEndPoint;
            _remotingEndPoint = socket.RemoteEndPoint;
            _messageArrivedHandler = messageArrivedHandler;
            _connectionClosedHandler = connectionClosedHandler;

            _sendSocketArgs = new SocketAsyncEventArgs();
            _sendSocketArgs.AcceptSocket = _socket;
            _sendSocketArgs.Completed += OnSendAsyncCompleted;

            _receiveSocketArgs = new SocketAsyncEventArgs();
            _receiveSocketArgs.AcceptSocket = socket;
            _receiveSocketArgs.Completed += OnReceiveAsyncCompleted;

            _logger = ObjectContainer.Resolve<ILoggerFactory>().Create(GetType().FullName);
            _framer = ObjectContainer.Resolve<IMessageFramer>();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            TryReceive();
            TrySend();
        }
Beispiel #4
0
 public ProducerSetting()
 {
     BrokerAddress = new IPEndPoint(SocketUtils.GetLocalIPV4(), 5000);
     BrokerAdminAddress = new IPEndPoint(SocketUtils.GetLocalIPV4(), 5002);
     SocketSetting = new SocketSetting();
     UpdateTopicQueueCountInterval = 1000;
     HeartbeatBrokerInterval = 1000;
     MessageMaxSize = 1024 * 1024 * 4;
 }
Beispiel #5
0
        static Settings()
        {
            var nameServerAddresses = ConfigurationManager.AppSettings["nameServerAddresses"];

            if (string.IsNullOrWhiteSpace(nameServerAddresses))
            {
                var defaultNameServer = new IPEndPoint(SocketUtils.GetLocalIPV4(), 9493);
                var defaultList = new List<IPEndPoint>();
                defaultList.Add(defaultNameServer);
                NameServerList = defaultList;
            }
            else
            {
                var addressList = nameServerAddresses.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                var endpointList = new List<IPEndPoint>();
                foreach (var address in addressList)
                {
                    var array = address.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                    var endpoint = new IPEndPoint(IPAddress.Parse(array[0]), int.Parse(array[1]));
                    endpointList.Add(endpoint);
                }
                NameServerList = endpointList;
            }

            var enableMonitorMessageAccumulate = ConfigurationManager.AppSettings["enableMonitorMessageAccumulate"];
            if (string.IsNullOrWhiteSpace(enableMonitorMessageAccumulate))
            {
                EnableMonitorMessageAccumulate = false;
            }
            else
            {
                EnableMonitorMessageAccumulate = bool.Parse(enableMonitorMessageAccumulate);
            }

            var messageAccumulateThreshold = ConfigurationManager.AppSettings["messageAccumulateThreshold"];
            if (string.IsNullOrWhiteSpace(messageAccumulateThreshold))
            {
                MessageAccumulateThreshold = 10 * 10000;
            }
            else
            {
                MessageAccumulateThreshold = int.Parse(messageAccumulateThreshold);
            }

            var scanMessageAccumulateInterval = ConfigurationManager.AppSettings["scanMessageAccumulateInterval"];
            if (string.IsNullOrWhiteSpace(scanMessageAccumulateInterval))
            {
                ScanMessageAccumulateInterval = 1000 * 60 * 5;
            }
            else
            {
                ScanMessageAccumulateInterval = int.Parse(scanMessageAccumulateInterval);
            }

            SocketSetting = new SocketSetting();
        }
Beispiel #6
0
        public ServerSocket(IPEndPoint listeningEndPoint, SocketSetting setting, IBufferPool receiveDataBufferPool, Action <ITcpConnection, byte[], Action <byte[]> > messageArrivedHandler)
        {
            Ensure.NotNull(listeningEndPoint, "listeningEndPoint");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");

            _listeningEndPoint        = listeningEndPoint;
            _setting                  = setting;
            _receiveDataBufferPool    = receiveDataBufferPool;
            _connectionEventListeners = new List <IConnectionEventListener>();
            _messageArrivedHandler    = messageArrivedHandler;
            _socket                      = SocketUtils.CreateSocket(_setting.SendBufferSize, _setting.ReceiveBufferSize);
            _acceptSocketArgs            = new SocketAsyncEventArgs();
            _acceptSocketArgs.Completed += AcceptCompleted;
            _logger                      = ObjectContainer.Resolve <ILoggerFactory>().Create(GetType().FullName);
        }
Beispiel #7
0
        public ServerSocket(IPEndPoint listeningEndPoint, SocketSetting setting, IBufferPool receiveDataBufferPool, Action<ITcpConnection, byte[], Action<byte[]>> messageArrivedHandler)
        {
            Ensure.NotNull(listeningEndPoint, "listeningEndPoint");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");

            _listeningEndPoint = listeningEndPoint;
            _setting = setting;
            _receiveDataBufferPool = receiveDataBufferPool;
            _connectionEventListeners = new List<IConnectionEventListener>();
            _messageArrivedHandler = messageArrivedHandler;
            _socket = SocketUtils.CreateSocket(_setting.SendBufferSize, _setting.ReceiveBufferSize);
            _acceptSocketArgs = new SocketAsyncEventArgs();
            _acceptSocketArgs.Completed += AcceptCompleted;
            _logger = ObjectContainer.Resolve<ILoggerFactory>().Create(GetType().FullName);
        }
Beispiel #8
0
        public ClientSocket(EndPoint serverEndPoint, EndPoint localEndPoint, SocketSetting setting, IBufferPool receiveDataBufferPool, Action<ITcpConnection, byte[]> messageArrivedHandler)
        {
            Ensure.NotNull(serverEndPoint, "serverEndPoint");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");

            _connectionEventListeners = new List<IConnectionEventListener>();

            _serverEndPoint = serverEndPoint;
            _localEndPoint = localEndPoint;
            _setting = setting;
            _receiveDataBufferPool = receiveDataBufferPool;
            _messageArrivedHandler = messageArrivedHandler;
            _waitConnectHandle = new ManualResetEvent(false);
            _socket = SocketUtils.CreateSocket(_setting.SendBufferSize, _setting.ReceiveBufferSize);
            _logger = ObjectContainer.Resolve<ILoggerFactory>().Create(GetType().FullName);
        }
        public ClientSocket(string name, EndPoint serverEndPoint, EndPoint localEndPoint, SocketSetting setting, IBufferPool receiveDataBufferPool, Action <ITcpConnection, byte[]> messageArrivedHandler)
        {
            Ensure.NotNull(serverEndPoint, "serverEndPoint");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");

            Name = name;
            _connectionEventListeners = new List <IConnectionEventListener>();
            _serverEndPoint           = serverEndPoint;
            _localEndPoint            = localEndPoint;
            _setting = setting;
            _receiveDataBufferPool = receiveDataBufferPool;
            _messageArrivedHandler = messageArrivedHandler;
            _waitConnectHandle     = new ManualResetEvent(false);
            _socket = SocketUtils.CreateSocket(_setting.SendBufferSize, _setting.ReceiveBufferSize);
            _logger = ObjectContainer.Resolve <ILoggerFactory>().Create(GetType().FullName);
            _flowControlThreshold = _setting.SendMessageFlowControlThreshold;
        }
Beispiel #10
0
 public ConsumerSetting()
 {
     BrokerAddress = new IPEndPoint(SocketUtils.GetLocalIPV4(), 5001);
     BrokerAdminAddress = new IPEndPoint(SocketUtils.GetLocalIPV4(), 5002);
     SocketSetting = new SocketSetting();
     ConsumeThreadMaxCount = Environment.ProcessorCount * 2;
     DefaultTimeoutMilliseconds = 60 * 1000;
     RebalanceInterval = 1000;
     HeartbeatBrokerInterval = 1000;
     UpdateTopicQueueCountInterval = 1000;
     PersistConsumerOffsetInterval = 1000;
     PullThresholdForQueue = 100000;
     PullTimeDelayMillsWhenFlowControl = 3000;
     SuspendPullRequestMilliseconds = 60 * 1000;
     PullRequestTimeoutMilliseconds = 70 * 1000;
     RetryMessageInterval = 3000;
     PullMessageBatchSize = 32;
     ConsumeFromWhere = ConsumeFromWhere.LastOffset;
 }
Beispiel #11
0
 public ConsumerSetting()
 {
     BrokerAddress = new IPEndPoint(SocketUtils.GetLocalIPV4(), 5001);
     BrokerAdminAddress = new IPEndPoint(SocketUtils.GetLocalIPV4(), 5002);
     SocketSetting = new SocketSetting();
     RebalanceInterval = 1000;
     HeartbeatBrokerInterval = 1000;
     UpdateTopicQueueCountInterval = 1000;
     SendConsumerOffsetInterval = 1000;
     PullMessageFlowControlThreshold = 1000;
     PullMessageFlowControlStepPercent = 1;
     PullMessageFlowControlStepWaitMilliseconds = 1;
     SuspendPullRequestMilliseconds = 60 * 1000;
     PullRequestTimeoutMilliseconds = 70 * 1000;
     RetryMessageInterval = 1000;
     PullMessageBatchSize = 32;
     ConsumeFromWhere = ConsumeFromWhere.LastOffset;
     MessageHandleMode = MessageHandleMode.Parallel;
 }
        public TcpConnection(string name, Socket socket, SocketSetting setting, IBufferPool receiveDataBufferPool, Action <ITcpConnection, byte[]> messageArrivedHandler, Action <ITcpConnection, SocketError> connectionClosedHandler)
        {
            Ensure.NotNull(name, "name");
            Ensure.NotNull(socket, "socket");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");
            Ensure.NotNull(connectionClosedHandler, "connectionClosedHandler");

            Id      = Guid.NewGuid();
            Name    = name;
            Socket  = socket;
            Setting = setting;
            _receiveDataBufferPool   = receiveDataBufferPool;
            LocalEndPoint            = socket.LocalEndPoint;
            RemotingEndPoint         = socket.RemoteEndPoint;
            _messageArrivedHandler   = messageArrivedHandler;
            _connectionClosedHandler = connectionClosedHandler;

            _sendSocketArgs = new SocketAsyncEventArgs
            {
                AcceptSocket = socket
            };
            _sendSocketArgs.Completed += OnSendAsyncCompleted;

            _receiveSocketArgs = new SocketAsyncEventArgs
            {
                AcceptSocket = socket
            };
            _receiveSocketArgs.Completed += OnReceiveAsyncCompleted;
            _receiveSocketArgs.UserToken  = new ConcurrentQueue <ReceivedData>();

            _logger = ObjectContainer.Resolve <ILoggerFactory>().Create(GetType().FullName);
            _framer = ObjectContainer.Resolve <IMessageFramer>();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);//执行到达的消息

            TryReceive();
            TrySend();
        }
Beispiel #13
0
        public TcpConnection(Socket socket, SocketSetting setting, IBufferPool receiveDataBufferPool, Action<ITcpConnection, byte[]> messageArrivedHandler, Action<ITcpConnection, SocketError> connectionClosedHandler)
        {
            Ensure.NotNull(socket, "socket");
            Ensure.NotNull(setting, "setting");
            Ensure.NotNull(receiveDataBufferPool, "receiveDataBufferPool");
            Ensure.NotNull(messageArrivedHandler, "messageArrivedHandler");
            Ensure.NotNull(connectionClosedHandler, "connectionClosedHandler");

            _socket = socket;
            _setting = setting;
            _flowControlThreshold = _setting.SendMessageFlowControlThreshold;
            _receiveDataBufferPool = receiveDataBufferPool;
            _localEndPoint = socket.LocalEndPoint;
            _remotingEndPoint = socket.RemoteEndPoint;
            _messageArrivedHandler = messageArrivedHandler;
            _connectionClosedHandler = connectionClosedHandler;

            //Initialize send socket async event args.
            for (var i = 0; i < 2; i++)
            {
                var sendSocketArgs = new SocketAsyncEventArgs();
                sendSocketArgs.AcceptSocket = _socket;
                sendSocketArgs.Completed += OnSendAsyncCompleted;
                _sendSocketArgsStack.Push(sendSocketArgs);
            }

            //Initialize receive socket async event args.
            _receiveSocketArgs = new SocketAsyncEventArgs();
            _receiveSocketArgs.AcceptSocket = socket;
            _receiveSocketArgs.Completed += OnReceiveAsyncCompleted;

            _logger = ObjectContainer.Resolve<ILoggerFactory>().Create(GetType().FullName);

            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);

            TryReceive();
            TrySend();
        }