Exemplo n.º 1
0
        public object Process()
        {
            if (!string.IsNullOrEmpty(RequestKey))
            {
                if (GlobalVariables.RequestDictionary.ContainsKey(RequestKey))
                {
                    OnProcessSuccessfully(GlobalVariables.RequestDictionary[RequestKey]);
                    RaiseEvent(GlobalVariables.RequestDictionary[RequestKey]);
                    return(GlobalVariables.RequestDictionary[RequestKey]);
                }
            }
            var             count = 0;
            StoreConnection connection;

            do
            {
                connection = GlobalVariables.ServerConnection;
                if (connection.SocketAlive)
                {
                    if (GlobalVariables.UserOnline && count > 0)
                    {
                        AutoSignin();
                        //GlobalVariables.StoreWorker.ForceAddRequest(this);
                        return(null);
                    }
                    //GlobalVariables.ServerConnection.Connection.Close();
                    break;
                }
                GlobalVariables.ServerConnection.ConfigAndConnectSocket();
                count++;
            } while (count < StoreConnection.RetryTime);
            if (count == StoreConnection.RetryTime)
            {
                //if (GlobalVariables)
                OnProcessError(null, "Lost connection");
                return(null);
            }
            BuildPacket();
            try
            {
                connection.SendPacket(Packet);
            }
            catch (SocketException se)
            {
                //if (se.SocketErrorCode == SocketError.TimedOut)
                //{
                //    return;
                //}
                OnProcessError(null, "Send packet error");
                return(null);
            }

            StoreReply reply = null;

            try
            {
                var packet = connection.ReceiveReply();
                reply = new StoreReply(packet, GlobalVariables.CountRequest);
            }
            catch (SocketException se)
            {
                //if (se.SocketErrorCode == SocketError.TimedOut)
                //{
                //    return;
                //}
                OnProcessError(null, "Receive packet error");
                return(null);
            }
            Debug.Assert(reply != null, "reply packet is null");
            Reply dataReply = reply.ProcessReply();

            if (dataReply == null)
            {
                OnProcessError(null, "Reply is null");
                return(null);
            }
            if (dataReply.type == (decimal)Reply.Type.OK)
            {
                OnProcessSuccessfully(dataReply);
                RaiseEvent(dataReply);
                if (!string.IsNullOrEmpty(RequestKey))
                {
                    GlobalVariables.RequestDictionary.Add(RequestKey, dataReply);
                }
            }
            else
            {
                OnProcessError(dataReply, "Reply is failed");
            }
            return(dataReply);
        }
Exemplo n.º 2
0
        public object Process()
        {
            if (!string.IsNullOrEmpty(RequestKey))
            {
                if (GlobalVariables.RequestDictionary.ContainsKey(RequestKey))
                {
                    OnProcessSuccessfully(GlobalVariables.RequestDictionary[RequestKey]);
                    RaiseEvent(GlobalVariables.RequestDictionary[RequestKey]);
                    return GlobalVariables.RequestDictionary[RequestKey];
                }
            }
            var count = 0;
            StoreConnection connection;
            do
            {
                connection = GlobalVariables.ServerConnection;
                if (connection.SocketAlive)
                {
                    if (GlobalVariables.UserOnline && count > 0)
                    {
                        AutoSignin();
                        //GlobalVariables.StoreWorker.ForceAddRequest(this);
                        return null;
                    }
                    //GlobalVariables.ServerConnection.Connection.Close();
                    break;
                }
                GlobalVariables.ServerConnection.ConfigAndConnectSocket();
                count++;
            } while (count < StoreConnection.RetryTime);
            if (count == StoreConnection.RetryTime)
            {
                //if (GlobalVariables)
                OnProcessError(null, "Lost connection");
                return null;
            }
            BuildPacket();
            try
            {
                connection.SendPacket(Packet);
            }
            catch (SocketException se)
            {
                //if (se.SocketErrorCode == SocketError.TimedOut)
                //{
                //    return;
                //}
                OnProcessError(null, "Send packet error");
                return null;
            }

            StoreReply reply = null;
            try
            {
                var packet = connection.ReceiveReply();
                reply = new StoreReply(packet, GlobalVariables.CountRequest);
            }
            catch (SocketException se)
            {
                //if (se.SocketErrorCode == SocketError.TimedOut)
                //{
                //    return;
                //}
                OnProcessError(null, "Receive packet error");
                return null;
            }
            Debug.Assert(reply != null, "reply packet is null");
            Reply dataReply = reply.ProcessReply();
            if (dataReply == null)
            {
                OnProcessError(null, "Reply is null");
                return null;
            }
            if (dataReply.type == (decimal)Reply.Type.OK)
            {
                OnProcessSuccessfully(dataReply);
                RaiseEvent(dataReply);
                if (!string.IsNullOrEmpty(RequestKey))
                {
                    GlobalVariables.RequestDictionary.Add(RequestKey, dataReply);
                }
            }
            else
            {
                OnProcessError(dataReply, "Reply is failed");
            }
            return dataReply;
        }