Example #1
0
        /// <inheritdoc/>
        protected override void BeginSendFile(IWriteRequest request, IFileRegion file)
        {
            SocketAsyncEventArgs saea = _writeBuffer.SocketAsyncEventArgs;

            saea.SendPacketsElements = new SendPacketsElement[] {
                new SendPacketsElement(file.FullName)
            };

            Boolean willRaiseEvent;

            try
            {
                willRaiseEvent = Socket.SendPacketsAsync(saea);
            }
            catch (ObjectDisposedException)
            {
                // do nothing
                return;
            }
            catch (Exception ex)
            {
                EndSend(ex);
                return;
            }
            if (!willRaiseEvent)
            {
                ProcessSend(saea);
            }
        }
Example #2
0
 public int Size(object msg)
 {
     return(msg switch
     {
         IByteBuffer byteBuffer => byteBuffer.ReadableBytes,
         IByteBufferHolder byteBufferHolder => byteBufferHolder.Content.ReadableBytes,
         IFileRegion fileRegion => 0,
         _ => _unknownSize,
     });
 /// <inheritdoc/>
 protected override void BeginSendFile(IWriteRequest request, IFileRegion file)
 {
     try
     {
         Socket.BeginSendFile(file.FullName, SendFileCallback, new SendingContext(Socket, file));
     }
     catch (ObjectDisposedException)
     {
         // ignore
     }
     catch (Exception ex)
     {
         EndSend(ex);
     }
 }
 /// <inheritdoc/>
 protected override void BeginSendFile(IWriteRequest request, IFileRegion file)
 {
     try
     {
         Socket.BeginSendFile(file.FullName, SendFileCallback, new SendingContext(Socket, file));
     }
     catch (ObjectDisposedException)
     {
         // ignore
     }
     catch (Exception ex)
     {
         EndSend(ex);
     }
 }
Example #5
0
        private void BeginSend()
        {
            IWriteRequest req = CurrentWriteRequest;

            if (req == null)
            {
                req = WriteRequestQueue.Poll(this);

                if (req == null)
                {
                    Interlocked.Exchange(ref _writing, 0);
                    return;
                }

                CurrentWriteRequest = req;
            }

            IoBuffer buf = req.Message as IoBuffer;

            if (buf == null)
            {
                IFileRegion file = req.Message as IFileRegion;
                if (file == null)
                {
                    EndSend(new InvalidOperationException("Don't know how to handle message of type '"
                                                          + req.Message.GetType().Name + "'.  Are you missing a protocol encoder?"),
                            true);
                }
                else
                {
                    BeginSendFile(req, file);
                }
            }
            else if (buf.HasRemaining)
            {
                BeginSend(req, buf);
            }
            else
            {
                EndSend(0);
            }
        }
Example #6
0
        /// <summary>
        /// Ends send operation.
        /// </summary>
        /// <param name="bytesTransferred">the bytes transferred in last send operation</param>
        protected void EndSend(Int32 bytesTransferred)
        {
            this.IncreaseWrittenBytes(bytesTransferred, DateTime.Now);

            IWriteRequest req = CurrentWriteRequest;

            if (req != null)
            {
                IoBuffer buf = req.Message as IoBuffer;
                if (buf == null)
                {
                    IFileRegion file = req.Message as IFileRegion;
                    if (file != null)
                    {
                        FireMessageSent(req);
                    }
                    else
                    {
                        // we only send buffers and files so technically it shouldn't happen
                    }
                }
                else if (!buf.HasRemaining)
                {
                    // Buffer has been sent, clear the current request.
                    Int32 pos = buf.Position;
                    buf.Reset();

                    FireMessageSent(req);

                    // And set it back to its position
                    buf.Position = pos;

                    buf.Free();
                }
            }

            if (Socket.Connected)
            {
                BeginSend();
            }
        }
Example #7
0
 /// <summary>
 /// Begins to send a file.
 /// </summary>
 /// <param name="request">the current write request</param>
 /// <param name="file">the file to send</param>
 protected abstract void BeginSendFile(IWriteRequest request, IFileRegion file);
Example #8
0
 protected abstract void BeginSendFile(IWriteRequest request, IFileRegion file);
Example #9
0
        /// <inheritdoc/>
        protected override void BeginSendFile(IWriteRequest request, IFileRegion file)
        {
            SocketAsyncEventArgs saea = _writeBuffer.SocketAsyncEventArgs;
            saea.SendPacketsElements = new SendPacketsElement[] {
                new SendPacketsElement(file.FullName)
            };

            Boolean willRaiseEvent;
            try
            {
                willRaiseEvent = Socket.SendPacketsAsync(saea);
            }
            catch (ObjectDisposedException)
            {
                // do nothing
                return;
            }
            catch (Exception ex)
            {
                EndSend(ex);
                return;
            }
            if (!willRaiseEvent)
            {
                ProcessSend(saea);
            }
        }
 protected override void BeginSendFile(IWriteRequest request, IFileRegion file)
 {
     EndSend(new InvalidOperationException("Cannot send a file via UDP"));
 }
Example #11
0
 /// <inheritdoc/>
 protected override void BeginSendFile(IWriteRequest request, IFileRegion file)
 {
     EndSend(new InvalidOperationException("Cannot send a file via UDP"));
 }