using HPSocketCS;
|
using iWareCc.Conveyor.Datagram;
|
using iWareCc.Conveyor.Entity;
|
using iWareCc.Conveyor.Service;
|
using iWareCc.Properties;
|
using iWareCommon.Utils;
|
using System;
|
using System.Text;
|
using iWareCc.Util;
|
using System.Runtime.InteropServices;
|
|
namespace iWareCc.Conveyor.Socket
|
{
|
public class GateSocketClient:SocketClient
|
{
|
/// <summary>
|
/// 端口对象
|
/// </summary>
|
private GateEntity GateEntity;
|
|
public GateSocketClient(GateEntity gateEntity, TcpClient client, String ip, String port)
|
: base(client, ip, port)
|
{
|
this.GateEntity = gateEntity;
|
}
|
|
protected override HandleResult OnClose(TcpClient sender, SocketOperation enOperation, int errorCode)
|
{
|
|
lock (GateEntity)
|
{
|
//清空发送和接收的报文缓存
|
GateEntity.DatagramReceivePool = "";
|
GateEntity.DatagramSendPool = "";
|
GateEntity.SendFlag = false;
|
this.IsClientOn = false;
|
|
}
|
|
return HandleResult.Ok;
|
}
|
|
protected override HandleResult OnReceive(TcpClient sender, byte[] bytes)
|
{
|
string msg = UTF8Encoding.UTF8.GetString(bytes);
|
|
//过滤#之后的乱码
|
string endFlag = "#";
|
int index = msg.IndexOf(endFlag);
|
msg = msg.Substring(0,index+endFlag.Length);
|
|
|
GateEntity.SendFlag = true;
|
|
new GateService(GateEntity).AddToDatagramReceivePool(msg);
|
|
LogTextHelper.WriteLine(Resources.LogDir + @"/输送机端口接收报文/" + GateEntity.Place.PlaceTypeName, msg);
|
|
return HandleResult.Ok;
|
}
|
|
protected override HandleResult OnSend(TcpClient sender, byte[] bytes)
|
{
|
GateEntity.SendFlag = false;
|
|
LogTextHelper.WriteLine(Resources.LogDir + @"/输送机端口发送报文/" + GateEntity.Place.PlaceTypeName, UTF8Encoding.UTF8.GetString(bytes));
|
|
return HPSocketCS.HandleResult.Ok;
|
}
|
|
protected override HandleResult OnConnect(TcpClient sender)
|
{
|
LogTextHelper.WriteLine(Resources.LogDir, GateEntity.Place.PlaceTypeName + "socket连上啦");
|
|
lock (GateEntity)
|
{
|
//清空发送和接收的报文缓存
|
GateEntity.DatagramReceivePool = "";
|
GateEntity.DatagramSendPool = "";
|
this.IsClientOn = true;
|
GateEntity.SendFlag = true;
|
|
}
|
|
return HandleResult.Ok;
|
}
|
|
|
public override bool Send(byte[] bytes)
|
{
|
|
return base.Send(bytes);
|
}
|
}
|
}
|