Exemplo n.º 1
0
 /// <summary>
 /// 加载配置文件数据
 /// </summary>
 /// <param name="isRefresh">true:重新加载</param>
 /// <returns></returns>
 public static PinganPayConfig LoadPinganConfig(bool isRefresh = false)
 {
     if (PinganPayConfig == null || isRefresh)
     {
         PinganPayConfig = ConfigManage.GetPinganPayConfig();
     }
     return(PinganPayConfig);
 }
Exemplo n.º 2
0
        /// <summary>
        /// 银行和企业的  下行请求 发送请求
        /// </summary>
        /// <param name="message">发送的内容</param>
        /// <param name="setting">发送前的设置</param>
        /// <returns></returns>
        public string SendDownMessage(string message, Action <Socket> setting = null)
        {
            String recvMessage = string.Empty;

            try
            {
                var pinganPayConfig = ConfigManage.GetPinganPayConfig();
                using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    if (setting == null)
                    {
                        //设置
                        socket.SendTimeout       = pinganPayConfig.DownSetting.SendTimeout * 1000;
                        socket.ReceiveTimeout    = pinganPayConfig.DownSetting.ReceiveTimeout * 1000;
                        socket.NoDelay           = pinganPayConfig.DownSetting.NoDelay;
                        socket.SendBufferSize    = pinganPayConfig.DownSetting.SendBufferSize;
                        socket.ReceiveBufferSize = pinganPayConfig.DownSetting.SendBufferSize;
                    }
                    else
                    {
                        setting(socket);
                    }
                    //连接
                    socket.Connect(pinganPayConfig.DownSetting.ListenIP, pinganPayConfig.DownSetting.ListenPort);
                    if (socket.Connected)
                    {
                        //发送数据
                        byte[] sendBytes = Utils.ToByte(message);
                        int    sendLen   = socket.Send(sendBytes);

                        //测试
                        //Thread.CurrentThread.Join();
                        //接收数据
                        recvMessage = RecvMessage(socket);
                    }
                    socket.Close();
                }
            }
            catch (Exception ex)
            {
                LogManager.GetCurrentClassLogger().Error("[请求数据]:" + message, ex);
                //throw ex;
            }
            return(recvMessage);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 上行请求 发送请求
        /// </summary>
        /// <param name="message">发送的内容</param>
        /// <param name="setting">发送前的设置</param>
        /// <returns></returns>
        public string SendUpMessage(string message, Action <TcpClient> setting = null)
        {
            String recvMessage = string.Empty;

            try
            {
                var pinganPayConfig = ConfigManage.GetPinganPayConfig();
                using (TcpClient tcpClient = new TcpClient())
                {
                    if (setting == null)
                    {
                        //设置
                        tcpClient.SendTimeout    = pinganPayConfig.UpSetting.SendTimeout * 1000;
                        tcpClient.ReceiveTimeout = pinganPayConfig.UpSetting.ReceiveTimeout * 1000;
                        tcpClient.NoDelay        = pinganPayConfig.UpSetting.NoDelay;
                        tcpClient.SendBufferSize = pinganPayConfig.UpSetting.SendBufferSize;
                    }
                    else
                    {
                        setting(tcpClient);
                    }
                    //连接
                    tcpClient.Connect(pinganPayConfig.UpSetting.IP, pinganPayConfig.UpSetting.Port);
                    if (tcpClient.Connected)
                    {
                        NetworkStream ns        = tcpClient.GetStream();
                        byte[]        sendBytes = Utils.ToByte(message);
                        ns.Write(sendBytes, 0, sendBytes.Length);
                        ns.Flush();
                        //获取内容
                        recvMessage = RecvMessage(tcpClient.Client);
                        ns.Close();
                        tcpClient.Close();

                        #region 无边界性的接收
                        //MemoryStream ms = new MemoryStream();
                        //byte[] buf = new byte[pinganPayConfig.UpSetting.SendBufferSize];
                        //int len = -1;
                        ////阻塞读取
                        //while ((len = ns.Read(buf, 0, buf.Length)) != -1)
                        //{
                        //    //断开连接或者超时
                        //    if (len == 0) break;
                        //    //写入到内存中
                        //    ms.Write(buf, 0, len);
                        //}
                        //ns.Close();

                        //byte[] resbyte = ms.ToArray();
                        //recvMessage = Utils.ToGBK(resbyte, 0, resbyte.Length);
                        //ms.Close();
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.GetCurrentClassLogger().Error("[请求数据]:" + message + " 返回:" + recvMessage, ex);
                //throw ex;
            }
            return(recvMessage);
        }