/// <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); } }
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); } }
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); } }
/// <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(); } }
/// <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);
protected abstract void BeginSendFile(IWriteRequest request, IFileRegion file);
protected override void BeginSendFile(IWriteRequest request, IFileRegion file) { EndSend(new InvalidOperationException("Cannot send a file via UDP")); }
/// <inheritdoc/> protected override void BeginSendFile(IWriteRequest request, IFileRegion file) { EndSend(new InvalidOperationException("Cannot send a file via UDP")); }