天津康师傅调度系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

450 lines
15 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using CommonLib;
using System.Net.NetworkInformation;
namespace FINSTCPIP
{
public class TcpSocket
{
public static readonly Dictionary<string ,Object> thisLock = new Dictionary<string, object>();
public static event CWriteDarkCasketEventHandler WriteDarkCasket;
public static void OnWriteDarkCasket(CWriteDarkCasketEventArgs e)
{
if (WriteDarkCasket != null)
{
WriteDarkCasket(null, e);
}
}
protected Socket m_Socket;
private int m_Devindex;
private string m_IP;
private int m_Port;
protected bool m_IsServer;
protected int m_BackLog;
protected byte[] m_Buffer = new byte[1024 * 256];
protected byte[] m_Buffer2 = new byte[1024 * 256];
private bool m_Close;
#region 构造函数
public TcpSocket(string ip, int port, int devindex = 0, bool isServer = false, int backlog = 10)
{
this.m_IP = ip;
this.m_Port = port;
this.m_IsServer = isServer;
this.m_BackLog = backlog;
this.m_Devindex = devindex;
}
#endregion
#region 事件
public event SocketOpenSuccessEventHandler OpenSuccess
{
add { onOpenSuccess += value; }
remove { onOpenSuccess -= value; }
}
protected SocketOpenSuccessEventHandler onOpenSuccess;
public event SocketOpenFailEventHandler OpenFail
{
add { onOpenFail += value; }
remove { onOpenFail -= value; }
}
protected SocketOpenFailEventHandler onOpenFail;
public event SocketAcceptSuccessEventHandler AcceptSuccess
{
add { onAcceptSuccess += value; }
remove { onAcceptSuccess -= value; }
}
protected SocketAcceptSuccessEventHandler onAcceptSuccess;
public event SocketAccpetFaileEventHandler AcceptFail
{
add { onAcceptFail += value; }
remove { onAcceptFail -= value; }
}
protected SocketAccpetFaileEventHandler onAcceptFail;
public event SocketCloseSuccessEventHandler CloseSuccess
{
add { onCloseSuccess += value; }
remove { onCloseSuccess -= value; }
}
protected SocketCloseSuccessEventHandler onCloseSuccess;
public event SocketCloseFailEventHandler CloseFail
{
add { onCloseFail += value; }
remove { onCloseFail -= value; }
}
protected SocketCloseFailEventHandler onCloseFail;
public event SocketSendSuccessEventHandler SendSuccess
{
add { onSendSuccess += value; }
remove { onSendSuccess -= value; }
}
protected SocketSendSuccessEventHandler onSendSuccess;
public event SocketSendFailEventHandler SendFail
{
add { onSendFail += value; }
remove { onSendFail -= value; }
}
protected SocketSendFailEventHandler onSendFail;
public event SocketReceiveSuccessEventHandler ReceiveSuccess
{
add { onReceiveSuccess += value; }
remove { onReceiveSuccess -= value; }
}
protected SocketReceiveSuccessEventHandler onReceiveSuccess;
public event SocketReceiveFailEventHandler ReceiveFail
{
add { onReceiveFail += value; }
remove { onReceiveFail -= value; }
}
protected SocketReceiveFailEventHandler onReceiveFail;
public event SocketDisconnectEventHandler Disconnect
{
add { onDisconnect += value; }
remove { onDisconnect -= value; }
}
protected SocketDisconnectEventHandler onDisconnect;
public string IP { get => m_IP; set => m_IP = value; }
public int Port { get => m_Port; set => m_Port = value; }
#endregion
#region 方法
public void Open()
{
if(m_Socket != null)
{
m_Socket.Dispose();
}
m_Close = false;
string localip = "";
int localport = 0;
IPEndPoint endPoint = new IPEndPoint(string.IsNullOrEmpty(m_IP) ? IPAddress.Any : Dns.GetHostAddresses(m_IP)[0], m_Port);
IPEndPoint localPoint = new IPEndPoint(Dns.GetHostAddresses(localip)[0], localport);
m_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
if(m_IsServer)
{
m_Socket.Bind(localPoint);
m_Socket.Listen(m_BackLog);
m_Socket.BeginAccept(new AsyncCallback(AcceptCallBack), null); //建立socket等待客户端链接
}
else
{
//m_Socket.Bind(localPoint);
m_Socket.Connect(endPoint);
}
if(onOpenSuccess != null)
{
foreach(SocketOpenSuccessEventHandler item in onOpenSuccess.GetInvocationList())
{
item.BeginInvoke(null, null);
}
}
}
catch (Exception ex)
{
if(onOpenFail != null)
{
foreach(SocketOpenFailEventHandler item in onOpenFail.GetInvocationList())
{
item.BeginInvoke(ex, null, null);
}
}
}
}
protected virtual void AcceptCallBack(IAsyncResult ar)
{
try
{
Socket socket = this.m_Socket.EndAccept(ar);
if(onAcceptSuccess != null)
{
foreach(SocketAcceptSuccessEventHandler item in onAcceptSuccess.GetInvocationList())
{
item.BeginInvoke(socket, null, null);
}
m_Socket.BeginAccept(new AsyncCallback(AcceptCallBack), null);
}
}
catch (Exception ex)
{
if(onAcceptFail != null)
{
foreach(SocketAccpetFaileEventHandler item in onAcceptFail.GetInvocationList())
{
item.BeginInvoke(ex, null, null);
}
}
}
}
public void Close()
{
if (m_Close == false)
{
m_Close = true;
}
else
{
return;
}
try
{
if (m_Socket.Connected == true)
{
m_Socket.Shutdown(SocketShutdown.Both);
if (m_IsServer)
{
Thread.Sleep(200);
m_Socket.Dispose();
m_Socket = null;
}
else
{
m_Socket.BeginDisconnect(false, CloseCallBack, m_Socket);
}
}
else
{
m_Socket = null;
}
}
catch (Exception ex)
{
if(onCloseFail != null)
{
foreach(SocketCloseFailEventHandler item in onCloseFail.GetInvocationList())
{
item.BeginInvoke(ex, null, null);
}
}
}
}
protected virtual void CloseCallBack(IAsyncResult ar)
{
try
{
Socket socket = ar.AsyncState as Socket;
socket.EndDisconnect(ar);
socket.Dispose();
//socket = null;
if (onCloseSuccess != null)
{
foreach(SocketCloseSuccessEventHandler item in onCloseSuccess.GetInvocationList())
{
item.BeginInvoke(null, null);
}
}
}
catch (Exception ex)
{
if(onCloseFail != null)
{
foreach(SocketCloseFailEventHandler item in onCloseFail.GetInvocationList())
{
item.BeginInvoke(ex, null, null);
}
}
}
}
public void Send(byte[] buffer, Socket socket = null)
{
//lock (thisLock1)
{
if (socket == null)
{
socket = this.m_Socket;
}
try
{
socket.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(SendCallBack), socket);
}
catch (Exception ex)
{
//m_Close = false;
if (onSendFail != null)
{
foreach (SocketSendFailEventHandler item in onSendFail.GetInvocationList())
{
item.BeginInvoke(ex, null, null);
}
}
if (onDisconnect != null && !m_Close)
{
foreach (SocketDisconnectEventHandler item in onDisconnect.GetInvocationList())
{
item.BeginInvoke(ex, null, null);
}
}
}
}
}
protected virtual void SendCallBack(IAsyncResult ar)
{
try
{
Socket socket = ar.AsyncState as Socket;
socket.EndSend(ar);
if(onSendSuccess != null)
{
foreach(SocketSendSuccessEventHandler item in onSendSuccess.GetInvocationList())
{
item.BeginInvoke(null, null);
}
}
}
catch (Exception ex)
{
if(onSendFail != null)
{
foreach(SocketSendFailEventHandler item in onSendFail.GetInvocationList())
{
item.BeginInvoke(ex, null, null);
}
}
if (onDisconnect != null && !m_Close)
{
foreach (SocketDisconnectEventHandler item in onDisconnect.GetInvocationList())
{
item.BeginInvoke(ex, null, null);
}
}
}
}
public void Receive(Socket socket = null)
{
//lock (thisLock1)
{
if (socket == null)
{
socket = this.m_Socket;
}
try
{
socket.BeginReceive(m_Buffer, 0, m_Buffer.Length, 0, new AsyncCallback(ReceiveCallBack), socket);
}
catch (Exception ex)
{
if (onReceiveFail != null)
{
foreach (SocketReceiveFailEventHandler item in onReceiveFail.GetInvocationList())
{
item.BeginInvoke(ex, null, null);
}
}
}
}
}
protected virtual void ReceiveCallBack(IAsyncResult ar)
{
try
{
Socket socket = ar.AsyncState as Socket;
int length = socket.EndReceive(ar);
if (length > 0)
{
byte[] data = new byte[length];
//if (m_IP == "192.168.0.232")
{
lock (TcpSocket.thisLock[m_IP])
{
Array.Copy(m_Buffer, data, length);
}
}
int[] tem = new int[1];
if (onReceiveSuccess != null)
{
foreach (SocketReceiveSuccessEventHandler item in onReceiveSuccess.GetInvocationList())
{
item.BeginInvoke(data, null, null);
}
}
socket.BeginReceive(m_Buffer, 0, m_Buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), socket);
}
else
{
//if (onDisconnect != null && !m_Close)
//{
// foreach (SocketDisconnectEventHandler item in onDisconnect.GetInvocationList())
// {
// item.BeginInvoke(new Exception(), null, null);
// }
//}
}
}
catch (Exception ex)
{
if(onReceiveFail != null)
{
foreach(SocketReceiveFailEventHandler item in onReceiveFail.GetInvocationList())
{
item.BeginInvoke(ex, null, null);
}
}
if(onDisconnect != null && !m_Close)
{
foreach(SocketDisconnectEventHandler item in onDisconnect.GetInvocationList())
{
item.BeginInvoke(ex, null, null);
}
}
}
}
#endregion
public delegate void SocketOpenSuccessEventHandler();
public delegate void SocketOpenFailEventHandler(Exception ex);
public delegate void SocketAcceptSuccessEventHandler(Socket socket);
public delegate void SocketAccpetFaileEventHandler(Exception ex);
public delegate void SocketCloseSuccessEventHandler();
public delegate void SocketCloseFailEventHandler(Exception ex);
public delegate void SocketSendSuccessEventHandler();
public delegate void SocketSendFailEventHandler(Exception ex);
public delegate void SocketReceiveSuccessEventHandler(byte[] receiveData);
public delegate void SocketReceiveFailEventHandler(Exception ex);
public delegate void SocketDisconnectEventHandler(Exception ex);
}
}