Ejemplo n.º 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);
        }
Ejemplo n.º 2
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.º 3
0
        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);
            }
        }
Ejemplo n.º 4
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);
            }
        }