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 Stop(GameServer gameServer) { if (_taskState == TaskState.Processing || _taskState == TaskState.Paused) { if (_fileStream != null) { _fileStream.Close(); } KProtocolHead protocol = new KProtocolHead(); protocol.ProtocolId = (ushort)ProtocolDef.e2g_closefile_def; gameServer.SendMessage(protocol); _taskState = TaskState.Aborting; return true; } else { if (_fileDownloadAutomation != null) _fileDownloadAutomation.FinishUnit(gameServer,false); return false; } }
public override bool Continue(GameServer gameServer) { if (_taskState == TaskState.Paused) { _overWrite = false; e2g_openfile protocol = new e2g_openfile(); protocol.szFileName = _sourceFileName; protocol.bFlag = (byte)0; //0-读,1-写,2-续传 protocol.bFullPath = (byte)1; //0-相对,1-绝对 if (gameServer.SendMessage(protocol)) { _taskState = TaskState.Processing; return true; } } return false; }
public override bool Start(GameServer gameServer) { if (_taskState == TaskState.Created || _taskState == TaskState.Queuing) { e2g_openfile protocol = new e2g_openfile(); protocol.szFileName = _sourceFileName; protocol.bFlag = (byte)0; //0-读,1-写,2-续传 protocol.bFullPath = (byte)1; //0-相对,1-绝对 if (gameServer.SendMessage(protocol)) { _startTime = DateTime.Now; _taskState = TaskState.Processing; return true; } } if (_fileDownloadAutomation != null) _fileDownloadAutomation.FinishUnit(gameServer,false); 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) { _fileStream = new FileStream(SystemConfig.Current.AdminServerUploadFileRootPath + SourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read); } _fileTotalLength = (uint)_fileStream.Length; e2g_seekfile protocol = new e2g_seekfile(); protocol.nOffset = _offset; //TODO 没有处理发包失败的情况 gameServer.SendMessage(protocol); return true; } else if (result == FSEyeResult.filetran_app_err)//续传文件不存在 { //TODO 任务重新开始 e2g_openfile protocol = new e2g_openfile(); protocol.szFileName = _targetFileName; protocol.bFlag = 1; //TODO 没有处理发包失败的情况 gameServer.SendMessage(protocol); return true; } else { //FSEyeResult.filetran_opening_err//文件已经打开 //FSEyeResult.filetran_cre_err//文件创建失败 //还有其他未知的错误 //这些情况无法处理,只能终止操作 if (_fileTranAutomation != null) _fileTranAutomation.FinishUnit(gameServer, false); Stop(gameServer); return false; } case (UInt16)ProtocolDef.g2e_seekfile_def: result = (FSEyeResult)((g2e_seekfile)message).nRetCode; if (result == FSEyeResult.fseye_success) { e2g_writefile protocol = new e2g_writefile((int)_fileStream.Length); _fileStream.Seek(_offset, SeekOrigin.Begin); _sendLength = (UInt16)_fileStream.Read(protocol.szBuf, 0, (int)_fileStream.Length); Array.Resize(ref protocol.szBuf, _sendLength); //TODO 没有处理发包失败的情况 gameServer.SendMessage(protocol); return true; } else { //FSEyeResult.filetran_seek_err //还有其他未知的错误 //这些情况无法处理,只能终止操作 if (_fileTranAutomation != null) _fileTranAutomation.FinishUnit(gameServer, false); Stop(gameServer); return false; } case (UInt16)ProtocolDef.g2e_writefile_def: result = (FSEyeResult)((g2e_writefile)message).nRetCode; if (result == FSEyeResult.fseye_success) { uint guardWriteLength = ((g2e_writefile)message).nWritedLen; _offset += guardWriteLength; CompletePercentage = (float)_offset / (float)_fileTotalLength; if (CompletePercentage < 1) { if (guardWriteLength < _sendLength) { _fileStream.Seek(guardWriteLength - _sendLength, SeekOrigin.Current); } e2g_writefile protocol = new e2g_writefile((int)_fileStream.Length); _sendLength = (UInt16)_fileStream.Read(protocol.szBuf, 0, (int)_fileStream.Length); Array.Resize(ref protocol.szBuf, _sendLength); //TODO 没有处理发包失败的情况 gameServer.SendMessage(protocol); } else { KProtocolHead protocol = new KProtocolHead(); protocol.ProtocolId = (ushort)ProtocolDef.e2g_closefile_def; //TODO 没有处理发包失败的情况 gameServer.SendMessage(protocol); } return true; } else { //还有其他未知的错误 //这些情况无法处理,只能终止操作 if (_fileTranAutomation != null) _fileTranAutomation.FinishUnit(gameServer, false); Stop(gameServer); return false; } case (UInt16)ProtocolDef.g2e_closefile_def: result = (FSEyeResult)((g2e_closefile)message).nRetCode; if (result == FSEyeResult.fseye_success) { //通知绑定的Automation此文件传输完成 if (_fileTranAutomation != null) _fileTranAutomation.FinishUnit(gameServer, true); //TODO 添加关闭成功代码 Complete(gameServer); return true; } else { if (_fileTranAutomation != null) _fileTranAutomation.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 Continue(GameServer gameServer) { if (_taskState == TaskState.Paused) { e2g_openfile protocol = new e2g_openfile(); protocol.szFileName = _targetFileName; protocol.bFlag = (byte)1; if (gameServer.SendMessage(protocol)) { _taskState = TaskState.Processing; return true; } } if (_fileTranAutomation != null) _fileTranAutomation.FinishUnit(gameServer, false); return false; }