public RpcTcpDuplexConnection(RpcTcpClientChannel channel, TcpUri serverUri)
            : base(RpcConnectionMode.Duplex, RpcConnectionDirection.Client)
        {
            _serverUri = serverUri;

            _socket = new RpcTcpSocketConnection(RpcConnectionDirection.Client);
            _socket.Disconnected += new Action <RpcTcpSocketConnection>(
                (socket) => {
                OnDisconnected();
            }
                );

            _socket.RequestReceived += new Action <RpcTcpSocketConnection, int, RpcRequest>(
                (socket, seq, request) => {
                var tx = new RpcTcpServerTransaction(_channel, this, socket, request, seq);
                OnTransactionCreated(tx);
            }
                );

            _socket.ResponseReceived += new Action <RpcTcpSocketConnection, int, RpcResponse>(
                (socket, seq, response) => {
                RpcTcpTransactionManager.EndTransaction(seq, response);
            }
                );

            _channel = new RpcDuplexCallbackChannel("tcp", serverUri.ToString(), this, channel);
        }
Exemple #2
0
        public void BeginConnect(TcpUri uri, Action <Exception> callback, bool enableKeepalive)
        {
            if (_connected)
            {
                return;
            }

            _counter.ConnectionsPending.Increment();

            if (Interlocked.CompareExchange(ref _connecting, 1, 0) == 0)
            {
                _socket          = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                _remoteUri       = uri;
                _connectCallback = callback;

                if (enableKeepalive)
                {
                    EnableKeepAlive(150000, 1000);
                }

                SocketAsyncEventArgs e = new SocketAsyncEventArgs();
                e.RemoteEndPoint = new IPEndPoint(uri.Address, uri.Port);
                e.Completed     += new EventHandler <SocketAsyncEventArgs>(
                    (sender, e2) => ProcessConnect(e2)
                    );

                if (!_socket.ConnectAsync(e))
                {
                    ProcessConnect(e);
                }
            }
        }
Exemple #3
0
        /// <summary>
        ///     初始化
        /// </summary>
        public void Initialize(string hostName, TcpUri basicCommunicationAddress)
        {
            _basePath = SystemWorker.ConfigurationProxy.GetField("kae-system", "ZooKeeperBasePath", false);
            if (_client.existsAsync(_basePath, false).Result == null)
            {
                try { _client.createAsync(_basePath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).Wait(); }
                catch (Exception) { }
            }
            //hosting node.
            string hostingPath = Path.Combine(_basePath, "hosting");

            hostingPath = hostingPath.Replace("\\", "/");
            if (_client.existsAsync(hostingPath, false).Result == null)
            {
                try { _client.createAsync(hostingPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).Wait(); }
                catch (Exception) { }
            }
            //protocols node.
            _protocolPath = Path.Combine(_basePath, "protocols");
            _protocolPath = _protocolPath.Replace("\\", "/");
            if (_client.existsAsync(_protocolPath, false).Result == null)
            {
                try { _client.createAsync(_protocolPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).Wait(); }
                catch (Exception) { }
            }
            //create resource node.
            string resourcePath = Path.Combine(_basePath, "hosting/", hostName);

            resourcePath = resourcePath.Replace("\\", "/");
            if (_client.existsAsync(resourcePath, false).Result == null)
            {
                try { _client.createAsync(resourcePath, Encoding.UTF8.GetBytes(basicCommunicationAddress.ToString()), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL).Wait(); }
                catch (Exception) { }
            }
        }
        public RpcTcpSimplexConnection(RpcTcpClientChannel channel, TcpUri serverUri, int concurrentConnection)
            : base(RpcConnectionMode.Simplex, RpcConnectionDirection.Client)
        {
            _channel         = channel;
            _serverUri       = serverUri;
            _connections     = new RpcTcpSimplexConnectionWrapper[concurrentConnection];
            _lc              = new LoopCounter(concurrentConnection);
            _connectionCount = new ComboClass <int>(0);

            for (int i = 0; i < concurrentConnection; i++)
            {
                var wrapper = new RpcTcpSimplexConnectionWrapper(this);
                RpcTcpSimplexConnectionManager.AddConnection(wrapper);
                _connections[i] = wrapper;
            }
        }
        /// <summary>
        ///     根据KAE网络资源来构建一个网络宿主信道
        /// </summary>
        /// <param name="network">网络通信类型</param>
        /// <param name="uri">返回的网络通信资源对象</param>
        /// <returns>返回构建后的宿主信道</returns>
        /// <exception cref="NotSupportedException">不支持的网络类型</exception>
        public static IHostTransportChannel BuildHostChannel(NetworkTypes network, out Uri uri)
        {
            IHostTransportChannel channel;

            switch (network)
            {
            case NetworkTypes.TCP:
                int port = GetDynamicTCPPort();
                channel = new TcpHostTransportChannel(port);
                uri     = new TcpUri(string.Format("tcp://{0}:{1}", GetCurrentMachineIP(), port));
                break;

            default: throw new NotSupportedException("#Sadly, current network type wasn't supported yet! #Network Type: " + network);
            }
            return(channel);
        }
Exemple #6
0
 public void Accept(SocketAsyncEventArgs e)
 {
     if (e.SocketError != SocketError.Success)
     {
         throw new Exception("Accept Failed:" + e.SocketError);
     }
     else
     {
         _socket = e.AcceptSocket;
         IPEndPoint ep = (IPEndPoint)_socket.RemoteEndPoint;
         _remoteUri     = new TcpUri(ep.Address, ep.Port);
         _connecting    = 0;
         _connected     = true;
         _connectedTime = DateTime.Now;
         BeginReceive();
     }
 }
Exemple #7
0
        /// <summary>
        ///    初始化资源
        /// </summary>
        /// <param name="businessPort">KAE Agent启动时需要注册的业务通信端口</param>
        /// <param name="systemPort">KAE Agent启动时需要注册的系统管理通信端口</param>
        /// <param name="localIP">当前本机IP</param>
        public static void Initialize(ushort businessPort, ushort systemPort, string localIP = null)
        {
            if (IsInitialized)
            {
                return;
            }
            IsInitialized = true;
            if (string.IsNullOrEmpty(localIP))
            {
                localIP = NetworkHelper.GetCurrentMachineIP();
            }
            /*Openning TCP port for business requests.*/
            TcpUri uri = new TcpUri(string.Format("tcp://{0}:{1}", localIP, businessPort));
            IHostTransportChannel hostChannel = new TcpHostTransportChannel(businessPort);

            hostChannel.ChannelCreated += ChannelCreated;
            if (!hostChannel.Regist())
            {
                hostChannel.ChannelCreated -= ChannelCreated;
                throw new AllocResourceFailedException("#Sadly, We couldn't alloc current network resource. #Uri: " + uri);
            }
            hostChannel.Tag = new KAENetworkResource {
                NetworkUri = uri, Protocol = ProtocolTypes.Metadata
            };
            _resources.Add(ProtocolTypes.Metadata, new Tuple <IHostTransportChannel, Uri>(hostChannel, uri));
            _tracing.DebugInfo("\t\t#Initialized network resource.\t(P: {0},\tURL: {1})", ConsoleColor.Gray, ProtocolTypes.Metadata, uri);
            /*Openning TCP port for system management*/
            uri         = new TcpUri(string.Format("tcp://{0}:{1}", localIP, systemPort));
            hostChannel = new TcpHostTransportChannel(systemPort);
            hostChannel.ChannelCreated += ChannelCreated;
            if (!hostChannel.Regist())
            {
                hostChannel.ChannelCreated -= ChannelCreated;
                throw new AllocResourceFailedException("#Sadly, We couldn't alloc current network resource. #Uri: " + uri);
            }
            hostChannel.Tag = new KAENetworkResource {
                NetworkUri = uri, Protocol = ProtocolTypes.INTERNAL_SPECIAL_RESOURCE
            };
            _resources.Add(ProtocolTypes.INTERNAL_SPECIAL_RESOURCE, new Tuple <IHostTransportChannel, Uri>(hostChannel, uri));
            _tracing.DebugInfo("\t\t#Initialized network resource.\t(P: {0},\tURL: {1})", ConsoleColor.Gray, ProtocolTypes.INTERNAL_SPECIAL_RESOURCE, uri);
        }