/// <summary> /// 命令处理 /// </summary> /// <param name="str">收到的命令</param> /// <returns>是否继续接收</returns> internal bool OnCommandReceived(string str) { if (CommandReceived != null) { CommandReceived(this, new CommandReceivedEventArgs(str)); } bool continueReceive = true; string[] msg = str.Split(' '); if (msg[0] == "Exit") { OnAllFinished(); continueReceive = false; Stop(); } else if (msg[0] == "GET") { if (msg[1] == "FileBlock") { int blockIndex; if (!int.TryParse(msg[2], out blockIndex)) { throw new FormatException("Bad BlockIndex " + msg[2]); } SendBlock(blockIndex); } else if (msg[1] == "BlockHash") { int blockIndex; if (!int.TryParse(msg[2], out blockIndex)) { throw new FormatException("Bad BlockIndex " + msg[2]); } byte[] hash = Blocks[blockIndex].DataHash; SendStringAsync(string.Format("BlockHash {0} {1}", blockIndex, BitConverter.ToInt32(hash, 0))); } else if (msg[1] == "FileName") { SendStringAsync(string.Format("SET FileName {0}", FileName.DoReplace())); } else if (msg[1] == "TotalBlock") { SendStringAsync(string.Format("SET TotalBlock {0}", TotalBlock)); } else if (msg[1] == "LastBlockSize") { SendStringAsync(string.Format("SET LastBlockSize {0}", LastBlockSize)); } else { throw new FormatException("Bad Command " + msg[1]); } } else { throw new FormatException("Bad Command " + msg[0]); } return(continueReceive); }
/// <summary> /// (无try)命令处理 /// </summary> /// <param name="str">收到的命令</param> /// <returns>是否继续接收</returns> private bool onCommandReceived(string str) { bool _continueReceive = true; string[] _msgs = str.Split(' '); if (_msgs[0] == Consts.CMD_EXIT) { _continueReceive = false; OnAllFinished(); InnerStop(true); } else if (_msgs[0] == Consts.CMD_GET) { if (_msgs[1] == Consts.CMD_FILEBLOCK) { int _blockIndex; if (!int.TryParse(_msgs[2], out _blockIndex)) { throw new FormatException($"{Consts.INFO_BADBLOCKINDEX}{_msgs[2]}"); } sendBlock(_blockIndex); } else if (_msgs[1] == Consts.CMD_BLOCKHASH) { int _blockIndex; if (!int.TryParse(_msgs[2], out _blockIndex)) { throw new FormatException($"{Consts.INFO_BADBLOCKINDEX}{_msgs[2]}"); } byte[] _hash = _Blocks[_blockIndex].DataHash; SendStringAsync(string.Format("{0} {1} {2}", Consts.CMD_BLOCKHASH, _blockIndex, BitConverter.ToInt32(_hash, 0))); } else if (_msgs[1] == Consts.CMD_FILENAME) { SendStringAsync($"{Consts.CMD_SET} {Consts.CMD_FILENAME} {FileName.DoReplace()}"); } else if (_msgs[1] == Consts.CMD_TOTALBLOCK) { SendStringAsync($"{Consts.CMD_SET} {Consts.CMD_TOTALBLOCK} {_TotalBlockCount}"); } else if (_msgs[1] == Consts.CMD_LASTBLOCKSIZE) { SendStringAsync($"{Consts.CMD_SET} {Consts.CMD_LASTBLOCKSIZE} {_LastBlockSize}"); } else { throw new FormatException($"{Consts.INFO_BADCOMMAND} {_msgs[1]}"); } } else { throw new FormatException($"{Consts.INFO_BADCOMMAND} {_msgs[1]}"); } return(_continueReceive); }