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.

296 lines
7.1 KiB

using PUtility;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PComm
{
public class PClient
{
private readonly int BUFF_SIZE = 1000 * 1000; // 1MB
public delegate void ClientConnectedHandler(PClient sender);
public event ClientConnectedHandler OnConnected;
public delegate void ErrorHandler(PClient sender, string msg);
public event ErrorHandler OnErrorCatched;
public delegate void ClientSendHander(PClient sender, PDataType dataType, byte[] data);
public event ClientSendHander OnSend;
public delegate void ClientReceivedHandler(PClient sender, PDataType dataType, byte[] data);
public event ClientReceivedHandler OnReceived;
public delegate void ClientDisconnectedHandler(PClient sender);
public event ClientDisconnectedHandler OnDisconnected;
private int ConnectionRetry = 0;
public string ID { get; set; }
public IPEndPoint EndPoint { get; private set; }
public bool Connected { get { return socket.Connected; } }
public bool DataCompression { get; set; } = false;
private Socket socket;
public PClient(IPAddress ipAddress, int port, string id = null)
{
if (string.IsNullOrEmpty(id))
id = Guid.NewGuid().ToString();
this.ID = id;
this.EndPoint = new IPEndPoint(ipAddress, port);
}
public PClient(Socket accepted)
{
if (string.IsNullOrEmpty(this.ID))
this.ID = Guid.NewGuid().ToString();
socket = accepted;
socket.ReceiveBufferSize = BUFF_SIZE;
socket.SendBufferSize = BUFF_SIZE;
this.EndPoint = (IPEndPoint)socket.RemoteEndPoint;
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null);
}
public void Connect()
{
if (socket == null || !socket.Connected)
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.ReceiveBufferSize = BUFF_SIZE;
socket.SendBufferSize = BUFF_SIZE;
}
try
{
socket.Connect(this.EndPoint);
if (OnConnected != null)
OnConnected(this);
// send ID
this.Send(PDataType.ClientID, Encoding.UTF8.GetBytes(this.ID));
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null);
ConnectionRetry = 0;
}
catch(Exception ex)
{
string msg = $"Socket connect fail({this.EndPoint.ToString()}, {ex.Message})";
Debug.WriteLine(msg);
PFileManager.Instance.WriteLog(msg);
if (OnErrorCatched != null)
OnErrorCatched(this, msg);
if (++ConnectionRetry >= 5)
return;
Connect();
}
}
private int SendToSocket(byte[] buffer)
{
try
{
if (!socket.Connected)
Connect();
return socket.Send(buffer, SocketFlags.None);
}
catch (Exception ex)
{
string msg = $"Socket send fail ({ex.Message})";
Debug.WriteLine(msg);
PFileManager.Instance.WriteLog(msg);
if (OnErrorCatched != null)
OnErrorCatched(this, msg);
if (CheckSocketConnection(socket))
{
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null);
}
else
{
msg = $"Socket close ({this.EndPoint.ToString()})";
Debug.WriteLine(msg);
PFileManager.Instance.WriteLog(msg);
if (OnErrorCatched != null)
OnErrorCatched(this, msg);
Close();
if (OnDisconnected != null)
OnDisconnected(this);
}
return 0;
}
}
private int SendByProtocol(PDataType dataType, byte[] data)
{
byte[] type = BitConverter.GetBytes((int)dataType);
SendToSocket(type);
byte[] isCompressed;
byte[] toSendData;
// 데이터 크기 100B 이하 또는 50MB 이상 압축하지 않음 (CPU 부하시간 더 큼)
// 압축한 경우 1 전송, 아닌경우 0 전송
if (DataCompression == false || data.Length <= 100 || data.Length >= 50000000)
{
isCompressed = BitConverter.GetBytes(false);
toSendData = data;
}
else
{
isCompressed = BitConverter.GetBytes(true);
toSendData = PUtil.CompressBytes(data, CompressionLevel.Optimal);
}
SendToSocket(isCompressed);
byte[] length = BitConverter.GetBytes(toSendData.Length);
SendToSocket(length);
int sendByteSize = SendToSocket(toSendData);
if (OnSend != null)
OnSend(this, dataType, toSendData);
return sendByteSize;
}
public int Send(PDataType dataType, byte[] data)
{
return SendByProtocol(dataType, data);
}
public int Send(string msg)
{
byte[] data = Encoding.UTF8.GetBytes(msg);
return Send(PDataType.SimpleString, data);
}
private void AcceptCallback(IAsyncResult ar)
{
try
{
socket.EndReceive(ar);
// get data type
byte[] dataTypeBuff = new byte[4];
socket.Receive(dataTypeBuff, dataTypeBuff.Length, SocketFlags.None);
PDataType dataType = (PDataType)BitConverter.ToInt32(dataTypeBuff, 0);
// check compressed
byte[] isCompressedBuff = new byte[1];
socket.Receive(isCompressedBuff, isCompressedBuff.Length, SocketFlags.None);
bool isCompressed = BitConverter.ToBoolean(isCompressedBuff, 0);
// get data size
byte[] sizeBuff = new byte[4];
socket.Receive(sizeBuff, sizeBuff.Length, SocketFlags.None);
int dataSize = BitConverter.ToInt32(sizeBuff, 0);
// if data size is larger than buff size, ready to receive data;
//if (dataSize >= BUFF_SIZE)
// Thread.Sleep(100);
byte[] receivedData = new byte[dataSize];
using (MemoryStream ms = new MemoryStream(dataSize))
{
while (dataSize > 0)
{
byte[] buff;
if (dataSize < BUFF_SIZE)
buff = new byte[dataSize];
else
buff = new byte[BUFF_SIZE];
int receiveSize = socket.Receive(buff, buff.Length, SocketFlags.None);
// Thread.Sleep(1);
ms.Write(buff, 0, receiveSize);
dataSize -= receiveSize;
}
receivedData = ms.ToArray();
}
byte[] data;
if (isCompressed)
data = PUtil.DecompressBytes(receivedData);
else
data = receivedData;
if (OnReceived != null)
OnReceived(this, dataType, data);
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null);
}
catch (Exception ex)
{
string msg = $"Socket receive fail ({ex.Message})";
Debug.WriteLine(msg);
PFileManager.Instance.WriteLog(msg);
if (OnErrorCatched != null)
OnErrorCatched(this, msg);
if (CheckSocketConnection(socket))
{
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null);
}
else
{
msg = $"Socket close ({this.EndPoint.ToString()})";
Debug.WriteLine(msg);
PFileManager.Instance.WriteLog(msg);
if (OnErrorCatched != null)
OnErrorCatched(this, msg);
Close();
if (OnDisconnected != null)
OnDisconnected(this);
}
}
}
private bool CheckSocketConnection(Socket socket)
{
if (!socket.Connected)
return false;
bool availability = socket.Available == 0;
bool poll = socket.Poll(1000, SelectMode.SelectRead);
if (availability && poll)
return false;
else
return true;
}
public void Close()
{
socket.Close();
//socket.Dispose();
}
public void Dispose()
{
socket.Dispose();
}
}
}