Beispiel #1
0
        public static string SendRequest(string command, string requestString, string ipAddress, string userAgent, bool isNeedEncrypt)
        {
            try
            {
                Queue <ClientData> queue = new Queue <ClientData>();
                int totalLength          = 0;

                totalLength  = DataManager.PushClientDataToQueueAndFeedbackLength(queue, "6");
                totalLength += DataManager.PushClientDataToQueueAndFeedbackLength(queue, "sendrequest_2");
                totalLength += DataManager.PushClientDataToQueueAndFeedbackLength(queue, command);
                totalLength += DataManager.PushClientDataToQueueAndFeedbackLength(queue, ipAddress);
                totalLength += DataManager.PushClientDataToQueueAndFeedbackLength(queue, userAgent);
                totalLength += DataManager.PushClientDataToQueueAndFeedbackLength(queue, isNeedEncrypt?"true":"false");
                totalLength += DataManager.PushClientDataToQueueAndFeedbackLength(queue, requestString);

                byte[] sendData = DataManager.GetSendData(queue, totalLength);

                ClientSocketManager csmgr = new ClientSocketManager();

                string message = string.Empty;

                byte[] retData = csmgr.SendRequest(sendData, ref message);

                if (message.Equals("socket:ok"))
                {
                    if (retData != null && retData.Length > 8)
                    {
                        return(Encoding.UTF8.GetString(retData, 8, retData.Length - 8));
                    }
                    else
                    {
                        logClient.ErrorFormat("cmd:{0} fail by return null data:{1} \r\nreq:{2}", command, message, requestString);
                        return("socket:failbynull");
                    }
                }
                else
                {
                    logClient.ErrorFormat("cmd:{0} fail in status:{1} request:{2} [from ip:{3},ua:{4},encrypt:{5}]", command, message, requestString, ipAddress, userAgent, isNeedEncrypt);
                    return(message);
                }
            }
            catch (Exception clientErr)
            {
                logClient.Error(string.Format("SendRequest_2异常!\r\ncmd:{0}\r\nrequest:{1}\r\nerr:{2}\r\nstackTrace:{3}\r\n", command, requestString, clientErr.Message, clientErr.StackTrace), clientErr);
            }

            return("fail");
        }
        public Response Send(string host, int port, Request request, int?timeoutMilliseconds)
        {
            var uri = string.Format("tcp://{0}:{1}/{2}?scope={3}", host, port, request.Path, request.ServiceScope);

            byte[] uriByte        = Encoding.UTF8.GetBytes(uri);
            byte[] uriLenInfoByte = BitConverter.GetBytes(uriByte.Length);
            byte[] sendData       = new byte[uriLenInfoByte.Length + uriByte.Length + request.Data.Length];

            Buffer.BlockCopy(uriLenInfoByte, 0, sendData, 0, uriLenInfoByte.Length);
            Buffer.BlockCopy(uriByte, 0, sendData, uriLenInfoByte.Length, uriByte.Length);
            Buffer.BlockCopy(request.Data, 0, sendData, uriLenInfoByte.Length + uriByte.Length, request.Data.Length);

            ClientSocketManager socketManager = new ClientSocketManager();
            string result = string.Empty;

            byte[] returnData = socketManager.SendRequest(sendData, ref result);

            if (result.Equals("ok") && returnData != null)
            {
                int tranId = BitConverter.ToInt32(returnData, 0);
                int sid    = BitConverter.ToInt32(returnData, 4);
                int responseStatusValue = BitConverter.ToInt32(returnData, 8);

                ResponseStatus responseStatus = (ResponseStatus)responseStatusValue;

                if (returnData.Length > 12)
                {
                    byte[] responseData = new byte[returnData.Length - 12];
                    Buffer.BlockCopy(returnData, 12, responseData, 0, returnData.Length - 12);

                    return(new Response(responseStatus, responseData));
                }

                return(new Response(responseStatus, new byte[0]));
            }
            else if (result.Equals("timeout"))
            {
                return(new Response(ResponseStatus.InternalServerError, new byte[0]));
            }
            else
            {
                return(new Response(ResponseStatus.BadRequest, new byte[0]));
            }
        }
Beispiel #3
0
        public static string GetBizJsonString(string bizName, string requestJson)
        {
            try {
                Queue <ClientData> queue = new Queue <ClientData>();
                int totalLength          = 0;

                totalLength  = DataManager.PushClientDataToQueueAndFeedbackLength(queue, "3");
                totalLength += DataManager.PushClientDataToQueueAndFeedbackLength(queue, "getbizjsonstring");
                totalLength += DataManager.PushClientDataToQueueAndFeedbackLength(queue, bizName);
                totalLength += DataManager.PushClientDataToQueueAndFeedbackLength(queue, requestJson);

                byte[] sendData = DataManager.GetSendData(queue, totalLength);

                ClientSocketManager csmgr = new ClientSocketManager();

                string message = string.Empty;

                byte[] retData = csmgr.SendRequest(sendData, ref message);

                if (message.Equals("socket:ok"))
                {
                    if (retData != null && retData.Length > 8)
                    {
                        return(Encoding.UTF8.GetString(retData, 8, retData.Length - 8));
                    }
                    else
                    {
                        logClient.ErrorFormat("cmd:{0} fail by return null data:{1} \r\nreq:{2}", bizName, message, requestJson);
                        return("socket:failbynull");
                    }
                }
                else
                {
                    logClient.ErrorFormat("bizname:{0} fail in status:{1} requestjson:{2}", bizName, message, requestJson);
                    return(message.StartsWith("socket:")?message:string.Format("socket:{0}", message));
                }
            }
            catch (Exception bizJsonErr) {
                logClient.Error(string.Format("GetBizJsonString异常!\r\nbizName:{0}\r\nrequestJson:{1}\r\nerr:{2}\r\nstackTrace:{3}\r\n", bizName, requestJson, bizJsonErr.Message, bizJsonErr.StackTrace), bizJsonErr);
            }

            return("socket:exception-fail");
        }