Beispiel #1
0
        public void Should_Properly_Segment_Bytes(int numberOfBytes, int bytesPerSegment, int expectedSegments)
        {
            var bytes    = new byte[numberOfBytes];
            var segments = ArraySegmentHelper.Segment(bytes, bytesPerSegment);

            Assert.Equal(expectedSegments, segments.Count);
        }
Beispiel #2
0
        public async Task Handle(QueuedMatchResponseModel response)
        {
            var responseModel = new MatchResponseModel
            {
                MatchId    = response.Match.Id,
                GameFormat = response.Match.Format,
                GameTitle  = response.Match.Title,
                GameMode   = response.Match.Mode,
                Players    = response.Match.Requests.SelectMany(x => x.Players.Select(p => new PlayerModel {
                    Id = p.Id, Name = p.Name
                }))
            };

            var json       = JsonSerializer.Serialize(responseModel);
            var bytes      = Encoding.UTF8.GetBytes(json);
            var bufferSize = 1024 * 4;
            var segments   = ArraySegmentHelper.Segment(bytes, bufferSize);

            var webSocket = response.WebSocket;
            var tcs       = response.WebSocketCompletionSource;

            foreach (var segment in segments)
            {
                var eom = segment == segments.Last();
                await webSocket.SendAsync(segment, WebSocketMessageType.Text, eom, CancellationToken.None);
            }

            tcs.TrySetResult(new object());
        }
Beispiel #3
0
        public void Should_Properly_Index_Single_Segment()
        {
            var bytes    = new byte[568];
            var segments = ArraySegmentHelper.Segment(bytes, 1024);

            Assert.Single(segments);
            var segment = segments.Single();

            Assert.Equal(0, segment.Offset);
            Assert.Equal(568, segment.Count);
        }
        private async Task SendRequest(MatchRequestModel request, ClientWebSocket webSocket, CancellationToken cancellationToken)
        {
            var json     = JsonSerializer.Serialize(request);
            var bytes    = Encoding.UTF8.GetBytes(json);
            var segments = ArraySegmentHelper.Segment(bytes, BUFFER_SIZE);

            await webSocket.ConnectAsync(new Uri(_matchmakingServer, "/ws"), cancellationToken);

            foreach (var segment in segments)
            {
                var eom = segment == segments.Last();
                await webSocket.SendAsync(segment, WebSocketMessageType.Text, eom, CancellationToken.None);
            }
        }
Beispiel #5
0
        public void Should_Properly_Index_Multiple_Segments()
        {
            var bytes    = new byte[202];
            var segments = ArraySegmentHelper.Segment(bytes, 100);

            Assert.Equal(3, segments.Count);

            var segment1 = segments[0];

            Assert.Equal(0, segment1.Offset);
            Assert.Equal(100, segment1.Count);

            var segment2 = segments[1];

            Assert.Equal(100, segment2.Offset);
            Assert.Equal(100, segment2.Count);

            var segment3 = segments[2];

            Assert.Equal(200, segment3.Offset);
            Assert.Equal(2, segment3.Count);
        }
Beispiel #6
0
        public void Should_Handle_No_Segments()
        {
            var segments = ArraySegmentHelper.Segment(new byte[0], 1024);

            Assert.Empty(segments);
        }