static public void WritePkgLog(object pkg, Logging _log) { if (pkg.GetType().IsSubclassOf(typeof(CmdReqBase)) || pkg.GetType() == typeof(CmdReqBase)) //or cast to CmdReqBase { CmdReqBase req = (CmdReqBase)pkg; _log.Write(LogType.Debug, "Command=" + req.Command); for (int i = 0; i < req.GetParamCount(); i++) { _log.Write(LogType.Debug, req.GetParamName(i) + "=" + req.GetParamValue(i)); } _log.Write(LogType.Debug, "DONE"); } if (pkg.GetType() == typeof(CmdRespBase)) { CmdRespBase resp = (CmdRespBase)pkg; _log.Write(LogType.Debug, "ErrorCode=" + resp.ErrorCode); for (int i = 0; i < resp.GetParamCount(); i++) { _log.Write(LogType.Debug, resp.GetParamName(i) + "=" + resp.GetParamValue(i)); } _log.Write(LogType.Debug, "DONE"); } }
public void OnDataReceived(IAsyncResult asyn) { PacketSocket psoc = (PacketSocket)asyn.AsyncState; try { if (!psoc.currentSocket.Connected) { return; } IPEndPoint ep = ((IPEndPoint)psoc.currentSocket.RemoteEndPoint); int iRx = psoc.currentSocket.EndReceive(asyn); if (iRx < 1) { return; } CmdReqBase request = new CmdReqBase(); string sLine = ""; while (true) { int r = ReadLine(ref sLine, psoc.currentSocket); if (r == 0) { return; } if (r <= 0) { throw new Exception("Read data error!"); } if (sLine.Trim() == "DONE") { break; } ParseLine(sLine, request); } _Log.Write(LogType.Debug, "Receive Command Success! Command Data as follow:------", true); PackageLog.WritePkgLog((object)request, _Log); _Log.Write(LogType.Debug, "---------------Command data end!-------------", true); // Treate Command CmdRespBase response = new CmdRespBase(); if (OnClientDataReceived != null) { OnClientDataReceived(request, response); } // Response Command sLine = "ErrorCode=" + response.ErrorCode; SendLine(sLine, psoc.currentSocket); for (int i = 0; i < response.GetParamCount(); i++) { sLine = response.GetParamName(i) + "=" + response.GetParamValue(i); SendLine(sLine, psoc.currentSocket); } sLine = "DONE"; SendLine(sLine, psoc.currentSocket); _Log.Write(LogType.Debug, "Send Command Response Success! Command Data as follow:-----", true); PackageLog.WritePkgLog(response, _Log); _Log.Write(LogType.Debug, "---------------Command data end!-------------", true); // Continue the waiting for data on the Socket // WaitForData(psoc, psoc.SocketID); //not need to wait for next request!once connect and disconnecct psoc.currentSocket.Close(); _socketList.Delete(psoc.SocketID); } catch (ObjectDisposedException) { //System.Diagnostics.Debugger.Log(0, "1", "\r\nOnDataReceived: Socket has been closed\r\n"); _Log.Write(LogType.Error, "\r\nOnDataReceived: Socket has been closed\r\n", true); } catch (SocketException se) { if (se.ErrorCode == 10054) // Error code for Connection reset by peer { string logmsg = "\r\nClient " + " Disconnected" + "\r\n"; _Log.Write(LogType.Error, logmsg, true); // Remove the reference to the worker socket of the closed client // so that this object will get garbage collected psoc.currentSocket.Close(); _socketList.Delete(psoc.SocketID); } else { _Log.Write(se); } } catch (Exception exx) { _Log.Write(exx); } }