Exemple #1
0
        private bool ParseLine(string sLine, CmdReqBase req)
        {
            int iPos = sLine.IndexOf("=");

            if (iPos < 0)
            {
                return(false);
            }

            string Name = "", Value = "";

            Name = sLine.Substring(0, iPos);
            if (iPos < sLine.Length - 1)
            {
                Value = sLine.Substring(iPos + 1, sLine.Length - iPos - 1);
            }

            if (CommandToken.IsCommandToken(Name))
            {
                req.Command = Value;
            }
            else
            {
                req.AddParameter(Name, Value);
            }
            return(true);
        }
Exemple #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);
            }
        }