Ejemplo n.º 1
0
 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");
     }
 }
Ejemplo n.º 2
0
        public CmdRespBase SendCommand(CmdReqBase Request)
        {
            try
            {
                if (!this.IsConnected)
                {
                    if (!Connect())
                    {
                        return(null);
                    }
                }

                #region Send
                try
                {
                    _Log.Write(LogType.Debug, "----------- Send Command Begin...-----------\r\n", true);
                    //send command
                    SendLine("Command=" + Request.Command);
                    //send parameter
                    for (int i = 0; i < Request.GetParamCount(); i++)
                    {
                        SendLine(Request.GetParamName(i) + "=" + Request.GetParamValue(i));
                    }
                    //send DONE
                    SendLine("DONE");

                    // log
                    PackageLog.WritePkgLog(Request, _Log);
                    _Log.Write(LogType.Debug, "------------ Send Command Success!----------\r\n", true);
                }
                catch (Exception ex)
                {
                    PackageLog.WritePkgLog(Request, _Log);
                    _Log.Write(LogType.Error, "---------- Send Command Failure!------------\r\n", true);
                    throw ex;
                }
                #endregion

                #region Receive
                try
                {
                    this.ResetCache();

                    _Log.Write(LogType.Debug, "----------- Receive Response Begin...-------\r\n", true);
                    CmdRespBase response;
                    if (CommandToken.IsNewPatient(Request.Command))
                    {
                        response = new CmdRespNewPatient();
                    }
                    else
                    {
                        response = new CmdRespBase();
                    }

                    string sLine = "";
                    int    r     = ReadLine(ref sLine);
                    while (r > 0)
                    {
                        if (sLine == "DONE")
                        {
                            break;
                        }
                        else
                        {
                            ParseLine(sLine, response);
                        }

                        r = ReadLine(ref sLine);
                    }

                    if (r > 0)
                    {
                        PackageLog.WritePkgLog(response, _Log);
                        _Log.Write(LogType.Debug, "----------- Receive Response Success!-------\r\n", true);
                        return(response);
                    }
                    else
                    {
                        _Log.Write(LogType.Error, "Recive response Failure\r\nReceived byte as follow:\r\n" + this.GetCatchHexString(), true);
                        return(null);
                    }
                }
                catch (Exception ex)
                {
                    _Log.Write(LogType.Error, "Recive response Failure\r\nReceived byte as follow:\r\n" + this.GetCatchHexString(), true);
                    throw ex;
                }
                #endregion
            }
            catch (ObjectDisposedException)
            {
                _Log.Write(LogType.Error, "OnDataReceived: Socket has been closed\r\n");
                return(null);
            }
            catch (SocketException se)
            {
                if (se.ErrorCode == 10054) // Error code for Connection reset by peer
                {
                    string logmsg = "Server " + _ServerIP + ":" + _ServerPort.ToString() + " Disconnected" + "\r\n";
                    _Log.Write(LogType.Error, logmsg, true);
                    return(null);
                }
                else
                {
                    _Log.Write(se);
                    return(null);
                }
            }
            catch (Exception ex)
            {
                _Log.Write(ex);
                return(null);
            }
        }