/// <summary> /// expect transport event from transport.<para/> /// if event arrived and packet data buffer is empty, return event directly.<para/> /// decode packet from packet data buffer, return packet if arrived, otherwise, return event. /// </summary> /// <param name="timeout"> /// a TimeSpan struct that specifies the timeout of waiting for a packet or event from the transport. /// </param> /// <returns> /// a TransportEvent object that contains the expected event from transport. /// </returns> /// <exception cref="TimeoutException"> /// thrown when timeout to wait for packet coming. /// </exception> /// <exception cref="TimeoutException"> /// thrown when timeout to wait for event coming. /// </exception> /// <exception cref="ObjectDisposedException"> /// thrown when this object is disposed. /// </exception> /// <exception cref="InvalidOperationException"> /// thrown when stream client is not connected, must invoke Connect() first. /// </exception> public TransportEvent ExpectTransportEvent(TimeSpan timeout) { if (disposed) { throw new ObjectDisposedException("StreamTransport"); } if (this.thread == null) { throw new InvalidOperationException( "stream client is not connected, must invoke Connect() first."); } // decode packet StackPacket packet = this.ExpectPacket(timeout); // if packet is decoded, return it. if (packet != null) { return(new TransportEvent( EventType.ReceivedPacket, this.localEndPoint, null, this.localEndPoint, packet)); } // if no packet, and there is event coming, return event. // set timeout to zero, must not wait for event coming. return(eventQueue.Dequeue(new TimeSpan())); }
/// <summary> /// expect transport event from transport.<para/> /// if event arrived and packet data buffer is empty, return event directly.<para/> /// decode packet from packet data buffer, return packet if arrived, otherwise, return event. /// </summary> /// <param name="timeout"> /// a TimeSpan struct that specifies the timeout of waiting for a packet or event from the transport. /// </param> /// <returns> /// a TransportEvent object that contains the expected event from transport. /// </returns> /// <exception cref="TimeoutException"> /// thrown when timeout to wait for packet coming. /// </exception> /// <exception cref="TimeoutException"> /// thrown when timeout to wait for event coming. /// </exception> /// <exception cref="ObjectDisposedException"> /// thrown when this object is disposed. /// </exception> /// <exception cref="InvalidOperationException"> /// thrown when the udp client is not connected to server, must invoke Start() first. /// </exception> public TransportEvent ExpectTransportEvent(TimeSpan timeout) { if (disposed) { throw new ObjectDisposedException("UdpClientTransport"); } if (this.udpClient == null) { throw new InvalidOperationException("udp client is not start, must invoke Start() first."); } try { object localEP = this.LspHookedLocalEP; object remoteEP = null; // decode packet StackPacket packet = this.GetPacket(timeout, true, out remoteEP); // if packet is decoded, return it. return(new TransportEvent(EventType.ReceivedPacket, remoteEP, localEP, packet)); } catch (InvalidOperationException exc) { if (exc.Data.Count == 0) { throw; } } // if no packet, and there is event coming, return event. // set timeout to zero, must not wait for event coming. return(eventQueue.Dequeue(new TimeSpan())); }
/// <summary> /// expect transport event from transport.<para/> /// if event arrived and packet data buffer is empty, return event directly.<para/> /// decode packet from packet data buffer, return packet if arrived, otherwise, return event. /// </summary> /// <param name="timeout"> /// a TimeSpan struct that specifies the timeout of waiting for a packet or event from the transport. /// </param> /// <returns> /// a TransportEvent object that contains the expected event from transport. /// </returns> /// <exception cref="TimeoutException"> /// thrown when timeout to wait for packet coming. /// </exception> /// <exception cref="TimeoutException"> /// thrown when timeout to wait for event coming. /// </exception> /// <exception cref="ObjectDisposedException"> /// thrown when this object is disposed. /// </exception> /// <exception cref="InvalidOperationException"> /// thrown when tcp client is not connected to server, must invoke Connect() first. /// </exception> public TransportEvent ExpectTransportEvent(TimeSpan timeout) { if (disposed) { throw new ObjectDisposedException("TcpClientTransport"); } if (this.stream == null) { throw new InvalidOperationException( "tcp client is not connected to server, must invoke Connect() first."); } // decode packet StackPacket packet = this.ExpectPacket(timeout); // if packet is decoded, return it. if (packet != null) { return(new TransportEvent( EventType.ReceivedPacket, this.localEndPoint, this.remoteEndPoint, this.localEndPoint, packet)); } // eventQueue == null which should only happens in dispose during disconnect // Temporally adding wait loop would avoid calling on the null object while (eventQueue == null) { System.Threading.Thread.Sleep(100); } // if no packet, and there is event coming, return event. // set timeout to zero, must not wait for event coming. return(eventQueue.Dequeue(new TimeSpan())); }
/// <summary> /// Get result of request /// </summary> public SmbdRequestResult GetRequestResult( TimeSpan timeout, RequestType type) { if (type == RequestType.Receive) { return(receiveRequestResult.Dequeue(timeout)); } return(otherRequestResult.Dequeue(timeout)); }
/// <summary> /// dequeue the received event from queue. /// </summary> /// <param name="eventQueue"> /// a SyncFilterQueue<TransportEvent> that specifies the queue to store event. /// </param> /// <param name="sequence"> /// a DataSequence object that stores the received data or event sequence. /// </param> /// <param name="timeout"> /// a TimeSpan object that specifies the timeout to wait for event. /// </param> /// <param name="filter"> /// a Filter<TransportEvent> that specifies the filter to get event. /// </param> /// <returns> /// return a TransportEvent object that stores the event. /// </returns> public static TransportEvent Dequeue( SyncFilterQueue <TransportEvent> eventQueue, DataSequence sequence, TimeSpan timeout, Filter <TransportEvent> filter) { TransportEvent transportEvent = eventQueue.Dequeue(timeout, filter); // remove the event from sequence. sequence.Remove(transportEvent); return(transportEvent); }
/// <summary> /// remove the specifies transport event from queue and data sequence.<para/> /// invoke this method when get event from the data sequence. /// </summary> /// <param name="eventQueue"> /// a SyncFilterQueue<TransportEvent> that specifies the queue to store event. /// </param> /// <param name="transportEvent"> /// a TransportEvent object that specifies the event to remove. /// </param> public static void Remove( SyncFilterQueue <TransportEvent> eventQueue, TransportEvent transportEvent) { lock (eventQueue) { TransportEvent removedTransportEvent = eventQueue.Dequeue(new TimeSpan()); if (removedTransportEvent != transportEvent) { throw new InvalidOperationException( "the transport event stores in the event queue and data sequence are not consistence."); } } }
/// <summary> /// expect packet from transport.<para/> /// the underlayer transport must be TcpClient, Stream or NetbiosClient. /// </summary> /// <param name="timeout"> /// a TimeSpan object that indicates the timeout to expect event. /// </param> /// <returns> /// a StackPacket object that specifies the received packet. /// </returns> /// <exception cref="ObjectDisposedException"> /// thrown when this object is disposed. /// </exception> /// <exception cref="InvalidOperationException"> /// thrown when stream client is not connected, must invoke Connect() first. /// </exception> public StackPacket ExpectPacket(TimeSpan timeout) { if (disposed) { throw new ObjectDisposedException("StreamTransport"); } if (this.stream == null) { throw new InvalidOperationException( "stream client is not connected, must invoke Connect() first."); } StackPacket packet = packetCache.Dequeue(timeout); return(packet); }
/// <summary> /// get one packet from packet cache.<para/> /// if no packet, return null. /// </summary> /// <param name="packetCache"> /// a SyncFilterQueue queue that stores the packet. /// </param> /// <param name="filter"> /// the filter to get packet. if null, do not filter packet. /// </param> /// <returns> /// a packet specified by filter. /// </returns> public static PacketType GetOne <PacketType>(SyncFilterQueue <PacketType> packetCache, Filter <PacketType> filter) { PacketType packet = default(PacketType); if (packetCache.Count == 0) { return(packet); } try { packet = packetCache.Dequeue(new TimeSpan(), filter); } catch (TimeoutException) { } return(packet); }