AsArray() public method

Returns the internal array of bytes the writer is using. This is NOT a copy.

public AsArray ( ) : byte[]
return byte[]
Ejemplo n.º 1
0
 private void PostInternalMessage(short msgType)
 {
   NetworkWriter networkWriter = new NetworkWriter();
   networkWriter.StartMessage(msgType);
   networkWriter.FinishMessage();
   this.PostInternalMessage(networkWriter.AsArray(), 0);
 }
Ejemplo n.º 2
0
 internal void InvokeHandlerOnClient(short msgType, MessageBase msg, int channelId)
 {
     NetworkWriter writer = new NetworkWriter();
     writer.StartMessage(msgType);
     msg.Serialize(writer);
     writer.FinishMessage();
     this.InvokeBytesOnClient(writer.AsArray(), channelId);
 }
Ejemplo n.º 3
0
 public static byte[] Serialize(INetMsg msg)
 {
     var networkWriter = new NetworkWriter();
     uint index = GetMsgIndexFromType( msg.GetType() );
     networkWriter.WritePackedUInt32( index );
     ((MessageBase)msg).Serialize(networkWriter);
     return networkWriter.AsArray();
 }
        // called by the server, to bypass network
        internal void InvokeHandlerOnClient(short msgType, MessageBase msg, int channelId)
        {
            // write the message to a local buffer
            NetworkWriter writer = new NetworkWriter();

            writer.StartMessage(msgType);
            msg.Serialize(writer);
            writer.FinishMessage();

            InvokeBytesOnClient(writer.AsArray(), channelId);
        }
Ejemplo n.º 5
0
    private byte[] LockTileMessage(World.Avatar avatar, byte tile)
    {
        var writer = new NetworkWriter();
        writer.Write((byte) Type.LockTile);
        writer.Write(avatar != null ? avatar.id : -1);
        writer.Write(tile);

        return writer.AsArray();
    }
Ejemplo n.º 6
0
    void Update()
    {
        if (!_isStarted)
            return;

        int recHostId;
        int connectionId;
        int channelId;
        byte[] recBuffer = new byte[1 << 14];
        int dataSize;
        byte error;

        if (!_isServer) {

            byte[] messageB = new byte[1<<13];
            int received = Receive(messageB, messageB.Length);
            if (received > 0) {
                byte[] temp = new byte[received];
                Array.Copy(messageB, temp, received);
                string value = ASCIIEncoding.ASCII.GetString(temp);
                Console.WriteLine(value);
                value += "0";
                temp = Encoding.ASCII.GetBytes(value);
                Send (temp, temp.Length);
            }

            //bool reinstantiateChunks = false;

            //while (true) {

            //    NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, recBuffer.Length, out dataSize, out error);
            //    if(error != 0)
            //    {
            //        Debug.Log ((NetworkError)error + " " + dataSize + " ");
            //        return;
            //    }
            //    if (recData == NetworkEventType.Nothing) {
            //        break;
            //    }

            //    switch(recData) {
            //    case NetworkEventType.ConnectEvent:
            //    {
            //        Debug.Log("Successfully connected to server");
            //        clientConnectionId = connectionId;
            //        break;
            //    }
            //    case NetworkEventType.DataEvent:  //if server will receive echo it will send it back to client, when client will receive echo from serve wit will send other message
            //    {
            //        NetworkReader nr = new NetworkReader(recBuffer);

            //        while (true) {
            //            try {
            //                switch ((ServerMessageType)nr.ReadUInt16()) {
            //                case ServerMessageType.SetChunk:
            //                    currentVessel.ReadSetChunkMessage(nr);
            //                    reinstantiateChunks = true;
            //                    break;
            //                case ServerMessageType.SyncVessel:
            //                    uint vesselIndex = nr.ReadUInt32();
            //                    ClientVessel cv;
            //                    if (!clientVessels.TryGetValue(vesselIndex, out cv)) {
            //                        cv = new ClientVessel(vesselIndex, nr);
            //                    } else {
            //                        cv.Sync(nr);
            //                    }

            //                    currentVessel = cv;
            //                    break;
            //                default:
            //                    break;
            //                }
            //            } catch (System.Exception ex) {
            //                break;
            //            }
            //        }

            //        if (reinstantiateChunks) {
            //            currentVessel.ReinstantiateChunks();
            //        }
            //        break;
            //    }
            //    case NetworkEventType.DisconnectEvent:
            //    {
            //        Debug.Log("Disconnect from host");
            //        _isStarted = false;
            //        break;
            //    }
            //    default:
            //        break;
            //    }
            //}

            //NetworkWriter nw = new NetworkWriter();

            //if (Input.GetMouseButtonDown(0)) {
            //    nw.Write((ushort)ClientMessageType.FillAt);
            //    nw.Write(MainCamera.CursorPosition);
            //}

            //currentPlayer.WriteInputMessage(nw);

            //if (nw.Position != 0) {
            //    if (Initialized) {
            //        NetworkTransport.Send(recHostId, clientConnectionId, ChannelId, nw.AsArray(), nw.Position, out error);
            //    }
            //}
            //if(error != 0)
            //    Debug.Log ((NetworkError)error);
        } else {
            try {
                NetworkReader nr = new NetworkReader(recBuffer);
                NetworkWriter nw = new NetworkWriter(new byte[1 << 14]);

                foreach (var vessel in ServerVessel.Vessels) {
                    vessel.Update(nw);
                }

                NetworkEventType recData;
                while (true) {
                    recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, recBuffer.Length, out dataSize, out error);
                    if(error != 0)
                    {
                        Debug.Log ((NetworkError)error + " " + dataSize + " ");
                        Shutdown();
                        return;
                    } else if (recData == NetworkEventType.Nothing) {
                        break;
                    } else {
                        switch(recData) {
                        case NetworkEventType.ConnectEvent:
                        {
                            Debug.Log("Client connecting");
                            //start this player off from the beginning
                            StartingVessel startVessel = StartingVessel.GetStartingVessel();
                            GameObject playerGO = GameObject.Instantiate(playerPrefab) as GameObject;
                            ServerPlayer player = playerGO.AddComponent<ServerPlayer>();
                            serverPlayers.Add(recHostId, player);
                            player.hostId = recHostId;
                            startVessel.AddPlayer(player, nw);

                            break;
                        }
                        case NetworkEventType.DataEvent:  //if server will receive echo it will send it back to client, when client will receive echo from serve wit will send other message
                        {
                            ServerPlayer player;
                            try {
                                player = serverPlayers[recHostId];
                            } catch (System.Exception ex) {
                                //							string pairs = "";
                                //							foreach (KeyValuePair<int, ServerPlayer> kvp in serverPlayers)
                                //							{
                                //								//textBox3.Text += ("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
                                //								pairs += string.Format("Key = {0}, Value = {1}", kvp.Key.ToString(), kvp.Value.ToString()) + "\n";
                                //							}
                                //							Debug.LogError("Failed to get ServerPlayer with hostId: " + recHostId.ToString() + "\n" + pairs);
                                break;
                            }

                            Debug.Log ("Client message received");

                            nr.SeekZero();
                            nw.SeekZero();
                            while (nr.Position != dataSize) {
                                switch ((ClientMessageType)nr.ReadUInt16()) {
                                case ClientMessageType.RequestChunk:
                                    Debug.Log ("ClientMessageType.RequestChunk");
                                    player.currentVessel.HandleChunkRequestMessage(nr, nw);
                                    break;
                                case ClientMessageType.Inputs:
                                    Debug.Log ("ClientMessageType.Inputs");
                                    player.ReadInputsMessage(nr);
                                    break;
                                case ClientMessageType.FillAt:
                                    Debug.Log ("ClientMessageType.FillAt");
                                    player.currentVessel.HandleFillAtMessage(nr);
                                    break;
                                default:
                                    break;
                                }
                            }

                            if (nw.Position != 0) {
                                Debug.Log("Sending response: " + recHostId + "," + connectionId + "," + channelId + "," + nw.AsArray() + "," + nw.Position + ".");
                                NetworkTransport.Send(recHostId, connectionId, channelId, nw.AsArray(), nw.Position, out error);
                            }
                            if(error != 0)
                            {
                                Debug.Log ((NetworkError)error);
                                Shutdown();
                                return;
                            }
                            break;
                        }
                        case NetworkEventType.DisconnectEvent:
                        {
                            Debug.Log("Client disconnecting");
                            ServerPlayer player = serverPlayers[recHostId];
                            player.currentVessel.RemovePlayer(player, nw);
                            serverPlayers.Remove(recHostId);
                            break;
                        }
                        default:
                            break;
                        }
                    }
                }
            } catch (System.Exception ex) {
                //Debug.Log(ex.StackTrace);
                Shutdown ();
            }
        }
    }
 public override bool SendWriter(NetworkWriter writer, int channelId) => 
     this.m_LocalServer.InvokeBytes(this, writer.AsArray(), (short) writer.AsArray().Length, channelId);
 public override bool SendWriter(NetworkWriter writer, int channelId)
 {
     return this.m_LocalServer.InvokeBytes(this, writer.AsArray(), (short) writer.AsArray().Length, channelId);
 }
Ejemplo n.º 9
0
    private IEnumerator SendWorld(int connectionID, World world)
    {
        {
            var writer = new NetworkWriter();

            writer.Write((byte) Type.Tilemap);
            writer.WriteBytesFull(world.tilemap);

            Send(connectionID, writer.AsArray());
        }

        {
            var writer = new NetworkWriter();

            writer.Write((byte) Type.Palette);
            
            for (int i = 0; i < 16; ++i) writer.Write((Color32) world.palette[i]);

            Send(connectionID, writer.AsArray());
        }

        {
            var writer = new NetworkWriter();

            writer.Write((byte) Type.Walls);
            writer.WriteBytesFull(world.walls.ToArray());

            Send(connectionID, writer.AsArray());
        }

        foreach (var avatar in world.avatars)
        {
            Send(connectionID, ReplicateAvatarMessage(avatar));

            if (avatar.id == connectionID)
            {
                Send(connectionID, GiveAvatarMessage(avatar));
            }
            else
            {
                Send(connectionID, AvatarInChunksMessages(world, avatar));
            }
        }

        for (int i = 0; i < maxTiles; ++i)
        {
            yield return new WaitForSeconds(0.125f);

            Send(connectionID, TileInChunksMessages(world, (byte) i));
        }
    }
Ejemplo n.º 10
0
 public override bool SendWriter(NetworkWriter writer, int channelId)
 {
     this.m_LocalClient.InvokeBytesOnClient(writer.AsArray(), channelId);
     return true;
 }
Ejemplo n.º 11
0
 public override bool SendWriter(NetworkWriter writer, int channelId)
 {
     return(this.m_LocalServer.InvokeBytes(this, writer.AsArray(), (int)((short)writer.AsArray().Length), channelId));
 }
Ejemplo n.º 12
0
    private byte[][] TileInChunksMessages(World world,
                                          byte tile,
                                          int size = 128)
    {
        int x = tile % 16;
        int y = tile / 16;

        Color[] colors = world.tileset.GetPixels(x * 32, y * 32, 32, 32);
        byte[] bytes = colors.Select(c => world.ColorToPalette(c, true)).ToArray();
        byte[] chunk;

        int offset = 0;

        var messages = new List<byte[]>();

        while (bytes.Any())
        {
            chunk = bytes.Take(size).ToArray();
            bytes = bytes.Skip(size).ToArray();

            var writer = new NetworkWriter();
            writer.Write((byte) Type.TileChunk);
            writer.Write(tile);
            writer.Write(offset);
            writer.WriteBytesFull(CrunchBytes(chunk));

            messages.Add(writer.AsArray());

            offset += size;
        }

        return messages.ToArray();
    }
Ejemplo n.º 13
0
    private byte[] ReplicateAvatarMessage(World.Avatar avatar)
    {
        var writer = new NetworkWriter();
        writer.Write((byte) Type.ReplicateAvatar);
        writer.Write(avatar.id);
        writer.Write(avatar.destination);
        writer.Write(avatar.source);

        return writer.AsArray();
    }
Ejemplo n.º 14
0
    private byte[] SetWallMessage(byte tile, bool wall)
    {
        var writer = new NetworkWriter();
        writer.Write((byte) Type.SetWall);
        writer.Write(tile);
        writer.Write(wall);

        return writer.AsArray();
    }
Ejemplo n.º 15
0
 public override bool SendWriter(NetworkWriter writer, int channelId)
 {
     this.m_LocalClient.InvokeBytesOnClient(writer.AsArray(), channelId);
     return(true);
 }
Ejemplo n.º 16
0
    private byte[] SetTileMessage(int location,
                                  byte tile)
    {
        var writer = new NetworkWriter();
        writer.Write((byte) Type.SetTile);
        writer.Write(location);
        writer.Write(tile);

        return writer.AsArray();
    }
Ejemplo n.º 17
0
    private byte[] PaletteEditMessage(byte index)
    {
        var writer = new NetworkWriter();
        writer.Write((byte) Type.PaletteEdit);
        writer.Write(index);
        writer.Write((Color32) world.palette[index]);

        return writer.AsArray();
    }
Ejemplo n.º 18
0
    private byte[] ChatMessage(World.Avatar avatar, string message)
    {
        var writer = new NetworkWriter();
        writer.Write((byte) Type.Chat);
        writer.Write(avatar.id);
        writer.Write(message);

        return writer.AsArray();
    }
Ejemplo n.º 19
0
    private byte[] MoveAvatarMessage(World.Avatar avatar,
                                     Vector2 destination)
    {
        var writer = new NetworkWriter();
        writer.Write((byte) Type.MoveAvatar);
        writer.Write(avatar.id);
        writer.Write(destination);

        return writer.AsArray();
    }
Ejemplo n.º 20
0
    private byte[] GiveAvatarMessage(World.Avatar avatar)
    {
        var writer = new NetworkWriter();
        writer.Write((byte) Type.GiveAvatar);
        writer.Write(avatar.id);

        return writer.AsArray();
    }
Ejemplo n.º 21
0
 public NetworkReader(NetworkWriter writer)
 {
     this.m_buf = new NetBuffer(writer.AsArray());
     NetworkReader.Initialize();
 }
Ejemplo n.º 22
0
    private byte[][] AvatarInChunksMessages(World world,
                                            World.Avatar avatar,
                                            int size = 128)
    {
        Color32[] colors = avatar.graphic.texture.GetPixels32();
        byte[] bytes = colors.Select(c => world.ColorToPalette(c, true)).ToArray();
        byte[] chunk;

        int offset = 0;

        var messages = new List<byte[]>();

        while (bytes.Any())
        {
            chunk = bytes.Take(size).ToArray();
            bytes = bytes.Skip(size).ToArray();

            var writer = new NetworkWriter();
            writer.Write((byte) Type.AvatarChunk);
            writer.Write(avatar.id);
            writer.Write(offset);
            writer.WriteBytesFull(CrunchBytes(chunk));

            messages.Add(writer.AsArray());

            offset += size;
        }

        return messages.ToArray();
    }