示例#1
0
        /// <summary>
        /// Send syncronization message
        /// </summary>
        /// <param name="evt">event</param>
        /// <param name="cableId">cableId no</param>
        /// <param name="data">data need to send</param>
        /// <param name="timeout">waitting timeout. In millisecond</param>
        /// <returns>data return from client</returns>
        private byte[] InnerSSend(UInt32 evt, byte[] data, int timeout)
        {
            if (Closing)
            {
                throw new NTcpException("Can't operate SingleConnectionCable when it is closing.", ErrorCode.Closing);
            }

            SingleConnection singleConn = _SyncConnection;

            if (!singleConn.Connected)
            {
                throw new NTcpException("Tcp disconnected", ErrorCode.Disconnected);
            }

            int millisecondsRemain = timeout;

            while (CableId == 0)
            {
                System.Threading.Thread.Sleep(10);

                if (!singleConn.Connected)
                {
                    throw new NTcpException("Tcp disconnected", ErrorCode.Disconnected);
                }

                if (timeout != System.Threading.Timeout.Infinite)
                {
                    millisecondsRemain -= 10;

                    if (millisecondsRemain <= 0)
                    {
                        throw new NTcpException("Tcp is establishing.", ErrorCode.Disconnected);
                    }
                }
            }

            return(singleConn.SyncSend(evt, CableId, data, timeout));
        }
示例#2
0
        private void InnerConnect(int millisecondsTimeout)
        {
            if (Closing)
            {
                throw new NTcpException("Can't operate SingleConnectionCable when it is closing.", ErrorCode.Closing);
            }

            if (TryToConnect)
            {
                throw new NTcpException("Try to connect by other tread now.", ErrorCode.TryToConenct);
            }

            try
            {
                try
                {
                    if (!_SyncConnection.Connected)
                    {
                        TryToConnect = true;

                        CableId = 0;
                        _SyncConnection.Connect(millisecondsTimeout, true);

                        ulong processAffinity = (ulong)System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity;

                        byte[] ret = _SyncConnection.SyncSend(MessageFlag.Inner, (uint)InnerEvent.GetProcessorId,
                                                              LittleEndianBitConverter.GetBytes(processAffinity));


                        CableId = LittleEndianBitConverter.ToUInt16(ret, sizeof(int));


                        OnConnectedEvent();
                    }
                }
                catch (Exception e)
                {
                    OnErrorEvent("InnerConnect", e);

                    return;
                }

                if (Capacity == 1)
                {
                    return;
                }

                while (true)
                {
                    SingleConnection pendingConn;

                    lock (_LockObj)
                    {
                        if (_PendingAsyncConnections.Count <= 0)
                        {
                            return;
                        }

                        pendingConn = _PendingAsyncConnections.Dequeue();
                    }

                    try
                    {
                        TryToConnect = true;

                        pendingConn.Connect(millisecondsTimeout);

                        lock (_LockObj)
                        {
                            _WorkingAsyncConnections.AddLast(pendingConn);
                        }
                    }
                    catch (Exception e)
                    {
                        _PendingAsyncConnections.Enqueue(pendingConn);

                        OnErrorEvent("InnerConnect", e);

                        return;
                    }
                }
            }
            finally
            {
                TryToConnect = false;
            }
        }