Skip to content

Commit 08f0112

Browse files
committed
CSHARP-2454: KeepAliveValues should use 4 byte unsigned integers instead of 8 byte.
1 parent 6f74861 commit 08f0112

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

src/MongoDB.Driver.Core/Core/Connections/KeepAliveValues.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@
1717

1818
namespace MongoDB.Driver.Core.Connections
1919
{
20+
// see the tcp_keepalive struct at the following page for documentation of the buffer layout
21+
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd877220(v=vs.85).aspx
22+
// note: a u_long in C is 4 bytes so the C# equivalent is uint
23+
2024
internal struct KeepAliveValues
2125
{
22-
public ulong OnOff { get; set; }
23-
public ulong KeepAliveTime { get; set; }
24-
public ulong KeepAliveInterval { get; set; }
26+
public uint OnOff { get; set; }
27+
public uint KeepAliveTime { get; set; }
28+
public uint KeepAliveInterval { get; set; }
2529

2630
public byte[] ToBytes()
2731
{
28-
// set the tcp_keepalive struct at the following page for documentation of the buffer layout
29-
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd877220(v=vs.85).aspx
30-
var bytes = new byte[24];
31-
Array.Copy(BitConverter.GetBytes(OnOff), 0, bytes, 0, 8);
32-
Array.Copy(BitConverter.GetBytes(KeepAliveTime), 0, bytes, 8, 8);
33-
Array.Copy(BitConverter.GetBytes(KeepAliveInterval), 0, bytes, 16, 8);
32+
var bytes = new byte[12];
33+
Array.Copy(BitConverter.GetBytes(OnOff), 0, bytes, 0, 4);
34+
Array.Copy(BitConverter.GetBytes(KeepAliveTime), 0, bytes, 4, 4);
35+
Array.Copy(BitConverter.GetBytes(KeepAliveInterval), 0, bytes, 8, 4);
3436
return bytes;
3537
}
3638
}

tests/MongoDB.Driver.Core.Tests/Core/Connections/KeepAliveValuesTests.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
using System;
17-
using System.Collections.Generic;
18-
using System.Linq;
19-
using System.Text;
20-
using System.Threading.Tasks;
2116
using FluentAssertions;
2217
using Xunit;
2318

@@ -28,8 +23,8 @@ public class KeepAliveValuesTests
2823
[Theory]
2924
[InlineData(0)]
3025
[InlineData(1)]
31-
[InlineData(ulong.MaxValue)]
32-
public void OnOff_get_and_set_work(ulong value)
26+
[InlineData(uint.MaxValue)]
27+
public void OnOff_get_and_set_work(uint value)
3328
{
3429
var subject = new KeepAliveValues();
3530

@@ -42,8 +37,8 @@ public void OnOff_get_and_set_work(ulong value)
4237
[Theory]
4338
[InlineData(0)]
4439
[InlineData(1)]
45-
[InlineData(ulong.MaxValue)]
46-
public void KeepAliveTime_get_and_set_work(ulong value)
40+
[InlineData(uint.MaxValue)]
41+
public void KeepAliveTime_get_and_set_work(uint value)
4742
{
4843
var subject = new KeepAliveValues();
4944

@@ -56,8 +51,8 @@ public void KeepAliveTime_get_and_set_work(ulong value)
5651
[Theory]
5752
[InlineData(0)]
5853
[InlineData(1)]
59-
[InlineData(ulong.MaxValue)]
60-
public void KeepAliveInterval_get_and_set_work(ulong value)
54+
[InlineData(uint.MaxValue)]
55+
public void KeepAliveInterval_get_and_set_work(uint value)
6156
{
6257
var subject = new KeepAliveValues();
6358

@@ -68,9 +63,9 @@ public void KeepAliveInterval_get_and_set_work(ulong value)
6863
}
6964

7065
[Theory]
71-
[InlineData(0x0102030405060708, 0x0203040506070809, 0x030405060708090a, new byte[] { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03 })]
72-
[InlineData(0xf1f2f3f4f5f6f7f8, 0xf2f3f4f5f6f7f8f9, 0xf3f4f5f6f7f8f9fa, new byte[] { 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3 })]
73-
public void ToBytes_should_return_expected_result(ulong onOff, ulong keepAliveTime, ulong keepAliveInterval, byte[] expectedResult)
66+
[InlineData(0x01020304, 0x02030405, 0x03040506, new byte[] { 0x04, 0x03, 0x02, 0x01, 0x05, 0x04, 0x03, 0x02, 0x06, 0x05, 0x04, 0x03 })]
67+
[InlineData(0xf1f2f3f4, 0xf2f3f4f5, 0xf3f4f5f6, new byte[] { 0xf4, 0xf3, 0xf2, 0xf1, 0xf5, 0xf4, 0xf3, 0xf2, 0xf6, 0xf5, 0xf4, 0xf3 })]
68+
public void ToBytes_should_return_expected_result(uint onOff, uint keepAliveTime, uint keepAliveInterval, byte[] expectedResult)
7469
{
7570
var subject = new KeepAliveValues
7671
{

0 commit comments

Comments
 (0)