Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an overload which accepts offset in ZmqSocket.Send(byte[] buffer, int offset, int size, int flags) #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/ZeroMQ/Interop/SocketProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ public byte[] Receive(byte[] buffer, int flags, out int size)
return buffer;
}

public int Send(byte[] buffer, int size, int flags)
public int Send(byte[] buffer, int offset, int size, int flags)
{
// Use zmq_buffer_send method if appropriate -> results in fewer P/Invoke calls
if (buffer.Length <= MaxBufferSize && LibZmq.zmq_buffer_send != null)
{
int sizeToSend = Math.Min(size, MaxBufferSize);
Marshal.Copy(buffer, 0, _buffer, sizeToSend);
Marshal.Copy(buffer, offset, _buffer, sizeToSend);

return Retry.IfInterrupted(LibZmq.zmq_buffer_send.Invoke, SocketHandle, _buffer, sizeToSend, flags);
}
Expand All @@ -174,7 +174,7 @@ public int Send(byte[] buffer, int size, int flags)

if (size > 0)
{
Marshal.Copy(buffer, 0, _msg.Data(), size);
Marshal.Copy(buffer, offset, _msg.Data(), size);
}

int bytesSent = Retry.IfInterrupted(LibZmq.zmq_msg_send.Invoke, _msg.Ptr, SocketHandle, flags);
Expand Down
27 changes: 25 additions & 2 deletions src/ZeroMQ/ZmqSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ public virtual byte[] Receive(byte[] buffer, SocketFlags flags, out int size)
/// P/Invoke calls required to send the message buffer.
/// </remarks>
/// <param name="buffer">A <see cref="byte"/> array that contains the message to be sent.</param>
/// <param name="offset">The offset in buffer</param>
/// <param name="size">The size of the message to send.</param>
/// <param name="flags">A combination of <see cref="SocketFlags"/> values to use when sending.</param>
/// <returns>The number of bytes sent by the socket.</returns>
Expand All @@ -721,7 +722,7 @@ public virtual byte[] Receive(byte[] buffer, SocketFlags flags, out int size)
/// <exception cref="ZmqSocketException">An error occurred sending data to a remote endpoint.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="ZmqSocket"/> has been closed.</exception>
/// <exception cref="NotSupportedException">The current socket type does not support Send operations.</exception>
public virtual int Send(byte[] buffer, int size, SocketFlags flags)
public virtual int Send(byte[] buffer, int offset, int size, SocketFlags flags)
{
EnsureNotDisposed();

Expand All @@ -735,7 +736,7 @@ public virtual int Send(byte[] buffer, int size, SocketFlags flags)
throw new ArgumentOutOfRangeException("size", "Expected a non-negative value less than or equal to the buffer length.");
}

int sentBytes = _socketProxy.Send(buffer, size, (int)flags);
int sentBytes = _socketProxy.Send(buffer, offset, size, (int)flags);

if (sentBytes >= 0)
{
Expand All @@ -758,6 +759,28 @@ public virtual int Send(byte[] buffer, int size, SocketFlags flags)
throw new ZmqSocketException(ErrorProxy.GetLastError());
}

/// <summary>
/// Queue a message buffer to be sent by the socket in blocking mode.
/// </summary>
/// <remarks>
/// Performance tip: To increase send performance, especially on low-powered devices, restrict the
/// size of <paramref name="buffer"/> to <see cref="MaxBufferSize"/>. This will reduce the number of
/// P/Invoke calls required to send the message buffer.
/// </remarks>
/// <param name="buffer">A <see cref="byte"/> array that contains the message to be sent.</param>
/// <param name="size">The size of the message to send.</param>
/// <param name="flags">A combination of <see cref="SocketFlags"/> values to use when sending.</param>
/// <returns>The number of bytes sent by the socket.</returns>
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="size"/> is a negative value or is larger than the length of <paramref name="buffer"/>.</exception>
/// <exception cref="ZmqSocketException">An error occurred sending data to a remote endpoint.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="ZmqSocket"/> has been closed.</exception>
/// <exception cref="NotSupportedException">The current socket type does not support Send operations.</exception>
public virtual int Send(byte[] buffer, int size, SocketFlags flags)
{
return Send(buffer, 0, size, flags);
}

/// <summary>
/// Queue a message buffer to be sent by the socket in non-blocking mode with a specified timeout.
/// </summary>
Expand Down