public override bool DoTask(GameServer gameServer, IProtocol message) { if (gameServer == null || message == null) { throw new ArgumentException("gameServer & message can not be null."); } if (_taskState == TaskState.Processing) { FSEyeResult result; switch (message.ProtocolId) { case (UInt16)ProtocolDef.g2e_openfile_def: result = (FSEyeResult)((g2e_openfile)message).nRetCode; if (result == FSEyeResult.fseye_success) { if (_fileStream == null) { try { if (_fileDownloadAutomation != null) { //如果有Automation关联的话,则给下载文件的名字加前缀。 string fileName = SystemConfig.Current.AdminServerUploadFileRootPath + gameServer.Id + "_" + gameServer.Name + "_" + _targetFileName; _fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); } else { _fileStream = new FileStream(SystemConfig.Current.AdminServerUploadFileRootPath + _targetFileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); } } catch (Exception) { if (_fileDownloadAutomation != null) { _fileDownloadAutomation.FinishUnit(gameServer, false); } return(false); } } //得到文件长度 _fileTotalLength = ((g2e_openfile)message).nFileLen; if ((uint)_fileStream.Length > _fileTotalLength) { //错误 现有文件长度大于源文件 覆盖现有文件 _fileStream.SetLength(0); _offset = 0; } if (_overWrite) { //覆盖 _fileStream.SetLength(0); _offset = 0; } else { //续传 if (_fileStream.Length == _fileTotalLength) { //文件已经存在且完成 KProtocolHead protocol = new KProtocolHead(); protocol.ProtocolId = (ushort)ProtocolDef.e2g_closefile_def; gameServer.SendMessage(protocol); return(true); } else { _offset = (uint)_fileStream.Length; _fileStream.Position = _fileStream.Length; } } e2g_seekfile seekfile = new e2g_seekfile(); seekfile.bKeep = 1; seekfile.nOffset = _offset; //TODO 没有处理发包失败的情况 gameServer.SendMessage(seekfile); return(true); } else { //文件不存在?! Stop(gameServer); if (_fileDownloadAutomation != null) { _fileDownloadAutomation.FinishUnit(gameServer, false); } return(false); } case (UInt16)ProtocolDef.g2e_seekfile_def: result = (FSEyeResult)((g2e_seekfile)message).nRetCode; if (result == FSEyeResult.fseye_success) { e2g_readfile protocol = new e2g_readfile(); protocol.nDataLen = 4000; gameServer.SendMessage(protocol); return(true); } else { //FSEyeResult.filetran_seek_err //还有其他未知的错误 Stop(gameServer); if (_fileDownloadAutomation != null) { _fileDownloadAutomation.FinishUnit(gameServer, false); } return(false); } case (UInt16)ProtocolDef.g2e_readfile_def: result = (FSEyeResult)((g2e_readfile)message).nRetCode; if (result == FSEyeResult.fseye_success) { g2e_readfile receivedProtocol = message as g2e_readfile; _fileStream.Write(receivedProtocol.szBuf, 0, (int)receivedProtocol.szBuf.Length); _offset += (uint)receivedProtocol.szBuf.Length; CompletePercentage = (float)_fileStream.Length / (float)_fileTotalLength; if (CompletePercentage < 1) { if (_offset != _fileStream.Length) { //Guard端读文件偏移和本地写文件的偏移不同 e2g_seekfile seekProtocol = new e2g_seekfile(); seekProtocol.nOffset = (uint)_fileStream.Length; gameServer.SendMessage(seekProtocol); } else { e2g_readfile readProtocol = new e2g_readfile(); readProtocol.nDataLen = 4000; gameServer.SendMessage(readProtocol); } } else { KProtocolHead protocol = new KProtocolHead(); protocol.ProtocolId = (ushort)ProtocolDef.e2g_closefile_def; //TODO 没有处理发包失败的情况 gameServer.SendMessage(protocol); } return(true); } else { Stop(gameServer); if (_fileDownloadAutomation != null) { _fileDownloadAutomation.FinishUnit(gameServer, false); } return(false); } case (UInt16)ProtocolDef.g2e_closefile_def: result = (FSEyeResult)((g2e_closefile)message).nRetCode; if (result == FSEyeResult.fseye_success) { //TODO 添加关闭成功代码 Complete(gameServer); return(true); } else { if (_fileDownloadAutomation != null) { _fileDownloadAutomation.FinishUnit(gameServer, false); } return(false); } default: break; } } else if (_taskState == TaskState.Aborting) { if (message.ProtocolId == (UInt16)ProtocolDef.g2e_closefile_def) { _taskState = TaskState.Stopped; } return(true); } return(false); }
public override bool DoTask(GameServer gameServer, IProtocol message) { if (gameServer == null || message == null) { throw new ArgumentException("gameServer & message can not be null."); } if (_taskState == TaskState.Processing) { FSEyeResult result; switch (message.ProtocolId) { case (UInt16)ProtocolDef.g2e_openfile_def: result = (FSEyeResult)((g2e_openfile)message).nRetCode; if (result == FSEyeResult.fseye_success) { if (_fileStream == null) { try { if (_fileDownloadAutomation != null) { //如果有Automation关联的话,则给下载文件的名字加前缀。 string fileName = SystemConfig.Current.AdminServerUploadFileRootPath + gameServer.Id + "_" + gameServer.Name + "_" + _targetFileName; _fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); } else { _fileStream = new FileStream(SystemConfig.Current.AdminServerUploadFileRootPath + _targetFileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); } } catch (Exception) { if (_fileDownloadAutomation != null) _fileDownloadAutomation.FinishUnit(gameServer,false); return false; } } //得到文件长度 _fileTotalLength = ((g2e_openfile)message).nFileLen; if ((uint)_fileStream.Length > _fileTotalLength) { //错误 现有文件长度大于源文件 覆盖现有文件 _fileStream.SetLength(0); _offset = 0; } if (_overWrite) { //覆盖 _fileStream.SetLength(0); _offset = 0; } else { //续传 if (_fileStream.Length == _fileTotalLength) { //文件已经存在且完成 KProtocolHead protocol = new KProtocolHead(); protocol.ProtocolId = (ushort)ProtocolDef.e2g_closefile_def; gameServer.SendMessage(protocol); return true; } else { _offset = (uint)_fileStream.Length; _fileStream.Position = _fileStream.Length; } } e2g_seekfile seekfile = new e2g_seekfile(); seekfile.bKeep = 1; seekfile.nOffset = _offset; //TODO 没有处理发包失败的情况 gameServer.SendMessage(seekfile); return true; } else { //文件不存在?! Stop(gameServer); if (_fileDownloadAutomation != null) _fileDownloadAutomation.FinishUnit(gameServer,false); return false; } case (UInt16)ProtocolDef.g2e_seekfile_def: result = (FSEyeResult)((g2e_seekfile)message).nRetCode; if (result == FSEyeResult.fseye_success) { e2g_readfile protocol = new e2g_readfile(); protocol.nDataLen = 4000; gameServer.SendMessage(protocol); return true; } else { //FSEyeResult.filetran_seek_err //还有其他未知的错误 Stop(gameServer); if (_fileDownloadAutomation != null) _fileDownloadAutomation.FinishUnit(gameServer,false); return false; } case (UInt16)ProtocolDef.g2e_readfile_def: result = (FSEyeResult)((g2e_readfile)message).nRetCode; if (result == FSEyeResult.fseye_success) { g2e_readfile receivedProtocol = message as g2e_readfile; _fileStream.Write(receivedProtocol.szBuf, 0, (int)receivedProtocol.szBuf.Length); _offset += (uint)receivedProtocol.szBuf.Length; CompletePercentage = (float)_fileStream.Length / (float)_fileTotalLength; if (CompletePercentage < 1) { if (_offset != _fileStream.Length) { //Guard端读文件偏移和本地写文件的偏移不同 e2g_seekfile seekProtocol = new e2g_seekfile(); seekProtocol.nOffset = (uint)_fileStream.Length; gameServer.SendMessage(seekProtocol); } else { e2g_readfile readProtocol = new e2g_readfile(); readProtocol.nDataLen = 4000; gameServer.SendMessage(readProtocol); } } else { KProtocolHead protocol = new KProtocolHead(); protocol.ProtocolId = (ushort)ProtocolDef.e2g_closefile_def; //TODO 没有处理发包失败的情况 gameServer.SendMessage(protocol); } return true; } else { Stop(gameServer); if (_fileDownloadAutomation != null) _fileDownloadAutomation.FinishUnit(gameServer,false); return false; } case (UInt16)ProtocolDef.g2e_closefile_def: result = (FSEyeResult)((g2e_closefile)message).nRetCode; if (result == FSEyeResult.fseye_success) { //TODO 添加关闭成功代码 Complete(gameServer); return true; } else { if (_fileDownloadAutomation != null) _fileDownloadAutomation.FinishUnit(gameServer,false); return false; } default: break; } } else if (_taskState == TaskState.Aborting) { if (message.ProtocolId == (UInt16)ProtocolDef.g2e_closefile_def) { _taskState = TaskState.Stopped; } return true; } return false; }