Files
TouchSocket/RRQMSocket/Common/RRQMAgreementHelper.cs
若汝棋茗 e1687b6023 优化Protocol
2021-06-11 01:25:33 +08:00

149 lines
4.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//------------------------------------------------------------------------------
// 此代码版权归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议若本仓库没有设置则按MIT开源协议授权
// CSDN博客https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频https://space.bilibili.com/94253567
// Gitee源代码仓库https://gitee.com/RRQM_Home
// Github源代码仓库https://github.com/RRQM
// 交流QQ群234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using RRQMCore.ByteManager;
using System;
using System.Net.Sockets;
using System.Text;
namespace RRQMSocket
{
/// <summary>
/// RRQM协议助手
/// </summary>
public class RRQMAgreementHelper
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="client"></param>
public RRQMAgreementHelper(IClient client)
{
this.mainSocket = client.MainSocket;
this.bytePool = client.BytePool;
}
private Socket mainSocket;
private BytePool bytePool;
#region
/// <summary>
/// 发送简单协议
/// </summary>
/// <param name="agreement"></param>
public void SocketSend(short agreement)
{
this.SocketSend(agreement, new byte[0], 0, 0);
}
/// <summary>
/// 发送字节
/// </summary>
/// <param name="agreement"></param>
/// <param name="dataBuffer"></param>
public void SocketSend(short agreement, byte[] dataBuffer)
{
this.SocketSend(agreement, dataBuffer, 0, dataBuffer.Length);
}
/// <summary>
/// 发送协议流
/// </summary>
/// <param name="agreement"></param>
/// <param name="dataByteBlock"></param>
public void SocketSend(short agreement, ByteBlock dataByteBlock)
{
this.SocketSend(agreement, dataByteBlock.Buffer, 0, (int)dataByteBlock.Length);
}
///// <summary>
///// 发送字节
///// </summary>
///// <param name="agreement"></param>
///// <param name="status"></param>
///// <param name="dataBuffer"></param>
//public void SocketSend(short agreement, byte status, byte[] dataBuffer)
//{
// ByteBlock byteBlock = this.bytePool.GetByteBlock(dataBuffer.Length + 1);
// byteBlock.Write(status);
// byteBlock.Write(dataBuffer);
// try
// {
// this.SocketSend(agreement, byteBlock.Buffer, 0, (int)byteBlock.Length);
// }
// catch (Exception ex)
// {
// throw ex;
// }
// finally
// {
// byteBlock.Dispose();
// }
//}
///// <summary>
///// 发送流
///// </summary>
///// <param name="dataByteBlock"></param>
//public void SocketSend(ByteBlock dataByteBlock)
//{
// int dataLen = (int)dataByteBlock.Position + 4;
// ByteBlock byteBlock = this.bytePool.GetByteBlock(dataLen);
// byte[] lenBytes = BitConverter.GetBytes(dataLen);
// byteBlock.Write(lenBytes);
// byteBlock.Write(dataByteBlock.Buffer, 0, (int)dataByteBlock.Position);
// try
// {
// this.mainSocket.Send(byteBlock.Buffer, 0, (int)byteBlock.Position, SocketFlags.None);
// }
// finally
// {
// byteBlock.Dispose();
// }
//}
/// <summary>
/// 发送字节
/// </summary>
/// <param name="agreement"></param>
/// <param name="dataBuffer"></param>
/// <param name="offset"></param>
/// <param name="length"></param>
public void SocketSend(short agreement, byte[] dataBuffer, int offset, int length)
{
int dataLen = length - offset + 6;
ByteBlock byteBlock = this.bytePool.GetByteBlock(dataLen);
byte[] lenBytes = BitConverter.GetBytes(dataLen);
byte[] agreementBytes = BitConverter.GetBytes(agreement);
byteBlock.Write(lenBytes);
byteBlock.Write(agreementBytes);
if (length > 0)
{
byteBlock.Write(dataBuffer, offset, length);
}
try
{
this.mainSocket.Send(byteBlock.Buffer, 0, (int)byteBlock.Position, SocketFlags.None);
}
finally
{
byteBlock.Dispose();
}
}
#endregion
}
}