Beispiel #1
0
        public TcpPeer(TcpClient client, TcpRaftNode rn)
        {
            _client = client;
            trn     = rn;
            try
            {
                stream = _client.GetStream();
                SetupSprot();
            }
            catch (Exception ex)
            {
                return;
            }

            trn.GetNodeByEntityName("default").TM.FireEventEach(10000, (o) =>
            {
                if (Handshake == null)
                {
                    this.Dispose();
                }
            }, null, true);

            Task.Run(async() => await Read());
        }
Beispiel #2
0
        public void AddPeerToClusterEndPoints(TcpPeer peer, bool handshake)
        {
            _sync.EnterWriteLock();
            try
            {
                if (peer.Handshake.NodeUID == trn.GetNodeByEntityName("default").NodeAddress.NodeUId)   //Self disconnect
                {
                    trn.NodeSettings.TcpClusterEndPoints.Where(r => r.EndPointSID == peer.EndPointSID)
                    .FirstOrDefault().Me = true;

                    peer.Dispose(true);
                    return;
                }

                //Choosing priority connection
                if (!Peers.ContainsKey(peer.EndPointSID))
                {
                    if (handshake && trn.GetNodeByEntityName("default").NodeAddress.NodeUId > peer.Handshake.NodeUID)
                    {
                        //trn.log.Log(new WarningLogEntry()
                        //{
                        //    LogType = WarningLogEntry.eLogType.DEBUG,
                        //    Description = $"{trn.port}> !!!!!dropped{peer.Handshake.NodeListeningPort} {peer.Handshake.NodeListeningPort} on handshake as weak"
                        //});


                        peer.Dispose(true);
                        return;
                    }

                    Peers[peer.EndPointSID] = peer;
                    peer.FillNodeAddress();

                    //trn.log.Log(new WarningLogEntry()
                    //{
                    //    LogType = WarningLogEntry.eLogType.DEBUG,
                    //    Description = $"{trn.port}> >>>>>>connected{peer.Handshake.NodeListeningPort}  {peer.Handshake.NodeListeningPort} by {(handshake ? "handshake" : "ACK")} with diff: {(trn.rn.NodeAddress.NodeUId - peer.Handshake.NodeUID)}"
                    //});

                    if (handshake)
                    {
                        //sending back handshake ack

                        peer.Write(
                            cSprot1Parser.GetSprot1Codec(
                                new byte[] { 00, 03 }, (new TcpMsgHandshake()
                        {
                            NodeListeningPort = trn.port,
                            NodeUID = trn.GetNodeByEntityName("default").NodeAddress.NodeUId,
                        }).SerializeBiser())
                            );
                    }
                }
                else
                {
                    //trn.log.Log(new WarningLogEntry()
                    //{
                    //    LogType = WarningLogEntry.eLogType.DEBUG,
                    //    Description = $"{trn.port}> !!!!!dropped{peer.Handshake.NodeListeningPort} {peer.Handshake.NodeListeningPort} as existing"
                    //});

                    //Sending ping on existing connection (may be it is alredy old)

                    Peers[peer.EndPointSID].Write(cSprot1Parser.GetSprot1Codec(new byte[] { 00, 05 }, null)); //ping

                    //removing incoming connection
                    peer.Dispose(true);

                    return;
                }
            }
            catch (Exception ex)
            {
                //throw;
            }
            finally
            {
                _sync.ExitWriteLock();
            }
        }
Beispiel #3
0
        private void packetParser(int codec, byte[] data)
        {
            try
            {
                switch (codec)
                {
                case 1:     //Handshake

                    Handshake = TcpMsgHandshake.BiserDecode(data);
                    if (trn.GetNodeByEntityName("default").NodeAddress.NodeUId != this.Handshake.NodeUID)
                    {
                        //trn.log.Log(new WarningLogEntry()
                        //{
                        //    LogType = WarningLogEntry.eLogType.DEBUG,
                        //    Description = $"{trn.port}> handshake from {this.Handshake.NodeListeningPort}"
                        //});
                    }
                    trn.spider.AddPeerToClusterEndPoints(this, true);
                    return;

                case 2:     //RaftMessage

                    if (this.na == null)
                    {
                        return;
                    }

                    var msg = TcpMsgRaft.BiserDecode(data);

                    Task.Run(() =>
                    {
                        trn.GetNodeByEntityName(msg.EntityName)
                        .IncomingSignalHandler(this.na, msg.RaftSignalType, msg.Data);
                    });
                    return;

                case 3:     //Handshake ACK

                    Handshake = TcpMsgHandshake.BiserDecode(data);
                    //trn.log.Log(new WarningLogEntry()
                    //{
                    //    LogType = WarningLogEntry.eLogType.DEBUG,
                    //    Description = $"{trn.port}> ACK from {this.Handshake.NodeListeningPort}"
                    //});
                    trn.spider.AddPeerToClusterEndPoints(this, false);

                    return;

                case 4:     //Free Message protocol

                    var Tcpmsg = TcpMsg.BiserDecode(data);
                    if (na != null)
                    {
                        trn.log.Log(new WarningLogEntry()
                        {
                            LogType     = WarningLogEntry.eLogType.DEBUG,
                            Description = $"{trn.port} ({trn.GetNodeByEntityName("default").NodeState})> peer {na.NodeAddressId} sent: { Tcpmsg.MsgType }"
                        });
                    }
                    return;

                case 5:     //Ping

                    //if (na != null)
                    //{
                    //    trn.log.Log(new WarningLogEntry()
                    //    {
                    //        LogType = WarningLogEntry.eLogType.DEBUG,
                    //        Description = $"{trn.port} ({trn.rn.NodeState})> peer {na.NodeAddressId} sent ping"
                    //    });
                    //}
                    return;
                }
            }
            catch (Exception ex)
            {
                Dispose();
            }
        }