Example #1
0
        public Test()
        {
            this.thread = new JobProcessor();
            this.thread.Start();
            this.host1             = new Acceptor(this.thread);
            this.host2             = new Acceptor(this.thread);
            this.host2.OnAccepted += delegate(Peer peer)
            {
                peer.PipeOpen += delegate(Peer _, Pipe pipe)
                {
                    pipe.PacketReceived += delegate(Packet packet)
                    {
                        Log <Test> .Logger.DebugFormat("receive {0}", packet.Length);
                    };
                };
            };
            this.host1.Start(10000);
            this.host2.Start(10001);
            Peer peer2 = Peer.CreateClient(this.host1, new TcpClient());

            peer2.Connected += delegate(Peer peer)
            {
                peer.InitPipe().SendPacket(SerializeWriter.ToBinary <Test.TestMessage>(new Test.TestMessage
                {
                    Y = "asd"
                }));
            };
            peer2.Connect(new IPEndPoint(IPAddress.Loopback, 10001));
        }
Example #2
0
        internal void ConnectToIP(IPEndPoint location, Action <Peer> callback)
        {
            if (this.disposed)
            {
                callback(null);
                return;
            }
            Peer peer;

            if (!this.peers.TryGetValue(location, out peer))
            {
                peer = Peer.CreateClient(this.acceptor, this.CreateTcpClient());
                this.peers[location] = peer;
                peer.PipeOpen       += this.peer_OnPipeOpen;
                peer.Connected      += delegate(Peer x)
                {
                    if (this.OnConnected != null)
                    {
                        this.OnConnected(x);
                    }
                    Log <Service> .Logger.DebugFormat("connected to {0}", location);

                    callback(x);
                };
                peer.Disconnected += delegate(Peer x)
                {
                    Log <Service> .Logger.FatalFormat("disconnected peer {0}", x);

                    this.RemovePeer(location, x);
                };
                peer.ConnectionFailed += delegate(Peer x)
                {
                    Log <Service> .Logger.ErrorFormat("connection failed : {0}", location);

                    callback(null);
                    this.RemovePeer(location, x);
                };
                peer.Connect(location);
                return;
            }
            if (peer.IsConnected)
            {
                callback(peer);
                return;
            }
            peer.Connected += delegate(Peer x)
            {
                callback(peer);
            };
        }
Example #3
0
 public static void Start()
 {
     PipeTest.suite        = new PipeTest();
     PipeTest.suite.thread = new JobProcessor();
     PipeTest.suite.thread.ExceptionOccur += delegate(object sender, EventArgs <Exception> e)
     {
         Console.Error.WriteLine(e.Value);
     };
     PipeTest.suite.thread.Start();
     PipeTest.suite.acceptor             = new Acceptor(PipeTest.suite.thread);
     PipeTest.suite.acceptor.OnAccepted += PipeTest.suite.acceptor_OnAccepted;
     PipeTest.suite.acceptor.Start(42);
     PipeTest.suite.acceptor.RegisterPipeMessageGroup(PipeTestMessages.TypeConverters);
     PipeTest.suite.connector            = Peer.CreateClient(PipeTest.suite.acceptor, new TcpClient());
     PipeTest.suite.connector.Connected += PipeTest.suite.connector_Connected;
     PipeTest.suite.connector.Connect(new IPEndPoint(IPAddress.Loopback, 42));
 }