Beispiel #1
0
        /// <summary>
        /// Creates a channel to the given address. This will setup the TCP connection.
        /// </summary>
        /// <param name="remoteAddress">The remote address.</param>
        /// <param name="connectionTimeoutMillis">The timeout for establishing a TCP connection.</param>
        /// <param name="handlers">The handlers to be added to the channel's pipeline.</param>
        /// <returns></returns>
        public MyTcpClient CreateTcp(IPEndPoint remoteAddress, int connectionTimeoutMillis, IDictionary <string, IChannelHandler> handlers)
        {
            _readWriteLockTcp.EnterReadLock();
            try
            {
                if (_shutdownTcp)
                {
                    return(null);
                }
                // try to acquire resources for the channel
                if (_semaphoreTcp == null || !_semaphoreTcp.TryAcquire())
                {
                    const string errorMsg = "Tried to acquire more resources (TCP) than announced.";
                    Logger.Error(errorMsg);
                    throw new SystemException(errorMsg);
                }

                // create and bind
                var pipeline         = new Pipeline(handlers);
                var filteredPipeline = _channelClientConfiguration.PipelineFilter.Filter(pipeline, true, true);

                var tcpClient = new MyTcpClient(_externalBindings.WildcardSocket(), filteredPipeline);
                _recipients.Add(tcpClient);
                SetupCloseListener(tcpClient, _semaphoreTcp);

                // TODO how to set CONNECT_TIMEOUT_MILLIS option?
                tcpClient.Socket.NoDelay     = true;
                tcpClient.Socket.LingerState = new LingerOption(false, 0);
                tcpClient.Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                // connect
                try
                {
                    tcpClient.ConnectAsync(remoteAddress).Wait();
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex);
                    throw;
                }

                return(tcpClient);
            }
            finally
            {
                _readWriteLockTcp.ExitReadLock();
            }
        }
Beispiel #2
0
        public void StartTcp()
        {
            //if(!ms.ConnectClient())
            if (!ms.ConnectAsync())
            {
                Console.WriteLine(ms.ErroerMessage);
            }
            System.Threading.Thread.Sleep(2000);
            for (int i = 0; i < 10; i++)
            {
                if (!ms.SendData("it is test data", Encoding.UTF8))
                {
                    Console.WriteLine(ms.ErroerMessage);
                }
            }

            Console.WriteLine("any key to disconnect");
            Console.ReadKey();
            for (int i = 0; i < 10; i++)
            {
                System.Threading.Thread.Sleep(10);
                if (!ms.SendData("it is test data", Encoding.UTF8))
                {
                    Console.WriteLine(ms.ErroerMessage);
                }
            }


            for (int i = 0; i < 10; i++)
            {
                Console.ReadKey();
                byte[] bs = ms.ReceiveAllData();
                if (bs != null)
                {
                    ms_OnReceiveData(bs);
                }
            }
            //ms.disConnectClient();
        }