예제 #1
0
            public virtual bool WaitAll(uint timeOut)
            {
                if (m_ClientSocket == null)
                {
                    return(false);
                }
                IntPtr h = m_ClientSocket.Handle;

                if (ClientCoreLoader.IsBatching(h) != 0)
                {
                    throw new InvalidOperationException("Can't call the method WaitAll during batching requests");
                }
                if (ClientCoreLoader.IsQueueStarted(h) != 0 && ClientCoreLoader.GetJobSize(h) > 0)
                {
                    throw new InvalidOperationException("Can't call the method WaitAll during enqueuing transactional requests");
                }
                return(ClientCoreLoader.WaitAll(h, timeOut) != 0);
            }
예제 #2
0
            /// <summary>
            /// Start a pool of sockets with a given two-dimensional matrix of connection contexts
            /// </summary>
            /// <param name="cc">A given two-dimensional matrix of connection contexts. Its first dimension length represents the number of threads; and the second dimension length is the number of sockets per thread</param>
            /// <param name="avg">A boolean value for building internal socket pool, which defaults to true.</param>
            /// <param name="ta">A value for COM thread apartment if there is COM object involved. It is ignored on non-window platforms, and default to tagThreadApartment.taNone</param>
            /// <returns>False if there is no connection established; and true as long as there is one connection started</returns>
            public virtual bool StartSocketPool(CConnectionContext[,] cc, bool avg, tagThreadApartment ta)
            {
                bool ok;

                char[] empty = { ' ', '\t', '\r', '\n' };
                if (cc == null || cc.Length == 0)
                {
                    throw new ArgumentException("Must set connection context argument properly");
                }
                if (Started)
                {
                    ShutdownPool();
                }
                CopyCC(cc);
                bool first            = true;
                int  threads          = cc.GetLength(0);
                int  socketsPerThread = cc.GetLength(1);

                if (!StartSocketPool((uint)socketsPerThread, (uint)threads, avg, ta))
                {
                    return(false);
                }
                Dictionary <CClientSocket, THandler> temp = new Dictionary <CClientSocket, THandler>();

                lock (m_cs)
                {
                    int index = 0;
                    foreach (CClientSocket cs in m_dicSocketHandler.Keys)
                    {
                        temp[cs] = m_dicSocketHandler[cs];
                        int m = index % threads;
                        int n = index / threads;
                        CConnectionContext c = m_mcc[m, n];
                        if (c.Host == null)
                        {
                            throw new InvalidOperationException("Host string can not be null");
                        }
                        c.Host = c.Host.Trim(empty);
                        if (c.Host.Length == 0)
                        {
                            throw new InvalidOperationException("Host string must be a valid string");
                        }
                        if (c.Port == 0)
                        {
                            throw new InvalidOperationException("Host port can't be zero");
                        }
                        cs.ConnectionContext = c;
                        ++index;
                    }
                }
                foreach (CClientSocket cs in temp.Keys)
                {
                    if (cs.Connected)
                    {
                        first = false;
                        continue;
                    }
                    CConnectionContext c = cs.ConnectionContext;
                    cs.UID = c.UserId;
                    cs.EncryptionMethod = c.EncrytionMethod;
                    cs.Zip = c.Zip;
                    IntPtr p = cs.Handle;
                    unsafe
                    {
                        fixed(byte *data = System.Text.Encoding.ASCII.GetBytes(c.Host))
                        {
                            IntPtr host = new IntPtr(data);

                            if (first)
                            {
                                //we use sync connecting for the first socket connection
                                ok = ClientCoreLoader.Connect(p, host, c.Port, 1, (byte)(c.V6 ? 1 : 0)) != 0;
                                if (ok && ClientCoreLoader.WaitAll(cs.Handle, uint.MaxValue) != 0)
                                {
                                    first = false;
                                }
                            }
                            else
                            {
                                ok = ClientCoreLoader.Connect(p, host, c.Port, 0, (byte)(c.V6 ? 1 : 0)) != 0;
                            }
                        }
                    }
                }
                return(ConnectedSockets > 0);
            }