예제 #1
0
        protected void ConnectTcpClient(TcpClient tcpClient, StageAddress address)
        {
            if (!tcpClient.Connected)
            {
                lock (tcpClient)
                {
                    if (!tcpClient.Connected)
                    {
                        //out socket
                        tcpClient.NoDelay        = true;
                        tcpClient.SendTimeout    = 5;
                        tcpClient.ReceiveTimeout = 5;

                        var hostPort = address.Address.Split(new[] { "tcp://" }, StringSplitOptions.None)[1];
                        var host     = hostPort.Split(':')[0];

                        var port = Int32.Parse(hostPort.Split(':')[1]);

                        tcpClient.ConnectAsync(host, port).Wait();

                        Task.Run(async() => {
                            await ProcessSocket(tcpClient);

                            if (_outSockets.TryGetValue(address.Address, out var tcpClientList))
                            {
                                tcpClientList.Remove(tcpClient);
                            }
                        });
                    }
                }
            }
        }
예제 #2
0
        public async Task <bool> Send(StageAddress address, byte[] data)
        {
            if (address.Address == null)
            {
                address = await LocalAddress();
            }

            //try send on known peers


            var reqSocket = new NetMQ.Sockets.DealerSocket();

            reqSocket.Options.Identity = Guid.NewGuid().ToByteArray();

            reqSocket.Connect(address.Address);

            var messageToServer = new NetMQMessage();

            messageToServer.AppendEmptyFrame();
            messageToServer.Append(data);

            reqSocket.SendMultipartMessage(messageToServer);

            return(true);
        }
예제 #3
0
        public async Task <Boolean> Send(StageAddress address, byte[] data)
        {
            if (address.Address == null)
            {
                address = await LocalAddress();
            }

            TcpClient tcpClient;

            if (_isListening)
            {
                //on stage
                if (_inSockets.TryGetValue(address.Address, out tcpClient))
                {
                    return(await SendSocketMessage(tcpClient, data) > 0);
                }
            }


            //on client
            var tcpClientList = _outSockets.GetOrAdd(address.Address, new List <TcpClient>());

            if (tcpClientList.Count < _options.MaxClientSockets)
            {
                tcpClient = new TcpClient();
                lock (tcpClientList)
                {
                    tcpClientList.Add(tcpClient);
                }
            }
            else
            {
                tcpClient = tcpClientList.OrderBy(c => Guid.NewGuid()).First();
            }

            ConnectTcpClient(tcpClient, address);


            var sent = await SendSocketMessage(tcpClient, data);

            return(sent > 0);
        }