|
|
|
@ -1,6 +1,7 @@ |
|
|
|
|
using System; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Diagnostics; |
|
|
|
|
using System.IO; |
|
|
|
|
using System.Linq; |
|
|
|
|
using System.Net; |
|
|
|
|
using System.Net.Sockets; |
|
|
|
@ -13,13 +14,13 @@ namespace PComm |
|
|
|
|
{ |
|
|
|
|
private readonly int BUFF_SIZE = 8192; |
|
|
|
|
|
|
|
|
|
public delegate void ClientReceivedHandler(PClient sender, byte[] data); |
|
|
|
|
public delegate void ClientReceivedHandler(PClient sender, PDataType dataType, byte[] data); |
|
|
|
|
public event ClientReceivedHandler OnReceived; |
|
|
|
|
|
|
|
|
|
public delegate void ClientDisconnectedHandler(PClient sender); |
|
|
|
|
public event ClientDisconnectedHandler Disconnected; |
|
|
|
|
|
|
|
|
|
public string ID { get; private set; } |
|
|
|
|
public string ID { get; set; } |
|
|
|
|
|
|
|
|
|
public IPEndPoint EndPoint { get; private set; } |
|
|
|
|
|
|
|
|
@ -27,33 +28,49 @@ namespace PComm |
|
|
|
|
|
|
|
|
|
private Socket socket; |
|
|
|
|
|
|
|
|
|
public PClient(string ip, int port) |
|
|
|
|
public PClient(string ip, int port, string id = null) |
|
|
|
|
{ |
|
|
|
|
if (string.IsNullOrEmpty(id)) |
|
|
|
|
id = Guid.NewGuid().ToString(); |
|
|
|
|
|
|
|
|
|
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); |
|
|
|
|
this.ID = Guid.NewGuid().ToString(); |
|
|
|
|
this.ID = id; |
|
|
|
|
this.EndPoint = new IPEndPoint(IPAddress.Parse(ip), port); |
|
|
|
|
socket.Connect(EndPoint); |
|
|
|
|
|
|
|
|
|
// send ID |
|
|
|
|
this.Send(PDataType.ClientID, Encoding.UTF8.GetBytes(this.ID)); |
|
|
|
|
|
|
|
|
|
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public PClient(Socket accepted) |
|
|
|
|
{ |
|
|
|
|
if (string.IsNullOrEmpty(this.ID)) |
|
|
|
|
this.ID = Guid.NewGuid().ToString(); |
|
|
|
|
|
|
|
|
|
socket = accepted; |
|
|
|
|
this.ID = Guid.NewGuid().ToString(); |
|
|
|
|
this.EndPoint = (IPEndPoint)socket.RemoteEndPoint; |
|
|
|
|
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public int Send(string msg) |
|
|
|
|
{ |
|
|
|
|
byte[] buff = Encoding.UTF8.GetBytes(msg); |
|
|
|
|
return socket.Send(buff, SocketFlags.None); |
|
|
|
|
socket.Send(BitConverter.GetBytes((int)PDataType.SimpleString), 0, 4, 0); |
|
|
|
|
byte[] data = Encoding.UTF8.GetBytes(msg); |
|
|
|
|
byte[] length = BitConverter.GetBytes(data.Length); |
|
|
|
|
socket.Send(length, 0, 4, 0); |
|
|
|
|
|
|
|
|
|
return socket.Send(data, SocketFlags.None); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public int Send(byte[] msg) |
|
|
|
|
public int Send(PDataType dataType, byte[] data) |
|
|
|
|
{ |
|
|
|
|
return socket.Send(msg, SocketFlags.None); |
|
|
|
|
socket.Send(BitConverter.GetBytes((int)dataType), 0, 4, 0); |
|
|
|
|
byte[] length = BitConverter.GetBytes(data.Length); |
|
|
|
|
socket.Send(length, 0, 4, 0); |
|
|
|
|
|
|
|
|
|
return socket.Send(data, SocketFlags.None); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void AcceptCallback(IAsyncResult ar) |
|
|
|
@ -62,25 +79,58 @@ namespace PComm |
|
|
|
|
{ |
|
|
|
|
socket.EndReceive(ar); |
|
|
|
|
|
|
|
|
|
byte[] buff = new byte[BUFF_SIZE]; |
|
|
|
|
|
|
|
|
|
int receiveSize = socket.Receive(buff, buff.Length, SocketFlags.None); |
|
|
|
|
if (receiveSize <= 0) |
|
|
|
|
{ |
|
|
|
|
Close(); |
|
|
|
|
// get data type |
|
|
|
|
byte[] dataTypeBuff = new byte[4]; |
|
|
|
|
socket.Receive(dataTypeBuff, dataTypeBuff.Length, SocketFlags.None); |
|
|
|
|
PDataType dataType = (PDataType)BitConverter.ToInt32(dataTypeBuff, 0); |
|
|
|
|
|
|
|
|
|
if (Disconnected != null) |
|
|
|
|
Disconnected(this); |
|
|
|
|
} |
|
|
|
|
else if (receiveSize < buff.Length) |
|
|
|
|
// get data size |
|
|
|
|
byte[] sizeBuff = new byte[4]; |
|
|
|
|
socket.Receive(sizeBuff, sizeBuff.Length, SocketFlags.None); |
|
|
|
|
int dataSize = BitConverter.ToInt32(sizeBuff, 0); |
|
|
|
|
|
|
|
|
|
using (MemoryStream ms = new MemoryStream()) |
|
|
|
|
{ |
|
|
|
|
Array.Resize<byte>(ref buff, receiveSize); |
|
|
|
|
} |
|
|
|
|
while (dataSize > 0) |
|
|
|
|
{ |
|
|
|
|
byte[] buff; |
|
|
|
|
if (dataSize < BUFF_SIZE) |
|
|
|
|
buff = new byte[dataSize]; |
|
|
|
|
else |
|
|
|
|
buff = new byte[BUFF_SIZE]; |
|
|
|
|
|
|
|
|
|
if (OnReceived != null) |
|
|
|
|
OnReceived(this, buff); |
|
|
|
|
int receiveSize = socket.Receive(buff, buff.Length, SocketFlags.None); |
|
|
|
|
|
|
|
|
|
ms.Write(buff, 0, buff.Length); |
|
|
|
|
dataSize -= receiveSize; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
byte[] data = ms.ToArray(); |
|
|
|
|
|
|
|
|
|
if (OnReceived != null) |
|
|
|
|
OnReceived(this, dataType, data); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null); |
|
|
|
|
|
|
|
|
|
//byte[] buff = new byte[BUFF_SIZE]; |
|
|
|
|
//int receiveSize = socket.Receive(buff, buff.Length, SocketFlags.None); |
|
|
|
|
//if (receiveSize <= 0) |
|
|
|
|
//{ |
|
|
|
|
// Close(); |
|
|
|
|
|
|
|
|
|
// if (Disconnected != null) |
|
|
|
|
// Disconnected(this); |
|
|
|
|
//} |
|
|
|
|
//else if (receiveSize < buff.Length) |
|
|
|
|
//{ |
|
|
|
|
// Array.Resize<byte>(ref buff, receiveSize); |
|
|
|
|
//} |
|
|
|
|
|
|
|
|
|
//if (OnReceived != null) |
|
|
|
|
// OnReceived(this, buff); |
|
|
|
|
|
|
|
|
|
//socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, AcceptCallback, null); |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|