using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using TechSysWCS.Model;
using System.Threading;
namespace TechSysWCS.Helper
{
class TcpClientHelper
{
public TcpClientHelper(string strIP, int nPort)
{
m_strIP = strIP;
m_nPort = nPort;
m_buffRece = new byte[1024];
m_enumRunFlag = enumRunFlag.Init;
Task task = new Task(runTask);
task.Start();
}
/// <summary>
/// 释放对象
/// </summary>
public void Destroy()
{
if (m_tcpClient != null && m_tcpClient.Connected)
{
m_tcpClient.Close();
}
m_tcpClient = null;
m_enumRunFlag = enumRunFlag.End;
Thread.Sleep(100);
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="strMsg"></param>
public void setSendMsg(byte[] buffSend)
{
this.m_buffSend = buffSend;
if (m_enumRunFlag == enumRunFlag.Sleep)
{
m_enumRunFlag = enumRunFlag.Send;
}
}
/// <summary>
/// 获取消息
/// </summary>
/// <returns></returns>
public byte[] getReceMsg()
{
return m_buffRece;
}
/// <summary>
/// 异步连接请求
/// </summary>
private async void Connect()
{
if (m_tcpClient == null)
{
m_tcpClient = new TcpClient();
try
{
await m_tcpClient.ConnectAsync(m_strIP, m_nPort);
}
catch (Exception ex)
{
Console.WriteLine("Connect Exception: {0}", ex.Message);
Close();
}
}
}
/// <summary>
/// 发送消息
/// </summary>
private async void sendMsg(byte[] byteData)
{
if (m_tcpClient != null && m_tcpClient.Connected)
{
try
{
NetworkStream stream = m_tcpClient.GetStream();
await stream.WriteAsync(byteData, 0, byteData.Length);
}
catch (Exception ex)
{
Console.WriteLine("sendMsg Exception: {0}", ex.Message);
Close();
}
}
else
{
m_enumRunFlag = enumRunFlag.Init;
}
}
/// <summary>
/// 接收消息
/// </summary>
private async void ReceMsg()
{
if (m_tcpClient != null && m_tcpClient.Connected)
{
Array.Clear(m_buffRece, 0, m_buffRece.Length);
m_nReceLen = 0;
try
{
NetworkStream stream = m_tcpClient.GetStream();
m_nReceLen = await stream.ReadAsync(m_buffRece, 0, m_buffRece.Length);
}
catch (Exception ex)
{
Console.WriteLine("ReceMsg Exception: {0}", ex.Message);
Close();
}
}
else
{
m_enumRunFlag = enumRunFlag.Init;
}
}
/// <summary>
/// 异步执行任务
/// </summary>
private void runTask()
{
while (m_enumRunFlag != enumRunFlag.End)
{
switch (m_enumRunFlag)
{
case enumRunFlag.Init:
{
m_enumRunFlag = enumRunFlag.Sleep;
Connect();
break;
}
case enumRunFlag.Send:
{
m_enumRunFlag = enumRunFlag.Rece;
sendMsg(m_buffSend);
break;
}
case enumRunFlag.Rece:
{
m_enumRunFlag = enumRunFlag.ReceResult;
ReceMsg();
break;
}
case enumRunFlag.ReceResult:
{
if (m_nReceLen > 0)
{
m_enumRunFlag = enumRunFlag.Sleep;
Console.Out.WriteLine("ReciveLen:{0}", m_nReceLen);
}
else
{
m_enumRunFlag = enumRunFlag.ReceResult;
}
break;
}
case enumRunFlag.Sleep:
{
Thread.Sleep(1000);
break;
}
default:
{
break;
}
}
Thread.Sleep(100);
}
}
/// <summary>
///关闭连接
/// </summary>
private void Close()
{
if (m_tcpClient != null && m_tcpClient.Connected)
{
m_tcpClient.Close();
}
m_tcpClient = null;
m_enumRunFlag = enumRunFlag.Init;
}
public enum enumRunFlag
{
Init = 0,
Send = 1,
Rece = 2,
ReceResult = 4,
Sleep = 5,
End = -1,
}
public enumRunFlag getRunFlag()
{
return m_enumRunFlag;
}
private TcpClient m_tcpClient;
private string m_strIP;
private int m_nPort;
private byte[] m_buffRece;
private byte[] m_buffSend;
private enumRunFlag m_enumRunFlag;
public int m_nReceLen{get; private set;}
}
}
网友评论