Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="flag"></param>
        /// <param name="evt"></param>
        /// <param name="cableId"></param>
        /// <param name="channel"></param>
        /// <param name="data"></param>
        /// <exception cref="TcpException"></exception>
        /// <exception cref="socketException"></exception>
        private void InnerASend(UInt32 evt, byte[] data)
        {
            if (Closing)
            {
                throw new NTcpException("Can't operate SingleConnectionCable when it is closing.", ErrorCode.Closing);
            }

            SingleConnection singleConn = GetAWorkingConnection();

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


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

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

            singleConn.AsyncSend(evt, CableId, data);
        }
Пример #2
0
        /// <summary>
        /// 创建新的连接对象
        /// </summary>
        /// <param name="address"></param>
        /// <param name="port"></param>
        /// <param name="isPolling"></param>
        /// <param name="pollCapacity"></param>
        /// <returns></returns>
        public static INTCPConnection CreatNewConnection(string address, int port, bool isPolling, int pollCapacity)
        {
            INTCPConnection driver         = null;
            var             remoteEndPoint = new IPEndPoint(IPAddress.Parse(address), port);

            if (isPolling == true)
            {
                //基于连接池的多窗口连接
                driver = new SingleConnectionCable(remoteEndPoint, pollCapacity);
            }
            else
            {
                //单连接
                driver = new SingleConnection(remoteEndPoint);
            }

            return(driver);
        }
Пример #3
0
        public SingleConnectionCable(IPEndPoint remoteIPEndPoint, int capacity)
        {
            RemoteIPEndPoint = remoteIPEndPoint;

            if (capacity <= 0)
            {
                throw new ArgumentException("Capacity must be large than 0");
            }

            Capacity = capacity;

            _WorkingAsyncConnections  = new LinkedList <SingleConnection>();
            _PendingAsyncConnections  = new Queue <SingleConnection>();
            _CurrentWorkingConnection = null;

            for (int i = 1; i < capacity; i++)
            {
                SingleConnection conn = new SingleConnection(remoteIPEndPoint);

                conn.ErrorEventHandler += InnerErrorEventHandler;

                conn.ReceiveEventHandler += InnerReceiveEventHandler;

                conn.RemoteDisconnected += InnerRemoteDisconnected;

                _PendingAsyncConnections.Enqueue(conn);
            }

            DefaultDataSerializer   = new Serialize.BinSerializer <object>();
            DefaultReturnSerializer = new Serialize.JsonSerializer <DataContainer>();

            _SyncConnection = new SingleConnection(remoteIPEndPoint);

            _SyncConnection.ErrorEventHandler += InnerErrorEventHandler;

            _SyncConnection.ReceiveEventHandler += InnerReceiveEventHandler;

            _SyncConnection.RemoteDisconnected += InnerRemoteDisconnected;

            _ConnectThread = new System.Threading.Thread(ConnectThreadProc);
            _ConnectThread.IsBackground = true;
            _ConnectThread.Start();
        }
Пример #4
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));
        }