|
|
@ -17,11 +17,20 @@ namespace PComm |
|
|
|
{ |
|
|
|
{ |
|
|
|
private readonly int BUFF_SIZE = 1000 * 50; // 50kB |
|
|
|
private readonly int BUFF_SIZE = 1000 * 50; // 50kB |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 delegate void ClientReceivedHandler(PClient sender, PDataType dataType, byte[] data); |
|
|
|
public event ClientReceivedHandler OnReceived; |
|
|
|
public event ClientReceivedHandler OnReceived; |
|
|
|
|
|
|
|
|
|
|
|
public delegate void ClientDisconnectedHandler(PClient sender); |
|
|
|
public delegate void ClientDisconnectedHandler(PClient sender); |
|
|
|
public event ClientDisconnectedHandler Disconnected; |
|
|
|
public event ClientDisconnectedHandler OnDisconnected; |
|
|
|
|
|
|
|
|
|
|
|
public string ID { get; set; } |
|
|
|
public string ID { get; set; } |
|
|
|
|
|
|
|
|
|
|
@ -72,6 +81,8 @@ namespace PComm |
|
|
|
try |
|
|
|
try |
|
|
|
{ |
|
|
|
{ |
|
|
|
socket.Connect(this.EndPoint); |
|
|
|
socket.Connect(this.EndPoint); |
|
|
|
|
|
|
|
if (OnConnected != null) |
|
|
|
|
|
|
|
OnConnected(this); |
|
|
|
|
|
|
|
|
|
|
|
// send ID |
|
|
|
// send ID |
|
|
|
this.Send(PDataType.ClientID, Encoding.UTF8.GetBytes(this.ID)); |
|
|
|
this.Send(PDataType.ClientID, Encoding.UTF8.GetBytes(this.ID)); |
|
|
@ -80,8 +91,11 @@ namespace PComm |
|
|
|
} |
|
|
|
} |
|
|
|
catch(Exception ex) |
|
|
|
catch(Exception ex) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Debug.WriteLine($"[ERROR] Socket connect fail({this.EndPoint.ToString()}): {ex.Message}"); |
|
|
|
string msg = $"Socket connect fail({this.EndPoint.ToString()}, {ex.Message})"; |
|
|
|
PFileManager.Instance.WriteLog($"[ERROR] Socket connect fail({this.EndPoint.ToString()}): {ex.Message}"); |
|
|
|
Debug.WriteLine(msg); |
|
|
|
|
|
|
|
PFileManager.Instance.WriteLog(msg); |
|
|
|
|
|
|
|
if (OnErrorCatched != null) |
|
|
|
|
|
|
|
OnErrorCatched(this, msg); |
|
|
|
|
|
|
|
|
|
|
|
Connect(); |
|
|
|
Connect(); |
|
|
|
} |
|
|
|
} |
|
|
@ -98,8 +112,11 @@ namespace PComm |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Debug.WriteLine($"[ERROR] Socket send: {ex.Message}"); |
|
|
|
string msg = $"Socket send fail ({ex.Message})"; |
|
|
|
PFileManager.Instance.WriteLog($"[ERROR] Socket send: {ex.Message}"); |
|
|
|
Debug.WriteLine(msg); |
|
|
|
|
|
|
|
PFileManager.Instance.WriteLog(msg); |
|
|
|
|
|
|
|
if (OnErrorCatched != null) |
|
|
|
|
|
|
|
OnErrorCatched(this, msg); |
|
|
|
|
|
|
|
|
|
|
|
if (CheckSocketConnection(socket)) |
|
|
|
if (CheckSocketConnection(socket)) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -107,13 +124,16 @@ namespace PComm |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
Debug.WriteLine($"[ERROR] Socket close {this.EndPoint.ToString()}"); |
|
|
|
msg = $"Socket close ({this.EndPoint.ToString()})"; |
|
|
|
PFileManager.Instance.WriteLog($"[ERROR] Socket close {this.EndPoint.ToString()}"); |
|
|
|
Debug.WriteLine(msg); |
|
|
|
|
|
|
|
PFileManager.Instance.WriteLog(msg); |
|
|
|
|
|
|
|
if (OnErrorCatched != null) |
|
|
|
|
|
|
|
OnErrorCatched(this, msg); |
|
|
|
|
|
|
|
|
|
|
|
Close(); |
|
|
|
Close(); |
|
|
|
|
|
|
|
|
|
|
|
if (Disconnected != null) |
|
|
|
if (OnDisconnected != null) |
|
|
|
Disconnected(this); |
|
|
|
OnDisconnected(this); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
return 0; |
|
|
@ -122,7 +142,6 @@ namespace PComm |
|
|
|
|
|
|
|
|
|
|
|
private int SendByProtocol(PDataType dataType, byte[] data) |
|
|
|
private int SendByProtocol(PDataType dataType, byte[] data) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
byte[] type = BitConverter.GetBytes((int)dataType); |
|
|
|
byte[] type = BitConverter.GetBytes((int)dataType); |
|
|
|
SendToSocket(type); |
|
|
|
SendToSocket(type); |
|
|
|
|
|
|
|
|
|
|
@ -145,13 +164,11 @@ namespace PComm |
|
|
|
byte[] length = BitConverter.GetBytes(toSendData.Length); |
|
|
|
byte[] length = BitConverter.GetBytes(toSendData.Length); |
|
|
|
SendToSocket(length); |
|
|
|
SendToSocket(length); |
|
|
|
|
|
|
|
|
|
|
|
return SendToSocket(toSendData); |
|
|
|
int sendByteSize = SendToSocket(toSendData); |
|
|
|
} |
|
|
|
if (OnSend != null) |
|
|
|
|
|
|
|
OnSend(this, dataType, toSendData); |
|
|
|
|
|
|
|
|
|
|
|
public int Send(string msg) |
|
|
|
return sendByteSize; |
|
|
|
{ |
|
|
|
|
|
|
|
byte[] data = Encoding.UTF8.GetBytes(msg); |
|
|
|
|
|
|
|
return Send(PDataType.SimpleString, data); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int Send(PDataType dataType, byte[] data) |
|
|
|
public int Send(PDataType dataType, byte[] data) |
|
|
@ -159,6 +176,12 @@ namespace PComm |
|
|
|
return SendByProtocol(dataType, 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) |
|
|
|
private void AcceptCallback(IAsyncResult ar) |
|
|
|
{ |
|
|
|
{ |
|
|
|
try |
|
|
|
try |
|
|
@ -218,8 +241,11 @@ namespace PComm |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Debug.WriteLine($"[ERROR] Socket receive: {ex.Message}"); |
|
|
|
string msg = $"Socket receive fail ({ex.Message})"; |
|
|
|
PFileManager.Instance.WriteLog($"[ERROR] Socket receive: {ex.Message}"); |
|
|
|
Debug.WriteLine(msg); |
|
|
|
|
|
|
|
PFileManager.Instance.WriteLog(msg); |
|
|
|
|
|
|
|
if (OnErrorCatched != null) |
|
|
|
|
|
|
|
OnErrorCatched(this, msg); |
|
|
|
|
|
|
|
|
|
|
|
if (CheckSocketConnection(socket)) |
|
|
|
if (CheckSocketConnection(socket)) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -227,13 +253,16 @@ namespace PComm |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
Debug.WriteLine($"[ERROR] Socket close {socket.RemoteEndPoint.ToString()}"); |
|
|
|
msg = $"Socket close ({socket.RemoteEndPoint.ToString()})"; |
|
|
|
PFileManager.Instance.WriteLog($"[ERROR] Socket close {socket.RemoteEndPoint.ToString()}"); |
|
|
|
Debug.WriteLine(msg); |
|
|
|
|
|
|
|
PFileManager.Instance.WriteLog(msg); |
|
|
|
|
|
|
|
if (OnErrorCatched != null) |
|
|
|
|
|
|
|
OnErrorCatched(this, msg); |
|
|
|
|
|
|
|
|
|
|
|
Close(); |
|
|
|
Close(); |
|
|
|
|
|
|
|
|
|
|
|
if (Disconnected != null) |
|
|
|
if (OnDisconnected != null) |
|
|
|
Disconnected(this); |
|
|
|
OnDisconnected(this); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|