예제 #1
0
        /// <summary>
        ///	<para>Sends a <see cref="Stream"/> of containing data serialized from
        ///	<paramref name="dataSerializer"/> and <paramref name="data"/> to
        ///	<see cref="AsyncSocketClient.EndPoint"/> with <paramref name="commandId"/>.
        ///	Calls <paramref name="resultAction"/> when all data has been sent to
        ///	the destination. Please note that this is just a tcp acknowledgement that the data
        ///	was received and not an acknowlegements that the data was processed.</para>
        /// </summary>
        /// <typeparam name="T">The type of data to be sent.</typeparam>
        /// <param name="commandId">The command id.</param>
        /// <param name="data">The data to send.</param>
        /// <param name="dataSerializer">
        ///	<para>A method to serialize <paramref name="data"/> into a <see cref="Stream"/>.</para>
        /// </param>
        /// <param name="resultAction">
        ///	<para>The method that will be called when the remote end point acknowledges that the data was
        ///	recieved, the operation fails, or the operation times out.</para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///	<para><paramref name="dataSerializer"/> is <see langword="null"/>.</para>
        ///	<para>- or -</para>
        ///	<para><paramref name="resultAction"/> is <see langword="null"/>.</para>
        /// </exception>
        public void SendOneWayAsync <T>(
            short commandId,
            T data,
            Procedure <T, Stream> dataSerializer,
            Action <OneWayAsyncEventArgs> resultAction)
        {
            if (dataSerializer == null)
            {
                throw new ArgumentNullException("dataSerializer");
            }
            if (resultAction == null)
            {
                throw new ArgumentNullException("resultAction");
            }

            var sendData = _memoryPool.Borrow();
            IPoolItem <SocketChannel> socket = null;

            try
            {
                ClientMessage.WriteMessage <T>(
                    sendData.Item,
                    _socketPool.Config.NetworkOrdered,
                    commandId,
                    _messageId,                     // must never send 0 otherwise the server header coming back will be the wrong size
                    false,
                    data,
                    dataSerializer);
                sendData.Item.Seek(0, SeekOrigin.Begin);

                socket = _socketPool.Pool.Borrow();

                socket.Item.SendOneWayAsync(
                    _socketPool.Config.ReceiveTimeout,
                    sendData,
                    e =>
                {
                    socket.Dispose();
                    resultAction(e);
                });
            }
            catch
            {
                if (socket != null)
                {
                    socket.IsCorrupted = true;
                    socket.Dispose();
                }
                sendData.Dispose();
                throw;
            }
        }
예제 #2
0
        internal void FireEvent(T value)
        {
            var key = (int)value.Type;

            if (events.ContainsKey(key))
            {
                List <Action <T> > list = events[key];
                if (list != null)
                {
                    for (int i = 0; i < list.Count; ++i)
                    {
                        Action <T> action = list[i];
                        if (action != null)
                        {
                            action.Invoke(value);
                        }
                    }
                }
            }

            IPoolItem poolItem = value as IPoolItem;

            if (poolItem != null)
            {
                poolItem.Dispose();
            }
        }
 /// <summary>
 /// Completes the operation.
 /// </summary>
 protected internal override void  PerformCompletion()
 {
     try
     {
         if (_completionAction != null)
         {
             _completionAction(this);
         }
     }
     finally
     {
         _responseDisposed = true;
         if (_response != null)
         {
             _response.Dispose();
             _response = null;
         }
     }
 }
예제 #4
0
 public void Dispose()
 {
     _busItem.Dispose();
     _signal.Dispose();
 }
예제 #5
0
 /// <summary>
 /// 归还项
 /// </summary>
 /// <param name="item"></param>
 public void Return(IPoolItem <T> item)
 {
     item.Dispose();
 }
예제 #6
0
 public void Dispose()
 {
     _busItem.Dispose();
 }