public override void Send(IPEndPoint receiver, Message message)
        {
            IPEndPoint instance = FindEndpoint(receiver.Address.ToString(), receiver.Port);

            if (instance == null)
            {
                Logger.Error($"Destination is not allowed: {receiver.Address}:{receiver.Port}");
            }
            else if (_socketLookup.ContainsKey(instance) && _socketLookup[instance] != null)
            {
                Send(_socketLookup[instance], message);
            }
            else
            {
                SyncReceiveTcpClient client = new SyncReceiveTcpClient(this);
                client.ConnectError += ClientOnConnectError;
                Register(client, instance);
                TcpBridgeRegistration registration = new TcpBridgeRegistration(_settings.PublicEndPoint);
                Queue(client, registration);
                Queue(client, message);
                client.Connect(receiver.Address, (ushort)receiver.Port, TimeSpan.FromSeconds(30));
            }
        }
        public TcpConnectionDemo()
        {
            LogProvider.GlobalLogWrite += LogProviderOnLogWrite;

            EventConsumer serverConsumer = new EventConsumer();

            serverConsumer.ClientConnected    += ServerConsumerOnClientConnected;
            serverConsumer.ClientDisconnected += ServerConsumerOnClientDisconnected;
            serverConsumer.ReceivedPacket     += ServerConsumerOnReceivedPacket;
            AsyncEventServer server = new AsyncEventServer(IPAddress.Any, 2345, serverConsumer);

            server.Start();

            EventConsumer clientConsumer = new EventConsumer();

            clientConsumer.ClientConnected    += ClientConsumerOnClientConnected;
            clientConsumer.ClientDisconnected += ClientConsumerOnClientDisconnected;
            clientConsumer.ReceivedPacket     += ClientConsumerOnReceivedPacket;
            SyncReceiveTcpClient client = new SyncReceiveTcpClient(clientConsumer);

            client.ConnectError += ClientConsumerOnConnectError;


            client.Connect(IPAddress.Parse("127.0.0.1"), 2345, TimeSpan.Zero);
            Console.WriteLine("Demo: Press any key to send.");
            Console.ReadKey();

            client.Send(new byte[9]);
            Console.WriteLine("Demo: Press any key to disconnect.");
            Console.ReadKey();

            client.Close();
            server.Stop();
            Console.WriteLine("Demo: Press any key to exit.");
            Console.ReadKey();
        }
Ejemplo n.º 3
0
 public void Connect()
 {
     _client.Connect("127.0.0.1", 2345, TimeSpan.Zero);
 }