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

C# Fix issues with socket buffer sizes #276

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 12 additions & 5 deletions src/bindings/csharp/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,19 @@ public Int32 Send(Byte[] buffer, int offset, int size, SocketFlags socketFlags)
if (buffer == null) {
throw new ArgumentNullException("buffer");
}
if (size < 0 || size > buffer.Length - offset) {
if (size <= 0 || size > buffer.Length - offset) {
throw new ArgumentOutOfRangeException("size");
}
if (offset < 0 || offset > buffer.Length) {
if (offset < 0 || offset >= buffer.Length) {
throw new ArgumentOutOfRangeException("offset");
}
int flags = 0;

// Must pin memory before using raw pointer
// C# garbage collector can move memory while in use
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
IntPtr bufferPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
return zts_bsd_send(_fd, bufferPtr + offset, (uint)Buffer.ByteLength(buffer), (int)flags);
return zts_bsd_send(_fd, bufferPtr + offset, (uint)size, (int)flags);
}

public int Available
Expand All @@ -344,13 +348,16 @@ public Int32 Receive(byte[] buffer, int offset, int size, SocketFlags socketFlag
if (buffer == null) {
throw new ArgumentNullException("buffer");
}
if (size < 0 || size > buffer.Length - offset) {
if (size <= 0 || size > buffer.Length - offset) {
throw new ArgumentOutOfRangeException("size");
}
if (offset < 0 || offset > buffer.Length) {
if (offset < 0 || offset >= buffer.Length) {
throw new ArgumentOutOfRangeException("offset");
}
int flags = 0;
// Must pin memory before using raw pointer
// C# garbage collector can move memory while in use
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
IntPtr bufferPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
return zts_bsd_recv(_fd, bufferPtr + offset, (uint)Buffer.ByteLength(buffer), (int)flags);
}
Expand Down