当前位置: 首页>>代码示例>>C#>>正文


C# ITcpConnection类代码示例

本文整理汇总了C#中ITcpConnection的典型用法代码示例。如果您正苦于以下问题:C# ITcpConnection类的具体用法?C# ITcpConnection怎么用?C# ITcpConnection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ITcpConnection类属于命名空间,在下文中一共展示了ITcpConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InitConnect

        internal void InitConnect(IPEndPoint serverEndPoint,
                                  Action<IPEndPoint, Socket> onConnectionEstablished,
                                  Action<IPEndPoint, SocketError> onConnectionFailed,
                                  ITcpConnection connection,
                                  TimeSpan connectionTimeout)
        {
            if (serverEndPoint == null)
                throw new ArgumentNullException("serverEndPoint");
            if (onConnectionEstablished == null)
                throw new ArgumentNullException("onConnectionEstablished");
            if (onConnectionFailed == null)
                throw new ArgumentNullException("onConnectionFailed");

            var socketArgs = _connectSocketArgsPool.Get();
            var connectingSocket = new Socket(serverEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            socketArgs.RemoteEndPoint = serverEndPoint;
            socketArgs.AcceptSocket = connectingSocket;
            var callbacks = (CallbacksStateToken) socketArgs.UserToken;
            callbacks.OnConnectionEstablished = onConnectionEstablished;
            callbacks.OnConnectionFailed = onConnectionFailed;
            callbacks.PendingConnection = new PendingConnection(connection, DateTime.UtcNow.Add(connectionTimeout));

            AddToConnecting(callbacks.PendingConnection);

            try
            {
                var firedAsync = connectingSocket.ConnectAsync(socketArgs);
                if (!firedAsync)
                    ProcessConnect(socketArgs);
            }
            catch (ObjectDisposedException)
            {
                HandleBadConnect(socketArgs);
            }
        }
开发者ID:jen20,项目名称:tcp-servers-talk,代码行数:35,代码来源:TcpClientConnector.cs

示例2: OnConnectionClosed

 private void OnConnectionClosed(ITcpConnection connection, SocketError socketError)
 {
     if (Interlocked.CompareExchange(ref _isClosed, 1, 0) == 0)
     {
         Console.WriteLine("Connection '{0}' [{1:B}] closed: {2}.", connection.ConnectionId, connection.RemoteEndPoint, socketError);
     }
 }
开发者ID:Cocotus,项目名称:SocketAsyncSample,代码行数:7,代码来源:AcceptedConnection.cs

示例3: RegisterConsumer

 public void RegisterConsumer(string groupName, string consumerId, IEnumerable<string> subscriptionTopics, IEnumerable<string> consumingQueues, ITcpConnection connection)
 {
     var consumerGroup = _consumerGroupDict.GetOrAdd(groupName, key => new ConsumerGroup(key));
     consumerGroup.Register(consumerId, connection);
     consumerGroup.UpdateConsumerSubscriptionTopics(consumerId, subscriptionTopics);
     consumerGroup.UpdateConsumerConsumingQueues(consumerId, consumingQueues);
 }
开发者ID:yonglehou,项目名称:equeue,代码行数:7,代码来源:ConsumerManager.cs

示例4: HandleRemotingRequest

        private void HandleRemotingRequest(ITcpConnection connection, byte[] message, Action<byte[]> sendReplyAction)
        {
            var remotingRequest = RemotingUtil.ParseRequest(message);
            var requestHandlerContext = new SocketRequestHandlerContext(connection, sendReplyAction);

            IRequestHandler requestHandler;
            if (!_requestHandlerDict.TryGetValue(remotingRequest.Code, out requestHandler))
            {
                var errorMessage = string.Format("No request handler found for remoting request, request code:{0}", remotingRequest.Code);
                _logger.Error(errorMessage);
                requestHandlerContext.SendRemotingResponse(new RemotingResponse(-1, remotingRequest.Sequence, Encoding.UTF8.GetBytes(errorMessage)));
                return;
            }

            try
            {
                _logger.DebugFormat("Handling remoting request, request code:{0}, request sequence:{1}", remotingRequest.Code, remotingRequest.Sequence);
                var remotingResponse = requestHandler.HandleRequest(requestHandlerContext, remotingRequest);
                if (!remotingRequest.IsOneway && remotingResponse != null)
                {
                    requestHandlerContext.SendRemotingResponse(remotingResponse);
                }
            }
            catch (Exception ex)
            {
                var errorMessage = string.Format("Exception raised when handling remoting request, request code:{0}.", remotingRequest.Code);
                _logger.Error(errorMessage, ex);
                if (!remotingRequest.IsOneway)
                {
                    requestHandlerContext.SendRemotingResponse(new RemotingResponse(-1, remotingRequest.Sequence, Encoding.UTF8.GetBytes(ex.Message)));
                }
            }
        }
开发者ID:hong1990,项目名称:ecommon,代码行数:33,代码来源:SocketRemotingServer.cs

示例5: OnConnectionFailed

 private void OnConnectionFailed(ITcpConnection connection, SocketError socketError)
 {
     if (Interlocked.CompareExchange(ref _isClosed, 1, 0) == 0)
     {
         Console.WriteLine("Connection '{0}' ({1:B}) to [{2}] failed: {3}.", _connectionName, _connectionId, connection.RemoteEndPoint, socketError);
     }
 }
开发者ID:Cocotus,项目名称:SocketAsyncSample,代码行数:7,代码来源:SocketClient.cs

示例6: OnConnectionClosed

        private void OnConnectionClosed(ITcpConnection connection, SocketError socketError)
        {
            connection.ConnectionClosed -= OnConnectionClosed;

            var handler = ConnectionClosed;
            if (handler != null)
                handler(this, socketError);
        }
开发者ID:jpierson,项目名称:EventStore,代码行数:8,代码来源:TcpTypedConnection.cs

示例7: SocketRequestHandlerContext

 public SocketRequestHandlerContext(ITcpConnection connection, Action<byte[]> sendReplyAction)
 {
     Channel = new SocketChannel(connection);
     SendRemotingResponse = remotingResponse =>
     {
         sendReplyAction(RemotingUtil.BuildResponseMessage(remotingResponse));
     };
 }
开发者ID:hong1990,项目名称:ecommon,代码行数:8,代码来源:SocketRequestHandlerContext.cs

示例8: Init

 public void Init(ITcpConnection connection, ICommandFactory commandFactory, int id, Car car)
 {
     Connection = connection;
     Id = id;
     Car = car;
     CommandFactory = commandFactory;
     _initilized = true;
 }
开发者ID:skohub,项目名称:CarControl,代码行数:8,代码来源:BaseCarProtocol.cs

示例9: Register

 public void Register(string consumerId, ITcpConnection connection)
 {
     var consumerHeartbeatInfo = _consumerDict.GetOrAdd(consumerId, key =>
     {
         _logger.InfoFormat("Consumer registered to group: {0}, consumerId: {1}", _groupName, consumerId);
         return new ConsumerHeartbeatInfo(key, connection);
     });
     consumerHeartbeatInfo.LastHeartbeatTime = DateTime.Now;
 }
开发者ID:a407121393,项目名称:equeue,代码行数:9,代码来源:ConsumerGroup.cs

示例10: HandleRemotingRequest

        private void HandleRemotingRequest(ITcpConnection connection, byte[] message, Action<byte[]> sendReplyAction)
        {
            if (_isShuttingdown) return;

            var remotingRequest = RemotingUtil.ParseRequest(message);
            var requestHandlerContext = new SocketRequestHandlerContext(connection, sendReplyAction);

            IRequestHandler requestHandler;
            if (!_requestHandlerDict.TryGetValue(remotingRequest.Code, out requestHandler))
            {
                var errorMessage = string.Format("No request handler found for remoting request:{0}", remotingRequest);
                _logger.Error(errorMessage);
                if (remotingRequest.Type != RemotingRequestType.Oneway)
                {
                    requestHandlerContext.SendRemotingResponse(new RemotingResponse(
                        remotingRequest.Type,
                        remotingRequest.Code,
                        remotingRequest.Sequence,
                        remotingRequest.CreatedTime,
                        -1,
                        Encoding.UTF8.GetBytes(errorMessage),
                        DateTime.Now,
                        remotingRequest.Header,
                        null));
                }
                return;
            }

            try
            {
                var remotingResponse = requestHandler.HandleRequest(requestHandlerContext, remotingRequest);
                if (remotingRequest.Type != RemotingRequestType.Oneway && remotingResponse != null)
                {
                    requestHandlerContext.SendRemotingResponse(remotingResponse);
                }
            }
            catch (Exception ex)
            {
                var errorMessage = string.Format("Unknown exception raised when handling remoting request:{0}.", remotingRequest);
                _logger.Error(errorMessage, ex);
                if (remotingRequest.Type != RemotingRequestType.Oneway)
                {
                    requestHandlerContext.SendRemotingResponse(new RemotingResponse(
                        remotingRequest.Type,
                        remotingRequest.Code,
                        remotingRequest.Sequence,
                        remotingRequest.CreatedTime,
                        -1,
                        Encoding.UTF8.GetBytes(ex.Message),
                        DateTime.Now,
                        remotingRequest.Header,
                        null));
                }
            }
        }
开发者ID:Aaron-Liu,项目名称:ecommon,代码行数:55,代码来源:SocketRemotingServer.cs

示例11: RegisterProducer

 public void RegisterProducer(string producerId, ITcpConnection connection)
 {
     _producerDict.AddOrUpdate(producerId, key =>
     {
         _logger.InfoFormat("Producer registered, producerId: {0}", key);
         return new ProducerHeartbeatInfo(connection) { LastHeartbeatTime = DateTime.Now };
     }, (key, existing) =>
     {
         existing.LastHeartbeatTime = DateTime.Now;
         return existing;
     });
 }
开发者ID:a407121393,项目名称:equeue,代码行数:12,代码来源:ProducerManager.cs

示例12: RegisterBroker

        public void RegisterBroker(ITcpConnection connection, BrokerRegistrationRequest request)
        {
            lock (_lockObj)
            {
                var brokerInfo = request.BrokerInfo;
                var cluster = _clusterDict.GetOrAdd(brokerInfo.ClusterName, x => new Cluster { ClusterName = x });
                var brokerGroup = cluster.BrokerGroups.GetOrAdd(brokerInfo.GroupName, x => new BrokerGroup { GroupName = x });
                Broker broker;
                if (!brokerGroup.Brokers.TryGetValue(brokerInfo.BrokerName, out broker))
                {
                    var connectionId = connection.RemotingEndPoint.ToAddress();
                    broker = new Broker
                    {
                        BrokerInfo = request.BrokerInfo,
                        TotalSendThroughput = request.TotalSendThroughput,
                        TotalConsumeThroughput = request.TotalConsumeThroughput,
                        TotalUnConsumedMessageCount = request.TotalUnConsumedMessageCount,
                        TopicQueueInfoList = request.TopicQueueInfoList,
                        TopicConsumeInfoList = request.TopicConsumeInfoList,
                        ProducerList = request.ProducerList,
                        ConsumerList = request.ConsumerList,
                        Connection = connection,
                        ConnectionId = connectionId,
                        LastActiveTime = DateTime.Now,
                        FirstRegisteredTime = DateTime.Now,
                        Group = brokerGroup
                    };
                    if (brokerGroup.Brokers.TryAdd(brokerInfo.BrokerName, broker))
                    {
                        _logger.InfoFormat("Registered new broker, brokerInfo: {0}", _jsonSerializer.Serialize(brokerInfo));
                    }
                }
                else
                {
                    broker.LastActiveTime = DateTime.Now;
                    broker.TotalSendThroughput = request.TotalSendThroughput;
                    broker.TotalConsumeThroughput = request.TotalConsumeThroughput;
                    broker.TotalUnConsumedMessageCount = request.TotalUnConsumedMessageCount;

                    if (!broker.BrokerInfo.IsEqualsWith(request.BrokerInfo))
                    {
                        var logInfo = string.Format("Broker basicInfo changed, old: {0}, new: {1}", broker.BrokerInfo, request.BrokerInfo);
                        broker.BrokerInfo = request.BrokerInfo;
                        _logger.Info(logInfo);
                    }

                    broker.TopicQueueInfoList = request.TopicQueueInfoList;
                    broker.TopicConsumeInfoList = request.TopicConsumeInfoList;
                    broker.ProducerList = request.ProducerList;
                    broker.ConsumerList = request.ConsumerList;
                }
            }
        }
开发者ID:Aaron-Liu,项目名称:equeue,代码行数:53,代码来源:ClusterManager.cs

示例13: OnRawDataReceived

 private void OnRawDataReceived(ITcpConnection connection, IEnumerable<ArraySegment<byte>> data)
 {
     try
     {
         _framer.UnFrameData(data);
     }
     catch (PackageFramingException exc)
     {
         Console.WriteLine(exc.Message);
         return;
     }
     connection.ReceiveAsync(OnRawDataReceived);
 }
开发者ID:Cocotus,项目名称:SocketAsyncSample,代码行数:13,代码来源:AcceptedConnection.cs

示例14: AcceptedConnection

        public AcceptedConnection(ITcpConnection connection)
        {
            _framer = new LengthPrefixMessageFramer();
            _framer.RegisterMessageArrivedCallback(OnMessageArrived);
            _connection = connection;

            _connection.ConnectionClosed += OnConnectionClosed;
            if (_connection.IsClosed)
            {
                OnConnectionClosed(_connection, SocketError.Success);
                return;
            }
        }
开发者ID:Cocotus,项目名称:SocketAsyncSample,代码行数:13,代码来源:AcceptedConnection.cs

示例15: OnConnectionClosed

 private void OnConnectionClosed(ITcpConnection connection, SocketError socketError)
 {
     foreach (var listener in _connectionEventListeners)
     {
         try
         {
             listener.OnConnectionClosed(connection, socketError);
         }
         catch (Exception ex)
         {
             _logger.Error(string.Format("Notify connection closed failed, listener type:{0}", listener.GetType().Name), ex);
         }
     }
 }
开发者ID:johnwonder,项目名称:SimpleSocket,代码行数:14,代码来源:ServerSocket.cs


注:本文中的ITcpConnection类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。