Exemple #1
0
        static void Init()
        {
            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"App_Data\Cache\"))
            {
                Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"App_Data\Cache\");
            }
            bool bInit = true;

            if (is_client == -1)
            {
                string filePath = AppDomain.CurrentDomain.BaseDirectory + @"App_Data\Cache\MeMShare.txt";
                try
                {
                    objFileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
                }
                catch { }
                if (objFileStream != null) //启动监听
                {
                    try
                    {
                        server.Start();
                        is_client = 0;
                        bInit     = false;
                    }
                    catch
                    {
                        if (HttpContext.Current != null)
                        {
                            HttpContext.Current.Response.Write("<center style='color:red'>共享内存模块启动失败!检查端口号 " + MemSharePort.ToString() + " 是否被占用!<br>可修改web.config 配置文件参数 <add key=\"MemSharePort\" value=\"" + MemSharePort.ToString() + "\"/> </center>");
                            HttpContext.Current.Response.End();
                        }
                        else
                        {
                            throw new Exception("<center style='color:red'>共享内存模块启动失败!检查端口号 " + MemSharePort.ToString() + " 是否被占用!<br>可修改web.config 配置文件参数 <add key=\"MemSharePort\" value=\"" + MemSharePort.ToString() + "\"/> </center>");
                        }
                    }
                }
                else
                {
                    is_client = 1;
                }
            }
            if (!bInit)
            {
                new Task(() =>
                {
                    while (!bClose)
                    {
                        TcpClient ct = server.AcceptTcpClient();
                        ct.GetStream().ReadTimeout = 2000;
                        NewClient _ct = new NewClient(ct);
                        ct.GetStream().BeginRead(_ct.buffer, 0, _ct.buffer.Length, new AsyncCallback(Deal), _ct);
                    }
                }).Start();
            }
        }
Exemple #2
0
        static int GetClient()
        {
            if (intconn > 0)
            {
                for (int i = 0; i < intconn; i++)
                {
                    if (Interlocked.CompareExchange(ref clients[i].iUsering, 1, 0).Equals(0))
                    {
                        return(i);
                    }
                }
            }
            if (intconn >= clients.Length)
            {
                Thread.Sleep(1);
                return(GetClient());
            }
            int iUse = Interlocked.Increment(ref intconn);

            iUse          = iUse - 1;
            clients[iUse] = new NewClient();
            return(iUse);
        }
Exemple #3
0
 public static string Get(string _key, int ifail = 0)
 {
     if (ifail > MAX_FAIL_COUNT)
     {
         return(string.Empty);
     }
     if (!IsClient)
     {
         Data.TryGetValue(_key.Trim(), out string _value);
         return(_value);
     }
     else
     {
         int       iconn = GetClient();
         NewClient ct    = clients[iconn];
         try
         {
             if (ct.tcp == null || !ct.tcp.Connected)
             {
                 ct.tcp = new TcpClient();
                 ct.tcp.Connect(MemShareIP, MemSharePort);
             }
         }
         catch
         {
             is_client = -1;
             ReleaseClient(iconn);
         }
         if (is_client == -1) //连接失败需要重新初始化
         {
             Init();
             return(Get(_key, ++ifail));
         }
         byte[] buffer = new byte[BUFFER_LEN];
         ct.tcp.Client.ReceiveTimeout = 100;
         int icount = 0;
         try
         {
             ct.tcp.Client.Send(Encoding.UTF8.GetBytes(string.Format("{0}&{1}&{2}", MyEncode(_key), "", "G")));
             icount = ct.tcp.Client.Receive(buffer, 0, BUFFER_LEN, SocketFlags.None);
         }
         catch
         {
             closeConn(ref ct.tcp);
             is_client = -1;
         }
         finally
         {
             ReleaseClient(iconn);
         }
         if (is_client == -1) //通讯失败需要重新初始化
         {
             Init();
             return(Get(_key, ++ifail));
         }
         if (icount > 0)
         {
             return(Encoding.UTF8.GetString(buffer, 0, icount));
         }
         return(string.Empty);
     }
 }