Beispiel #1
0
        public async Task OnRecieve(IChannelHandlerContext context, string msgstr)
        {
            //
            if (!string.IsNullOrEmpty(msgstr))
            {
                //RaftCommand  msgObj = Newtonsoft.Json.JsonConvert.DeserializeObject<RaftCommand>(msgstr, new JsonSerializerSettings()
                //{
                //    NullValueHandling = NullValueHandling.Ignore,
                //    TypeNameHandling = TypeNameHandling.All,
                //    TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Full
                //}
                //);
                int         index  = msgstr.IndexOf(',');
                string      num    = msgstr.Substring(0, index);
                string      base64 = msgstr.Substring(index + 1);
                byte[]      data   = Convert.FromBase64String(base64);
                RaftCommand cmd    = new RaftCommand();
                cmd.Code = Convert.ToInt32(num);
                switch (cmd.Code)
                {
                case RaftCommand.Handshake:
                    cmd.Message = TcpMsgHandshake.BiserDecode(data);
                    break;

                case RaftCommand.HandshakeACK:
                    cmd.Message = TcpMsgHandshake.BiserDecode(data);
                    break;

                case RaftCommand.RaftMessage:
                    cmd.Message = TcpMsgRaft.BiserDecode(data);
                    TcpMsgRaft t = (TcpMsgRaft)cmd.Message;
                    switch (t.RaftSignalType)
                    {
                    case eRaftSignalType.LeaderHearthbeat:
                        t.orginalObject = LeaderHeartbeat.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.CandidateRequest:
                        t.orginalObject = CandidateRequest.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.StateLogEntryAccepted:
                        t.orginalObject = StateLogEntryApplied.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.StateLogEntryRequest:
                        t.orginalObject = StateLogEntryRequest.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.StateLogEntrySuggestion:
                        t.orginalObject = StateLogEntrySuggestion.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.StateLogRedirectRequest:
                        t.orginalObject = StateLogEntryRedirectRequest.BiserDecode(t.Data);
                        break;

                    case eRaftSignalType.VoteOfCandidate:
                        t.orginalObject = VoteOfCandidate.BiserDecode(t.Data);
                        break;
                    }
                    break;

                case RaftCommand.FreeMessage:
                    cmd.Message = TcpMsg.BiserDecode(data);
                    break;
                }
                this.packetParser(cmd as RaftCommand);
            }
        }
Beispiel #2
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();
            }
        }