Example #1
0
 public TcpPeer(IChannelHandlerContext client, RaftServiceNode rn)
 {
     _nettyclient = client;
     trn          = rn;
     trn.GetNode().timerLoop.TM.FireEventEach(10000, (o) =>
     {
     }, null, true);
 }
        public void AddPeerToClusterEndPoints(TcpPeer peer, bool handshake)
        {
            _sync.EnterWriteLock();
            try
            {
                //Choosing priority connection
                if (!Peers.ContainsKey(peer.EndPointSID))
                {
                    Peers[peer.EndPointSID] = peer;
                    peer.FillNodeAddress();

                    if (handshake)
                    {
                        peer.Send(RaftCommand.HandshakeACK, new TcpMsgHandshake()
                        {
                            NodeListeningPort = trn.port,
                            NodeUID           = trn.GetNode().NodeAddress.NodeUId,
                        });
                    }
                }
                else
                {
                    //Peers[peer.EndPointSID].Send(RaftCommand.Ping,"ping"); //ping
                    //removing incoming connection
                    peer.Dispose(true);
                    return;
                }
            }
            catch (Exception ex)
            {
                //throw;
            }
            finally
            {
                _sync.ExitWriteLock();
            }
        }
Example #3
0
        private void packetParser(RaftCommand message)
        {
            try
            {
                switch (message.Code)
                {
                case RaftCommand.Handshake:     //Handshake

                    Handshake = message.Message as TcpMsgHandshake;
                    trn.peerNetwork.AddPeerToClusterEndPoints(this, true);
                    return;

                case RaftCommand.RaftMessage:     //RaftMessage

                    if (this.na == null)
                    {
                        return;
                    }
                    var msg = message.Message as TcpMsgRaft;
                    Task.Run(() =>
                    {
                        try
                        {
                            var node = trn.GetNode();
                            if (node != null)
                            {
                                node.HandleRaftSignal(this.na, msg.RaftSignalType, msg.orginalObject);
                            }
                            else
                            {
                                Console.WriteLine("cant find node");
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                        }
                    });
                    return;

                case RaftCommand.HandshakeACK:     //Handshake ACK
                    Handshake = message.Message as TcpMsgHandshake;
                    trn.peerNetwork.AddPeerToClusterEndPoints(this, false);
                    return;

                case RaftCommand.FreeMessage:     //Free Message protocol
                    var Tcpmsg = message.Message as TcpMsg;
                    if (na != null)
                    {
                        trn.log.Log(new WarningLogEntry()
                        {
                            LogType     = WarningLogEntry.eLogType.DEBUG,
                            Description = $"{trn.port} ({trn.GetNode().States.NodeState})> peer {na.NodeAddressId} sent: { Tcpmsg.MsgType }"
                        });
                    }
                    return;

                case RaftCommand.Ping:     //Ping
                    return;
                }
            }
            catch (Exception ex)
            {
                Dispose();
            }
        }