Exemplo n.º 1
0
        /// <summary>
        /// 转化结果
        /// </summary>
        /// <param name="Param"></param>
        /// <returns></returns>
        public SocketMiddleData ExecuteMapper(object Param)
        {
            JObject        Obj      = (Param as string).ToModel <JObject>();
            SendTypeEnum   SendType = Enum.Parse <SendTypeEnum>(Obj["SendType"].ToString());
            int?           SendPort = Convert.ToInt32(Obj["SendPort"].ToString());
            ISocketResult  Result   = Obj["MiddleResult"].ToJson().ToModel <SocketResultDefault>();
            ISocketSession Session  = Obj["Session"].ToJson().ToModel <SocketSessionDefault>();

            return(SocketMiddleData.Middle(SendType, Result, Session, SendPort));
        }
Exemplo n.º 2
0
 /// <summary>
 /// 传输数据
 /// </summary>
 /// <param name="SendType"></param>
 /// <param name="Result"></param>
 /// <param name="Session"></param>
 /// <param name="SendPort"></param>
 /// <returns></returns>
 public static SocketMiddleData Middle(SendTypeEnum SendType, ISocketResult Result, ISocketSession Session = null, int?SendPort = null)
 {
     return(new SocketMiddleData()
     {
         SendType = SendType,
         MiddleResult = Result,
         Session = Session,
         SendPort = SendPort
     });
 }
Exemplo n.º 3
0
        /// <summary>
        /// 发生短信消息验证
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public ActionResult SendCode(string phone = "")
        {
            SendTypeEnum sendType   = SendTypeEnum.提现号码验证;
            Return_Msg   returnData = new Return_Msg();

            try
            {
                if (string.IsNullOrEmpty(phone))
                {
                    returnData.Msg = "手机号不能为空!";
                    return(Json(returnData));
                }
                C_UserInfo userinfo = C_UserInfoBLL.SingleModel.GetModelByTelephone_appid(phone, _pxhAppId);
                if (userinfo == null)
                {
                    returnData.Msg = "没有找到用户信息!";
                    return(Json(returnData));
                }

                string        redisKey      = string.Format(_redis_PhoneKey, phone);
                SendMsgHelper sendMsgHelper = new SendMsgHelper();
                string        authCode      = RedisUtil.Get <string>(redisKey);
                if (string.IsNullOrEmpty(authCode))
                {
                    authCode = EncodeHelper.CreateRandomCode(4);
                }
                bool result = sendMsgHelper.AliSend(phone, "{\"code\":\"" + authCode + "\",\"product\":\" " + Enum.GetName(typeof(SendTypeEnum), sendType) + "\"}", "小未科技", 401);
                if (result)
                {
                    RedisUtil.Set <string>(redisKey, authCode, TimeSpan.FromMinutes(5));
                    returnData.isok = true;
                    returnData.Msg  = "验证码发送成功!";
                }
                else
                {
                    returnData.Msg = "验证码发送失败,请稍后再试!";
                }
                returnData.dataObj = authCode;
                return(Json(returnData));
            }
            catch (Exception ex)
            {
                returnData.Msg = "系统异常!" + ex.Message;
                return(Json(returnData));
            }
        }
Exemplo n.º 4
0
        public static async Task <Result> SendWithTimeoutAsync(
            this Socket socket,
            byte[] buffer,
            int offset,
            int size,
            SocketFlags socketFlags,
            SendTypeEnum sendType = SendTypeEnum.SendTypeDelay,
            int timeoutMs         = -1)
        {
            int  cycles        = 0;
            bool sendException = false;
            bool msgSent       = false;

            do
            {
                ++cycles;
                Console.WriteLine("Trying to send: {0}", cycles);

                if (sendType == SendTypeEnum.SendTypeDelay || (cycles > MaxCycles))
                {
                    sendException = true;
                }

                try
                {
                    var asyncResult = socket.BeginSend(buffer, offset, size, socketFlags, null, null);
                    var sendTask    = Task <int> .Factory.FromAsync(asyncResult, _ => socket.EndSend(asyncResult));

                    if (sendTask != await Task.WhenAny(sendTask, Task.Delay(timeoutMs)).ConfigureAwait(false))
                    {
                        if (sendException)
                        {
                            throw new TimeoutException();
                        }
                    }
                    // we sent so break out of loop and return OK
                    msgSent = true;
                    break;
                }
                catch (SocketException ex)
                {
                    if (sendException)
                    {
                        return(Result.Fail($"{ex.Message} ({ex.GetType()})"));
                    }
                }
                catch (TimeoutException ex)
                {
                    if (sendException)
                    {
                        return(Result.Fail($"{ex.Message} ({ex.GetType()})"));
                    }
                }
            } while (cycles <= MaxCycles);

            if (!msgSent || (cycles > MaxCycles))
            {
                return(Result.Fail("There was a problem sending."));
            }
            return(Result.Ok());
        }