예제 #1
0
        private void versionMsgHandle(P2PState state)
        {
            var peer = this.Peers.Where(p => p.IP == state.IP && p.Port == state.Port).FirstOrDefault();

            if (peer != null)
            {
                var versionMsg = new VersionMsg();
                int index      = 0;
                versionMsg.Deserialize(state.Command.Payload, ref index);
                bool checkResult;

                if (versionMsg.Version < Versions.MinimumSupportVersion)
                {
                    checkResult = false;
                    var data = new RejectMsg();
                    data.ReasonCode = ErrorCode.Engine.P2P.Connection.P2P_VERSION_NOT_BE_SUPPORT_BY_REMOTE_PEER;

                    var rejectCommand = P2PCommand.CreateCommand(CommandNames.Other.Reject, data);
                    this.Send(rejectCommand, state.IP, state.Port);

                    this.RemovePeer(state.IP, state.Port);
                }
                else if (Math.Abs(Time.EpochTime - versionMsg.Timestamp) > 2 * 60 * 60 * 1000)
                {
                    checkResult = false;
                    var data = new RejectMsg();
                    data.ReasonCode = ErrorCode.Engine.P2P.Connection.TIME_NOT_MATCH_WITH_RMOTE_PEER;

                    var rejectCommand = P2PCommand.CreateCommand(CommandNames.Other.Reject, data);
                    this.Send(rejectCommand, state.IP, state.Port);
                }
                else
                {
                    peer.Version = versionMsg.Version;
                    checkResult  = true;
                }

                if (checkResult)
                {
                    var verAckCommand = P2PCommand.CreateCommand(CommandNames.P2P.VerAck, null);
                    this.Send(verAckCommand, state.IP, state.Port);
                }
            }
        }
예제 #2
0
        private void pingMsgHandle(P2PState state)
        {
            var peer = this.Peers.Where(p => p.IP == state.IP && p.Port == state.Port).FirstOrDefault();

            if (peer == null)
            {
                if (this.Peers.Count < this.maxConnections)
                {
                    var newPeer = new P2PNode();
                    newPeer.IP              = state.IP;
                    newPeer.Port            = state.Port;
                    newPeer.IsConnected     = false;
                    newPeer.ConnectedTime   = Time.EpochTime;
                    newPeer.IsTrackerServer = false;
                    newPeer.IsInbound       = true;

                    this.Peers.Add(newPeer);

                    if (this.receivedMessageBuffer.ContainsKey(state.IP + ":" + state.Port))
                    {
                        this.receivedMessageBuffer[state.IP + ":" + state.Port] = new List <byte>();
                    }
                    else
                    {
                        this.receivedMessageBuffer.Add(state.IP + ":" + state.Port, new List <byte>());
                    }

                    var pongCommand = P2PCommand.CreateCommand(CommandNames.P2P.Pong, null);
                    this.Send(pongCommand, newPeer.IP, newPeer.Port);

                    var verPayload = new VersionMsg();
                    verPayload.Version   = Versions.EngineVersion;
                    verPayload.Timestamp = Time.EpochTime;

                    var versionCommand = P2PCommand.CreateCommand(CommandNames.P2P.Version, verPayload);
                    this.Send(versionCommand, state.IP, state.Port);
                }
                else
                {
                    var payload = new RejectMsg();
                    payload.ReasonCode = ErrorCode.Engine.P2P.Connection.THE_NUMBER_OF_CONNECTIONS_IS_FULL;

                    var rejectCommand = P2PCommand.CreateCommand(CommandNames.Other.Reject, payload);
                    this.Send(rejectCommand, state.IP, state.Port);
                }
            }
            else
            {
                var pongCommand = P2PCommand.CreateCommand(CommandNames.P2P.Pong, null);
                this.Send(pongCommand, state.IP, state.Port);

                var verPayload = new VersionMsg();
                verPayload.Version   = Versions.EngineVersion;
                verPayload.Timestamp = Time.EpochTime;

                var versionCommand = P2PCommand.CreateCommand(CommandNames.P2P.Version, verPayload);
                this.Send(versionCommand, state.IP, state.Port);

                if (!this.receivedMessageBuffer.ContainsKey(state.IP + ":" + state.Port))
                {
                    this.receivedMessageBuffer.Add(state.IP + ":" + state.Port, new List <byte>());
                }

                //var payload = new RejectMsg();
                //payload.ReasonCode = ErrorCode.Engine.P2P.Connection.THE_PEER_IS_EXISTED;

                //var rejectCommand = P2PCommand.CreateCommand(CommandNames.Other.Reject, payload);
                //this.Send(rejectCommand, state.IP, state.Port);
            }
        }
예제 #3
0
        public JsonResult procemsg([FromBody] string messagecont)
        {
            Result    _Result    = new Result();
            RejectMsg _RejectMsg = new RejectMsg();

            string cost_centre = "", payment_method = "", val = "";
            bool   message_rejected = false;
            double total = 0, total_excluding_GST = 0, GST = 0;

            string          regularExpressionPattern = @"<(\{1})?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)>";
            Regex           regex      = new Regex(regularExpressionPattern, RegexOptions.Compiled);
            MatchCollection collection = regex.Matches(messagecont);

            foreach (Match match in collection)
            {
                int Start, End;
                if (messagecont.Contains(match.Value) && messagecont.Contains(match.Value.Replace(@"<", @"</")))
                {
                    Start = messagecont.IndexOf(match.Value, 0, StringComparison.CurrentCulture) + match.Value.Length;

                    End = messagecont.IndexOf(match.Value.Replace(@"<", @"</"), Start, StringComparison.CurrentCulture);

                    val = messagecont.Substring(Start, End - Start);

                    if (match.Value.Contains("<cost_centre>"))
                    {
                        cost_centre = val;
                    }
                    else if (match.Value.Contains("<payment_method>"))
                    {
                        payment_method = val;
                    }

                    else if (match.Value.Contains("<total>"))
                    {
                        total = Convert.ToDouble(val);
                    }
                }
                else
                {
                    message_rejected = true;

                    break;
                }
            }

            if (cost_centre.Length == 0)
            {
                cost_centre = "UNKNOWN";
            }

            if (total <= 0)
            {
                message_rejected = true;
            }
            if (message_rejected)
            {
                _RejectMsg = new RejectMsg()
                {
                    message_rejected = "message is rejected"
                };
                return(Json(_RejectMsg, JsonRequestBehavior.AllowGet));
            }
            else
            {
                total_excluding_GST = total / 1.15;
                GST = total - total_excluding_GST;


                _Result = new Result()
                {
                    cost_centre         = cost_centre,
                    payment_method      = payment_method,
                    total               = total,
                    total_excluding_GST = total_excluding_GST,
                    GST = GST
                };
            }

            return(Json(_Result, JsonRequestBehavior.AllowGet));
        }