Esempio n. 1
0
        public static FilePiece parse_packet(byte[] packet)
        {
            if (packet.Length < 28){ // header isn't long enough
                return null;
            }
            Int32 data_length = System.BitConverter.ToInt32(packet, 0);
            if (data_length < 0){ // no data
                return null;
            }
            Int64 piece_number = System.BitConverter.ToInt64(packet, 4);
            if (piece_number < 0){ // can't have less than 0 piece number
                return null;
            }
            byte[] checksum = new byte[16];
            for(int i = 12; i < 28; i++){
                checksum[i] = packet[i];
            }
            System.ArraySegment<byte> data_segment = new System.ArraySegment<byte>(packet, 28, packet.Length-28);

            byte[] data = data_segment.Array;

            // do a checksum
            MD5 check = new MD5CryptoServiceProvider();
            byte[] sum = check.ComputeHash(data);
            if (sum.Equals(checksum)){
                return null; // data checksum doesn't match
            }

            FilePiece piece = new FilePiece(piece_number, data);

            return piece;
        }
Esempio n. 2
0
        public async Task WebSocketTinyReceiveGeneratesEndOfMessage()
        {
            // Arrange
            RequestDelegate appDelegate = async ctx =>
            {
                if (ctx.WebSockets.IsWebSocketRequest)
                {
                    var websocket = await ctx.WebSockets.AcceptWebSocketAsync();
                    var receiveArray = new byte[1024];
                    while (true)
                    {
                        var receiveResult = await websocket.ReceiveAsync(new System.ArraySegment<byte>(receiveArray), CancellationToken.None);
                        var sendBuffer = new System.ArraySegment<byte>(receiveArray, 0, receiveResult.Count);
                        await websocket.SendAsync(sendBuffer, receiveResult.MessageType, receiveResult.EndOfMessage, CancellationToken.None);
                    }
                }
            };
            var server = TestServer.Create(app =>
            {
                app.Run(appDelegate);
            });

            // Act
            var client = server.CreateWebSocketClient();
            var clientSocket = await client.ConnectAsync(new System.Uri("http://localhost"), CancellationToken.None);
            var hello = Encoding.UTF8.GetBytes("hello");
            await clientSocket.SendAsync(new System.ArraySegment<byte>(hello), WebSocketMessageType.Text, true, CancellationToken.None);

            // Assert
            var buffer = new byte[1];
            for (var i = 0; i < hello.Length; i++)
            {
                bool last = i == (hello.Length - 1);
                var result = await clientSocket.ReceiveAsync(new System.ArraySegment<byte>(buffer), CancellationToken.None);
                Assert.Equal(buffer.Length, result.Count);
                Assert.Equal(buffer[0], hello[i]);
                Assert.Equal(last, result.EndOfMessage);
            }

            clientSocket.Dispose();
        }
Esempio n. 3
0
 static public int Size <T>(System.ArraySegment <T> s)
 {
     return(s.Count);
 }
Esempio n. 4
0
        public async Task WebSocketWorks()
        {
            // Arrange
            RequestDelegate appDelegate = async ctx =>
            {
                if (ctx.WebSockets.IsWebSocketRequest)
                {
                    var websocket = await ctx.WebSockets.AcceptWebSocketAsync();
                    var receiveArray = new byte[1024];
                    while (true)
                    {
                        var receiveResult = await websocket.ReceiveAsync(new System.ArraySegment<byte>(receiveArray), CancellationToken.None);
                        if (receiveResult.MessageType == WebSocketMessageType.Close)
                        {
                            await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal Closure", CancellationToken.None);
                            break;
                        }
                        else
                        {
                            var sendBuffer = new System.ArraySegment<byte>(receiveArray, 0, receiveResult.Count);
                            await websocket.SendAsync(sendBuffer, receiveResult.MessageType, receiveResult.EndOfMessage, CancellationToken.None);
                        }
                    }
                }
            };
            var server = TestServer.Create(app =>
            {
                app.Run(appDelegate);
            });

            // Act
            var client = server.CreateWebSocketClient();
            var clientSocket = await client.ConnectAsync(new System.Uri("http://localhost"), CancellationToken.None);
            var hello = Encoding.UTF8.GetBytes("hello");
            await clientSocket.SendAsync(new System.ArraySegment<byte>(hello), WebSocketMessageType.Text, true, CancellationToken.None);
            var world = Encoding.UTF8.GetBytes("world!");
            await clientSocket.SendAsync(new System.ArraySegment<byte>(world), WebSocketMessageType.Binary, true, CancellationToken.None);
            await clientSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Normal Closure", CancellationToken.None);

            // Assert
            Assert.Equal(WebSocketState.CloseSent, clientSocket.State);

            var buffer = new byte[1024];
            var result = await clientSocket.ReceiveAsync(new System.ArraySegment<byte>(buffer), CancellationToken.None);
            Assert.Equal(hello.Length, result.Count);
            Assert.True(hello.SequenceEqual(buffer.Take(hello.Length)));
            Assert.Equal(WebSocketMessageType.Text, result.MessageType);

            result = await clientSocket.ReceiveAsync(new System.ArraySegment<byte>(buffer), CancellationToken.None);
            Assert.Equal(world.Length, result.Count);
            Assert.True(world.SequenceEqual(buffer.Take(world.Length)));
            Assert.Equal(WebSocketMessageType.Binary, result.MessageType);

            result = await clientSocket.ReceiveAsync(new System.ArraySegment<byte>(buffer), CancellationToken.None);
            Assert.Equal(WebSocketMessageType.Close, result.MessageType);
            Assert.Equal(WebSocketState.Closed, clientSocket.State);

            clientSocket.Dispose();
        }
 public static System.Threading.Tasks.Task <System.Net.Sockets.SocketReceiveMessageFromResult> ReceiveMessageFromAsync(this System.Net.Sockets.Socket socket, System.ArraySegment <byte> buffer, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEndPoint)
 {
     throw null;
 }
 public static System.Threading.Tasks.Task <int> SendToAsync(this System.Net.Sockets.Socket socket, System.ArraySegment <byte> buffer, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEndPoint)
 {
     throw null;
 }
Esempio n. 7
0
        public async Task WebSocketWorks()
        {
            // Arrange
            // This logger will attempt to access information from HttpRequest once the HttpContext is created
            var             logger      = new VerifierLogger();
            RequestDelegate appDelegate = async ctx =>
            {
                if (ctx.WebSockets.IsWebSocketRequest)
                {
                    var websocket = await ctx.WebSockets.AcceptWebSocketAsync();

                    var receiveArray = new byte[1024];
                    while (true)
                    {
                        var receiveResult = await websocket.ReceiveAsync(new System.ArraySegment <byte>(receiveArray), CancellationToken.None);

                        if (receiveResult.MessageType == WebSocketMessageType.Close)
                        {
                            await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal Closure", CancellationToken.None);

                            break;
                        }
                        else
                        {
                            var sendBuffer = new System.ArraySegment <byte>(receiveArray, 0, receiveResult.Count);
                            await websocket.SendAsync(sendBuffer, receiveResult.MessageType, receiveResult.EndOfMessage, CancellationToken.None);
                        }
                    }
                }
            };
            var builder = new WebHostBuilder()
                          .ConfigureServices(services =>
            {
                services.AddSingleton <ILogger <IWebHost> >(logger);
            })
                          .Configure(app =>
            {
                app.Run(appDelegate);
            });
            var server = new TestServer(builder);

            // Act
            var client = server.CreateWebSocketClient();
            // The HttpContext will be created and the logger will make sure that the HttpRequest exists and contains reasonable values
            var clientSocket = await client.ConnectAsync(new System.Uri("http://localhost"), CancellationToken.None);

            var hello = Encoding.UTF8.GetBytes("hello");
            await clientSocket.SendAsync(new System.ArraySegment <byte>(hello), WebSocketMessageType.Text, true, CancellationToken.None);

            var world = Encoding.UTF8.GetBytes("world!");
            await clientSocket.SendAsync(new System.ArraySegment <byte>(world), WebSocketMessageType.Binary, true, CancellationToken.None);

            await clientSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Normal Closure", CancellationToken.None);

            // Assert
            Assert.Equal(WebSocketState.CloseSent, clientSocket.State);

            var buffer = new byte[1024];
            var result = await clientSocket.ReceiveAsync(new System.ArraySegment <byte>(buffer), CancellationToken.None);

            Assert.Equal(hello.Length, result.Count);
            Assert.True(hello.SequenceEqual(buffer.Take(hello.Length)));
            Assert.Equal(WebSocketMessageType.Text, result.MessageType);

            result = await clientSocket.ReceiveAsync(new System.ArraySegment <byte>(buffer), CancellationToken.None);

            Assert.Equal(world.Length, result.Count);
            Assert.True(world.SequenceEqual(buffer.Take(world.Length)));
            Assert.Equal(WebSocketMessageType.Binary, result.MessageType);

            result = await clientSocket.ReceiveAsync(new System.ArraySegment <byte>(buffer), CancellationToken.None);

            Assert.Equal(WebSocketMessageType.Close, result.MessageType);
            Assert.Equal(WebSocketState.Closed, clientSocket.State);

            clientSocket.Dispose();
        }
 public static System.Threading.Tasks.Task <int> ReceiveAsync(this System.Net.Sockets.Socket socket, System.ArraySegment <byte> buffer, System.Net.Sockets.SocketFlags socketFlags)
 {
     throw null;
 }
Esempio n. 9
0
        public async Task WebSocketWorks()
        {
            // Arrange
            RequestDelegate appDelegate = async ctx =>
            {
                if (ctx.WebSockets.IsWebSocketRequest)
                {
                    var websocket = await ctx.WebSockets.AcceptWebSocketAsync();

                    var receiveArray = new byte[1024];
                    while (true)
                    {
                        var receiveResult = await websocket.ReceiveAsync(new System.ArraySegment <byte>(receiveArray), CancellationToken.None);

                        if (receiveResult.MessageType == WebSocketMessageType.Close)
                        {
                            await websocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Normal Closure", CancellationToken.None);

                            break;
                        }
                        else
                        {
                            var sendBuffer = new System.ArraySegment <byte>(receiveArray, 0, receiveResult.Count);
                            await websocket.SendAsync(sendBuffer, receiveResult.MessageType, receiveResult.EndOfMessage, CancellationToken.None);
                        }
                    }
                }
            };
            var server = TestServer.Create(app =>
            {
                app.Run(appDelegate);
            });

            // Act
            var client       = server.CreateWebSocketClient();
            var clientSocket = await client.ConnectAsync(new System.Uri("http://localhost"), CancellationToken.None);

            var hello = Encoding.UTF8.GetBytes("hello");
            await clientSocket.SendAsync(new System.ArraySegment <byte>(hello), WebSocketMessageType.Text, true, CancellationToken.None);

            var world = Encoding.UTF8.GetBytes("world!");
            await clientSocket.SendAsync(new System.ArraySegment <byte>(world), WebSocketMessageType.Binary, true, CancellationToken.None);

            await clientSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Normal Closure", CancellationToken.None);

            // Assert
            Assert.Equal(WebSocketState.CloseSent, clientSocket.State);

            var buffer = new byte[1024];
            var result = await clientSocket.ReceiveAsync(new System.ArraySegment <byte>(buffer), CancellationToken.None);

            Assert.Equal(hello.Length, result.Count);
            Assert.True(hello.SequenceEqual(buffer.Take(hello.Length)));
            Assert.Equal(WebSocketMessageType.Text, result.MessageType);

            result = await clientSocket.ReceiveAsync(new System.ArraySegment <byte>(buffer), CancellationToken.None);

            Assert.Equal(world.Length, result.Count);
            Assert.True(world.SequenceEqual(buffer.Take(world.Length)));
            Assert.Equal(WebSocketMessageType.Binary, result.MessageType);

            result = await clientSocket.ReceiveAsync(new System.ArraySegment <byte>(buffer), CancellationToken.None);

            Assert.Equal(WebSocketMessageType.Close, result.MessageType);
            Assert.Equal(WebSocketState.Closed, clientSocket.State);

            clientSocket.Dispose();
        }
 public bool Equals(System.ArraySegment <T> obj)
 {
     return(default(bool));
 }
Esempio n. 11
0
        /// <summary>
        /// Compute xxHash for the data byte array
        /// </summary>
        /// <param name="data">The source of data</param>
        /// <param name="seed">The seed number</param>
        /// <returns>hash</returns>
        public static unsafe ulong ComputeHash(System.ArraySegment <byte> data, uint seed = 0)
        {
            Debug.Assert(data != null);

            return(ComputeHash(data.Array, data.Offset, data.Count, seed));
        }
Esempio n. 12
0
 public virtual bool TryGetBuffer(out System.ArraySegment <byte> buffer)
 {
     throw null;
 }
Esempio n. 13
0
        public static void OnFlatBuffersEntityUpdated(string name, System.ArraySegment <byte> range)
        {
            //EB.Debug.LogError("frameCount : {0} , name : {1}", UnityEngine.Time.frameCount, name);
            //EB.Debug.LogError("OnFlatBuffersEntityUpdated : {0}", name);

            FlatBuffers.ByteBuffer buffer = new FlatBuffers.ByteBuffer(range.Array, range.Offset);
            switch (name)
            {
            case "Combat":
                var combat = GM.DataCache.Combat.GetRootAsCombat(buffer);
                SkillTemplateManager.Instance.InitFromDataCache(combat);
                SkillTemplateManager.Instance.InitSkillLevelUpFromDataCache(combat);
                BuffTemplateManager.Instance.InitTemplateFromCache(combat);
                break;

            case "Economy":
                var economy = GM.DataCache.Economy.GetRootAsEconomy(buffer);
                EconemyTemplateManager.Instance.InitFromDataCache(economy);
                break;

            case "Task":
                var task = GM.DataCache.Task.GetRootAsTask(buffer);
                TaskTemplateManager.Instance.InitFromDataCache(task);
                break;

            case "Resource":
                var resource = GM.DataCache.Resource.GetRootAsResource(buffer);
                BuyResourceTemplateManager.Instance.InitFromDataCache(resource);
                break;

            case "Scene":
                var scene = GM.DataCache.Scene.GetRootAsScene(buffer);
                SceneTemplateManager.Instance.InitFromDataCache(scene);
                break;

            case "Character":
                var character = GM.DataCache.Character.GetRootAsCharacter(buffer);
                CharacterTemplateManager.Instance.InitFromDataCache(character);
                break;

            case "Guide":
                var guide = GM.DataCache.Guide.GetRootAsGuide(buffer);
                var gd    = guide.GetArray(0);
                GuideManager.Instance.InitConfigData(gd);
                GuideTemplateManager.Instance.InitFromDataCache(gd);
                DialogueTemplateManager.Instance.InitDialogueData(gd);
                GuideAudioTemplateManager.Instance.InitGuideAudioData(gd);
                MessageTemplateManager.Instance.InitFromDataCache(gd);
                FuncTemplateManager.Instance.InitFromDataCache(gd);
                PreviewTemplateManager.Instance.InitFromDataCache(gd);
                FuncActivedTemplateManager.Instance.InitFuncActivedData(gd);
                GuideNodeTemplateManager.Instance.InitGuideNode(gd);
                break;

            case "Event":
                var evt = GM.DataCache.Event.GetRootAsEvent(buffer);
                WelfareTemplateManager.Instance.InitTemplateFromCache(evt);
                EventTemplateManager.Instance.InitFromDataCache(evt);
                break;

            case "Vip":
                var vip = GM.DataCache.Vip.GetRootAsVip(buffer);
                VIPTemplateManager.Instance.InitTemplateFromCache(vip);
                break;

            case "Alliance":
                var alliance = GM.DataCache.Alliance.GetRootAsAlliance(buffer);
                AlliancesManager.Instance.Config.OnUpdateFunc(alliance);
                AllianceTemplateManager.Instance.InitFromDataCache(alliance);
                break;

            case "Shop":
                var shop = GM.DataCache.Shop.GetRootAsShop(buffer);
                ShopTemplateManager.Instance.InitFromDataCache(shop);
                break;

            case "NewGameConfig":
                var newGameConfig = GM.DataCache.NewGameConfig.GetRootAsNewGameConfig(buffer);
                NewGameConfigTemplateManager.Instance.InitFromDataCache(newGameConfig);
                break;
            }
        }