using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HslCommunication.Core.Net;
using HslCommunication.Core.IMessage;
using HslCommunication.Core;
using System.Net.Sockets;
#if !NET35
using System.Threading.Tasks;
#endif
namespace HslCommunication.Enthernet
{
///
/// 同步访问数据的客户端类,用于向服务器请求一些确定的数据信息
///
///
/// 详细的使用说明,请参照博客http://www.cnblogs.com/dathlin/p/7697782.html
///
///
/// 此处贴上了Demo项目的服务器配置的示例代码
///
///
public class NetSimplifyClient : NetworkDoubleBase
{
#region Constructor
///
/// 实例化一个客户端的对象,用于和服务器通信
///
/// 服务器的ip地址
/// 服务器的端口号
public NetSimplifyClient( string ipAddress, int port )
{
IpAddress = ipAddress;
Port = port;
}
///
/// 实例化一个客户端对象,需要手动指定Ip地址和端口
///
public NetSimplifyClient( )
{
}
#endregion
#region Override NetworkDoubleBase
///
/// 连接上服务器后需要进行的初始化操作,无论是否允许操作都要进行验证
///
/// 网络套接字
/// 是否初始化成功,依据具体的协议进行重写
protected override OperateResult InitializationOnConnect( Socket socket )
{
if (isUseAccountCertificate)
{
return AccountCertificate( socket );
}
return OperateResult.CreateSuccessResult( );
}
#endregion
///
/// 客户端向服务器进行请求,请求字符串数据,忽略了自定义消息反馈
///
/// 用户的指令头
/// 发送数据
/// 带返回消息的结果对象
public OperateResult ReadFromServer( NetHandle customer, string send )
{
var read = ReadFromServerBase( HslProtocol.CommandBytes( customer, Token, send ) );
if (!read.IsSuccess) return OperateResult.CreateFailedResult( read );
return OperateResult.CreateSuccessResult( Encoding.Unicode.GetString( read.Content ) );
}
///
/// 客户端向服务器进行请求,请求字符串数组,忽略了自定义消息反馈
///
/// 用户的指令头
/// 发送数据
/// 带返回消息的结果对象
public OperateResult ReadFromServer( NetHandle customer, string[] send )
{
var read = ReadFromServerBase( HslProtocol.CommandBytes( customer, Token, send ) );
if (!read.IsSuccess) return OperateResult.CreateFailedResult( read );
return OperateResult.CreateSuccessResult( HslProtocol.UnPackStringArrayFromByte( read.Content ) );
}
///
/// 客户端向服务器进行请求,请求字节数据
///
/// 用户的指令头
/// 发送的字节内容
/// 带返回消息的结果对象
public OperateResult ReadFromServer( NetHandle customer, byte[] send )
{
return ReadFromServerBase( HslProtocol.CommandBytes( customer, Token, send ) );
}
///
/// 客户端向服务器进行请求,请求字符串数据,并返回状态信息
///
/// 用户的指令头
/// 发送数据
/// 带返回消息的结果对象
public OperateResult ReadCustomerFromServer( NetHandle customer, string send )
{
var read = ReadCustomerFromServerBase( HslProtocol.CommandBytes( customer, Token, send ) );
if (!read.IsSuccess) return OperateResult.CreateFailedResult( read );
return OperateResult.CreateSuccessResult( read.Content1, Encoding.Unicode.GetString( read.Content2 ) );
}
///
/// 客户端向服务器进行请求,请求字符串数据,并返回状态信息
///
/// 用户的指令头
/// 发送数据
/// 带返回消息的结果对象
public OperateResult ReadCustomerFromServer( NetHandle customer, string[] send )
{
var read = ReadCustomerFromServerBase( HslProtocol.CommandBytes( customer, Token, send ) );
if (!read.IsSuccess) return OperateResult.CreateFailedResult( read );
return OperateResult.CreateSuccessResult( read.Content1, HslProtocol.UnPackStringArrayFromByte( read.Content2 ) );
}
///
/// 客户端向服务器进行请求,请求字符串数据,并返回状态信息
///
/// 用户的指令头
/// 发送数据
/// 带返回消息的结果对象
public OperateResult ReadCustomerFromServer( NetHandle customer, byte[] send )
{
return ReadCustomerFromServerBase( HslProtocol.CommandBytes( customer, Token, send ) );
}
///
/// 需要发送的底层数据
///
/// 需要发送的底层数据
/// 带返回消息的结果对象
private OperateResult ReadFromServerBase( byte[] send )
{
var read = ReadCustomerFromServerBase( send );
if (!read.IsSuccess) return OperateResult.CreateFailedResult( read );
return OperateResult.CreateSuccessResult( read.Content2 );
}
///
/// 需要发送的底层数据
///
/// 需要发送的底层数据
/// 带返回消息的结果对象
private OperateResult ReadCustomerFromServerBase( byte[] send )
{
// 核心数据交互
var read = ReadFromCoreServer( send );
if (!read.IsSuccess) return OperateResult.CreateFailedResult( read );
// 提炼数据信息
byte[] headBytes = new byte[HslProtocol.HeadByteLength];
byte[] contentBytes = new byte[read.Content.Length - HslProtocol.HeadByteLength];
Array.Copy( read.Content, 0, headBytes, 0, HslProtocol.HeadByteLength );
if (contentBytes.Length > 0) Array.Copy( read.Content, HslProtocol.HeadByteLength, contentBytes, 0, read.Content.Length - HslProtocol.HeadByteLength );
if(BitConverter.ToInt32(headBytes, 0) == HslProtocol.ProtocolErrorMsg)
{
return new OperateResult( Encoding.ASCII.GetString( contentBytes ) );
}
int customer = BitConverter.ToInt32( headBytes, 4 );
contentBytes = HslProtocol.CommandAnalysis( headBytes, contentBytes );
return OperateResult.CreateSuccessResult( (NetHandle)customer, contentBytes );
}
#if !NET35
///
/// 客户端向服务器进行异步请求,请求字符串数据
///
/// 用户的指令头
/// 发送数据
public Task> ReadFromServerAsync( NetHandle customer, string send )
{
return Task.Run( ( ) => ReadFromServer( customer, send ) );
}
///
/// 客户端向服务器进行异步请求,请求字节数据
///
/// 用户的指令头
/// 发送的字节内容
/// 带返回消息的结果对象
public Task> ReadFromServerAsync( NetHandle customer, byte[] send )
{
return Task.Run( ( ) => ReadFromServer( customer, send ) );
}
///
/// 客户端向服务器进行异步请求,请求字符串数据
///
/// 用户的指令头
/// 发送数据
public Task> ReadFromServerAsync( NetHandle customer, string[] sends )
{
return Task.Run( ( ) => ReadFromServer( customer, sends ) );
}
///
/// 客户端向服务器进行请求,请求字符串数据,并返回状态信息
///
/// 用户的指令头
/// 发送数据
/// 带返回消息的结果对象
public Task> ReadCustomerFromServerAsync( NetHandle customer, string send )
{
return Task.Run( ( ) => ReadCustomerFromServer( customer, send ) );
}
///
/// 客户端向服务器进行请求,请求字符串数据,并返回状态信息
///
/// 用户的指令头
/// 发送数据
/// 带返回消息的结果对象
public Task> ReadCustomerFromServerAsync( NetHandle customer, string[] send )
{
return Task.Run( ( ) => ReadCustomerFromServer( customer, send ) );
}
///
/// 客户端向服务器进行请求,请求字符串数据,并返回状态信息
///
/// 用户的指令头
/// 发送数据
/// 带返回消息的结果对象
public Task> ReadCustomerFromServerAsync( NetHandle customer, byte[] send )
{
return Task.Run( ( ) => ReadCustomerFromServer( customer, send ) );
}
#endif
#region Object Override
///
/// 获取本对象的字符串表示形式
///
/// 字符串信息
public override string ToString( )
{
return $"NetSimplifyClient[{IpAddress}:{Port}]";
}
#endregion
}
}