ab56a9bcf7
SVN-Revision: r240
271 lines
11 KiB
C#
271 lines
11 KiB
C#
/******************************
|
|
* 说明:远程文件Socket通讯协议
|
|
* 创建人:龚宇超
|
|
* 创建日期:2018-01-23
|
|
* 修改人:
|
|
* 修改日期:
|
|
* 修改备注:
|
|
* 版本:1.0.0.0
|
|
******************************/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Net.Sockets;
|
|
using System.IO;
|
|
|
|
namespace Lskj.SocketService
|
|
{
|
|
/// <summary>
|
|
/// 远程文件Socket通讯协议
|
|
/// </summary>
|
|
public class RemoteStreamSocketProtocol : BaseSocketProtocol
|
|
{
|
|
private FileStream _fileStream;
|
|
private byte[] _readBuffer;
|
|
|
|
public RemoteStreamSocketProtocol(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken)
|
|
: base(asyncSocketServer, asyncSocketUserToken)
|
|
{
|
|
_socketFlag = "RemoteStream";
|
|
_fileStream = null;
|
|
}
|
|
|
|
public override void Close()
|
|
{
|
|
base.Close();
|
|
if (_fileStream != null)
|
|
_fileStream.Close();
|
|
_fileStream = null;
|
|
}
|
|
|
|
public override bool ProcessCommand(byte[] buffer, int offset, int count) //处理分完包的数据,子类从这个方法继承
|
|
{
|
|
RemoteStreamSocketCommand command = StrToCommand(_incomingDataParser.Command);
|
|
_outgoingDataAssembler.Clear();
|
|
_outgoingDataAssembler.AddResponse();
|
|
_outgoingDataAssembler.AddCommand(_incomingDataParser.Command);
|
|
if (command == RemoteStreamSocketCommand.FileExists)
|
|
return DoFileExists();
|
|
else if (command == RemoteStreamSocketCommand.OpenFile)
|
|
return DoOpenFile();
|
|
else if (command == RemoteStreamSocketCommand.SetSize)
|
|
return DoSetSize();
|
|
else if (command == RemoteStreamSocketCommand.GetSize)
|
|
return DoGetSize();
|
|
else if (command == RemoteStreamSocketCommand.SetPosition)
|
|
return DoSetPosition();
|
|
else if (command == RemoteStreamSocketCommand.GetPosition)
|
|
return DoGetPosition();
|
|
else if (command == RemoteStreamSocketCommand.Read)
|
|
return DoRead();
|
|
else if (command == RemoteStreamSocketCommand.Write)
|
|
return DoWrite(buffer, offset, count);
|
|
else if (command == RemoteStreamSocketCommand.Seek)
|
|
return DoSeek();
|
|
else if (command == RemoteStreamSocketCommand.CloseFile)
|
|
return DoCloseFile();
|
|
else
|
|
{
|
|
Program.Logger.Error("Unknow command: " + _incomingDataParser.Command);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public RemoteStreamSocketCommand StrToCommand(string command)
|
|
{
|
|
if (command.Equals(ProtocolKey.FileExists, StringComparison.CurrentCultureIgnoreCase))
|
|
return RemoteStreamSocketCommand.FileExists;
|
|
else if (command.Equals(ProtocolKey.OpenFile, StringComparison.CurrentCultureIgnoreCase))
|
|
return RemoteStreamSocketCommand.OpenFile;
|
|
else if (command.Equals(ProtocolKey.SetSize, StringComparison.CurrentCultureIgnoreCase))
|
|
return RemoteStreamSocketCommand.SetSize;
|
|
else if (command.Equals(ProtocolKey.GetSize, StringComparison.CurrentCultureIgnoreCase))
|
|
return RemoteStreamSocketCommand.GetSize;
|
|
else if (command.Equals(ProtocolKey.SetPosition, StringComparison.CurrentCultureIgnoreCase))
|
|
return RemoteStreamSocketCommand.SetPosition;
|
|
else if (command.Equals(ProtocolKey.GetPosition, StringComparison.CurrentCultureIgnoreCase))
|
|
return RemoteStreamSocketCommand.GetPosition;
|
|
else if (command.Equals(ProtocolKey.Read, StringComparison.CurrentCultureIgnoreCase))
|
|
return RemoteStreamSocketCommand.Read;
|
|
else if (command.Equals(ProtocolKey.Write, StringComparison.CurrentCultureIgnoreCase))
|
|
return RemoteStreamSocketCommand.Write;
|
|
else if (command.Equals(ProtocolKey.Seek, StringComparison.CurrentCultureIgnoreCase))
|
|
return RemoteStreamSocketCommand.Seek;
|
|
else if (command.Equals(ProtocolKey.CloseFile, StringComparison.CurrentCultureIgnoreCase))
|
|
return RemoteStreamSocketCommand.CloseFile;
|
|
else
|
|
return RemoteStreamSocketCommand.None;
|
|
}
|
|
|
|
public bool DoFileExists()
|
|
{
|
|
string filename = "";
|
|
if (_incomingDataParser.GetValue(ProtocolKey.FileName, ref filename))
|
|
{
|
|
if (File.Exists(filename))
|
|
_outgoingDataAssembler.AddSuccess();
|
|
else
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.FileNotExist, "file not exists");
|
|
}
|
|
else
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.ParameterError, "");
|
|
return DoSendResult();
|
|
}
|
|
|
|
public bool DoOpenFile()
|
|
{
|
|
string filename = "";
|
|
short mode = 0;
|
|
if (_incomingDataParser.GetValue(ProtocolKey.FileName, ref filename) & _incomingDataParser.GetValue(ProtocolKey.Mode, ref mode))
|
|
{
|
|
RemoteStreamMode readWriteMode = (RemoteStreamMode)mode;
|
|
if (File.Exists(filename))
|
|
{
|
|
if (readWriteMode == RemoteStreamMode.Read)
|
|
_fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
|
|
else
|
|
_fileStream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
|
|
}
|
|
else
|
|
_fileStream = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite);
|
|
_outgoingDataAssembler.AddSuccess();
|
|
}
|
|
else
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.ParameterError, "");
|
|
return DoSendResult();
|
|
}
|
|
|
|
public bool DoSetSize()
|
|
{
|
|
long fileSize = 0;
|
|
if (_incomingDataParser.GetValue(ProtocolKey.Size, ref fileSize))
|
|
{
|
|
if (_fileStream == null)
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.NotOpenFile, "");
|
|
else
|
|
{
|
|
_fileStream.SetLength(fileSize);
|
|
_outgoingDataAssembler.AddSuccess();
|
|
}
|
|
}
|
|
else
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.ParameterError, "");
|
|
return DoSendResult();
|
|
}
|
|
|
|
public bool DoGetSize()
|
|
{
|
|
if (_fileStream == null)
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.NotOpenFile, "");
|
|
else
|
|
{
|
|
_outgoingDataAssembler.AddSuccess();
|
|
_outgoingDataAssembler.AddValue(ProtocolKey.Size, _fileStream.Length);
|
|
}
|
|
return DoSendResult();
|
|
}
|
|
|
|
public bool DoSetPosition()
|
|
{
|
|
long position = 0;
|
|
if (_incomingDataParser.GetValue(ProtocolKey.Position, ref position))
|
|
{
|
|
if (_fileStream == null)
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.NotOpenFile, "");
|
|
else
|
|
{
|
|
_fileStream.Position = position;
|
|
_outgoingDataAssembler.AddSuccess();
|
|
}
|
|
}
|
|
else
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.ParameterError, "");
|
|
return DoSendResult();
|
|
}
|
|
|
|
public bool DoGetPosition()
|
|
{
|
|
if (_fileStream == null)
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.NotOpenFile, "");
|
|
else
|
|
{
|
|
_outgoingDataAssembler.AddSuccess();
|
|
_outgoingDataAssembler.AddValue(ProtocolKey.Position, _fileStream.Position);
|
|
}
|
|
return DoSendResult();
|
|
}
|
|
|
|
public bool DoRead()
|
|
{
|
|
int count = 0;
|
|
if (_incomingDataParser.GetValue(ProtocolKey.Count, ref count))
|
|
{
|
|
if (_fileStream == null)
|
|
{
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.NotOpenFile, "");
|
|
}
|
|
else
|
|
{
|
|
if (_readBuffer == null)
|
|
_readBuffer = new byte[count];
|
|
else if (_readBuffer.Length < count) //避免多次申请内存
|
|
_readBuffer = new byte[count];
|
|
count = _fileStream.Read(_readBuffer, 0, count);
|
|
_outgoingDataAssembler.AddSuccess();
|
|
_outgoingDataAssembler.AddValue(ProtocolKey.Count, count); //返回读取个数
|
|
}
|
|
}
|
|
else
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.ParameterError, "");
|
|
return DoSendResult(_readBuffer, 0, count);
|
|
}
|
|
|
|
public bool DoWrite(byte[] buffer, int offset, int count)
|
|
{
|
|
if (_fileStream == null)
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.NotOpenFile, "");
|
|
else
|
|
{
|
|
_fileStream.Write(buffer, offset, count);
|
|
_outgoingDataAssembler.AddSuccess();
|
|
_outgoingDataAssembler.AddValue(ProtocolKey.Count, count); //返回写入个数
|
|
}
|
|
return DoSendResult();
|
|
}
|
|
|
|
public bool DoSeek()
|
|
{
|
|
long offset = 0;
|
|
int seekOrign = 0;
|
|
if (_incomingDataParser.GetValue(ProtocolKey.Offset, ref offset) & _incomingDataParser.GetValue(ProtocolKey.SeekOrigin, ref seekOrign))
|
|
{
|
|
if (_fileStream == null)
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.NotOpenFile, "");
|
|
else
|
|
{
|
|
offset = _fileStream.Seek(offset, (SeekOrigin)seekOrign);
|
|
_outgoingDataAssembler.AddSuccess();
|
|
_outgoingDataAssembler.AddValue(ProtocolKey.Offset, offset);
|
|
}
|
|
}
|
|
else
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.ParameterError, "");
|
|
return DoSendResult();
|
|
}
|
|
|
|
public bool DoCloseFile()
|
|
{
|
|
if (_fileStream == null)
|
|
_outgoingDataAssembler.AddFailure(ProtocolCode.NotOpenFile, "");
|
|
else
|
|
{
|
|
_fileStream.Close();
|
|
_fileStream = null;
|
|
_outgoingDataAssembler.AddSuccess();
|
|
}
|
|
return DoSendResult();
|
|
}
|
|
}
|
|
}
|