Esempio n. 1
0
        public void OnMessage(PeerConnection peer, Messages.MineralMessage msg)
        {
            try
            {
                Logger.Refactoring(
                    string.Format("OnMessage : {0}", msg.Type.ToString()));

                switch (msg.Type)
                {
                case MessageTypes.MsgType.SYNC_BLOCK_CHAIN:
                {
                    Manager.Instance.SyncBlockHandler.ProcessMessage(peer, msg);
                }
                break;

                case MessageTypes.MsgType.BLOCK_CHAIN_INVENTORY:
                {
                    Manager.Instance.ChainInventoryHandler.ProcessMessage(peer, msg);
                }
                break;

                case MessageTypes.MsgType.INVENTORY:
                {
                    Manager.Instance.InventoryHandler.ProcessMessage(peer, msg);
                }
                break;

                case MessageTypes.MsgType.FETCH_INV_DATA:
                {
                    Manager.Instance.FetchInventoryHandler.ProcessMessage(peer, msg);
                }
                break;

                case MessageTypes.MsgType.BLOCK:
                {
                    Manager.Instance.BlockHandler.ProcessMessage(peer, msg);
                }
                break;

                case MessageTypes.MsgType.TXS:
                {
                    Manager.Instance.TransactionHandler.ProcessMessage(peer, msg);
                }
                break;

                default:
                {
                    throw new P2pException(P2pException.ErrorType.NO_SUCH_MESSAGE, msg.Type.ToString());
                }
                }
            }
            catch (System.Exception e)
            {
                ProcessException(peer, msg, e);
            }
        }
Esempio n. 2
0
        private void ProcessException(PeerConnection peer, Messages.MineralMessage message, System.Exception exception)
        {
            string     reason_message = "";
            ReasonCode code           = ReasonCode.Unknown;

            if (exception is P2pException)
            {
                P2pException.ErrorType type = ((P2pException)exception).Type;
                switch (type)
                {
                case P2pException.ErrorType.BAD_TRX:
                    code = ReasonCode.BadTx;
                    break;

                case P2pException.ErrorType.BAD_BLOCK:
                    code = ReasonCode.BadBlock;
                    break;

                case P2pException.ErrorType.NO_SUCH_MESSAGE:
                case P2pException.ErrorType.MESSAGE_WITH_WRONG_LENGTH:
                case P2pException.ErrorType.BAD_MESSAGE:
                    code = ReasonCode.BadProtocol;
                    break;

                case P2pException.ErrorType.SYNC_FAILED:
                    code = ReasonCode.SyncFail;
                    break;

                case P2pException.ErrorType.UNLINK_BLOCK:
                    code = ReasonCode.Unlinkable;
                    break;

                default:
                    code = ReasonCode.Unknown;
                    break;
                }

                reason_message = string.Format("Message from {0} process failed, {1} \n type: {2}, detail: {3}.",
                                               peer.Address,
                                               message,
                                               type,
                                               exception.Message);
                Logger.Error(reason_message);
            }
            else
            {
                code           = ReasonCode.Unknown;
                reason_message = string.Format("Message from {0} process failed, {1}",
                                               peer.Address,
                                               message);
                Logger.Error(reason_message);
            }

            peer.Disconnect(code, reason_message);
        }