Exemple #1
0
 /// <summary>
 /// 释放TCP调用客户端
 /// </summary>
 /// <param name="client">TCP调用客户端</param>
 protected void saveClient(fastCSharp.net.tcpClient client)
 {
     if (client != null)
     {
         bool isSave = false;
         Monitor.Enter(clientLock);
         try
         {
             --currentCount;
             if (!isStop && client.IsStart)
             {
                 isSave = true;
                 clients.Add(client);
             }
         }
         finally { Monitor.Exit(clientLock); }
         if (isSave)
         {
             pulseWait();
         }
         else if (client.IsStart)
         {
             client.Dispose();
         }
     }
 }
Exemple #2
0
 /// <summary>
 /// TCP调用
 /// </summary>
 /// <param name="command">TCP调用命令</param>
 /// <returns>TCP调用是否成功</returns>
 public bool Call(byte[] command)
 {
     fastCSharp.net.tcpClient client = getClient();
     try
     {
         return(client.Call(command));
     }
     finally { saveClient(client); }
 }
Exemple #3
0
 /// <summary>
 /// TCP调用
 /// </summary>
 /// <typeparam name="inputParameterType">输入参数类型</typeparam>
 /// <param name="command">TCP调用命令</param>
 /// <param name="inputParameter">输入参数</param>
 /// <returns>TCP调用是否成功</returns>
 public bool Call <inputParameterType>(byte[] command, inputParameterType inputParameter)
     where inputParameterType : fastCSharp.setup.cSharp.serialize.ISerialize
 {
     fastCSharp.net.tcpClient client = getClient();
     try
     {
         return(client.Call(command, inputParameter));
     }
     finally { saveClient(client); }
 }
Exemple #4
0
 /// <summary>
 /// 创建TCP调用客户端
 /// </summary>
 private void createClient()
 {
     fastCSharp.net.tcpClient value = newClient();
     if (value.IsStart)
     {
         Monitor.Enter(clientLock);
         try
         {
             clients.Add(value);
             --createCount;
         }
         finally { Monitor.Exit(clientLock); }
         pulseWait();
     }
     else
     {
         onClientError(null);
     }
 }
Exemple #5
0
 /// <summary>
 /// TCP调用
 /// </summary>
 /// <param name="command">TCP调用命令</param>
 /// <param name="onCall">回调委托,返回false表示失败</param>
 /// <returns>TCP调用客户端</returns>
 public fastCSharp.net.tcpClient Call(action <bool> onCall, byte[] command)
 {
     fastCSharp.net.tcpClient client = null;
     try
     {
         client = getClient();
     }
     finally
     {
         if (client != null)
         {
             client.Call(new async <bool> {
                 ClientPool = this, Client = client, OnReturn = onCall
             }.CallOnReturn, command);
         }
         else
         {
             onCall(false);
         }
     }
     return(client);
 }
Exemple #6
0
 /// <summary>
 /// TCP调用
 /// </summary>
 /// <typeparam name="inputParameterType">输入参数类型</typeparam>
 /// <param name="onCall">回调委托,返回false表示失败</param>
 /// <param name="command">TCP调用命令</param>
 /// <param name="inputParameter">输入参数</param>
 /// <returns>TCP调用客户端</returns>
 public fastCSharp.net.tcpClient Call <inputParameterType>(action <bool> onCall, byte[] command, inputParameterType inputParameter)
     where inputParameterType : fastCSharp.setup.cSharp.serialize.ISerialize
 {
     fastCSharp.net.tcpClient client = null;
     try
     {
         client = getClient();
     }
     finally
     {
         if (client != null)
         {
             client.Call(new async <bool> {
                 ClientPool = this, Client = client, OnReturn = onCall
             }.CallOnReturn, command, inputParameter);
         }
         else
         {
             onCall(false);
         }
     }
     return(client);
 }
Exemple #7
0
 /// <summary>
 /// TCP调用并返回参数值
 /// </summary>
 /// <typeparam name="outputParameterType">输出参数类型</typeparam>
 /// <param name="onGet">回调委托,返回null表示失败</param>
 /// <param name="command">TCP调用命令</param>
 /// <param name="outputParameter">输出参数</param>
 /// <returns>TCP调用客户端</returns>
 public fastCSharp.net.tcpClient Get <outputParameterType>(action <outputParameterType> onGet, byte[] command, outputParameterType outputParameter)
     where outputParameterType : fastCSharp.setup.cSharp.serialize.ISerialize
 {
     fastCSharp.net.tcpClient client = null;
     try
     {
         client = getClient();
     }
     finally
     {
         if (client != null)
         {
             client.Get(new async <outputParameterType> {
                 ClientPool = this, Client = client, OnReturn = onGet
             }.CallOnReturn, command, outputParameter);
         }
         else
         {
             onGet(default(outputParameterType));
         }
     }
     return(client);
 }
Exemple #8
0
            /// <summary>
            /// 获取TCP调用客户端
            /// </summary>
            /// <returns>TCP调用客户端</returns>
            protected fastCSharp.net.tcpClient getClient()
            {
                fastCSharp.net.tcpClient value = null;
                bool isStop = false, isFull = false, isCreate = false, isNew = false;

                Monitor.Enter(clientLock);
                try
                {
                    if (this.isStop)
                    {
                        isStop = true;
                    }
                    else if (clients.Count == 0)
                    {
                        if (currentCount >= maxCount)
                        {
                            isFull = true;
                        }
                        else
                        {
                            ++currentCount;
                            isNew = true;
                        }
                    }
                    else
                    {
                        value = clients.Pop();
                        ++currentCount;
                        if (clients.Count == 0 && currentCount + createCount < maxCount)
                        {
                            ++createCount;
                            isCreate = true;
                        }
                    }
                }
                finally { Monitor.Exit(clientLock); }
                if (value != null)
                {
                    if (isCreate)
                    {
                        fastCSharp.threading.task.Default.Add(createClient, onClientError);
                    }
                    return(value);
                }
                if (isNew)
                {
                    try
                    {
                        value = newClient();
                        isNew = false;
                        return(value);
                    }
                    finally
                    {
                        if (isNew)
                        {
                            Monitor.Enter(clientLock);
                            --currentCount;
                            Monitor.Exit(clientLock);
                        }
                    }
                }
                if (isFull)
                {
                    Interlocked.Increment(ref waitCount);
                    fastCSharp.log.Default.Add(null, (serviceName ?? "未知服务") + " 最大并发 " + maxCount.toString() + ", 等待客户端 " + waitCount.toString(), true);
                    while (!isStop && value == null)
                    {
                        Monitor.Enter(waitLock);
                        try
                        {
                            Monitor.Wait(waitLock);
                        }
                        finally { Monitor.Exit(waitLock); }
                        Monitor.Enter(clientLock);
                        try
                        {
                            if (this.isStop)
                            {
                                isStop = true;
                            }
                            else if (clients.Count != 0)
                            {
                                value = clients.Pop();
                            }
                        }
                        finally { Monitor.Exit(clientLock); }
                    }
                    Interlocked.Decrement(ref waitCount);
                }
                if (!isFull)
                {
                    fastCSharp.log.Default.Throw(null, "已关闭", false);
                }
                return(value);
            }