SeekZero() public method

Seeks to the start of the internal buffer.

public SeekZero ( ) : void
return void
Example #1
0
 private void FixedUpdate()
 {
     if (m_ParameterWriter == null)
     {
         return;
     }
     CheckSendRate();
     if (CheckAnimStateChanged(out int stateHash, out float normalizedTime))
     {
         AnimationMessage animationMessage = new AnimationMessage();
         animationMessage.netId          = base.netId;
         animationMessage.stateHash      = stateHash;
         animationMessage.normalizedTime = normalizedTime;
         m_ParameterWriter.SeekZero();
         WriteParameters(m_ParameterWriter, autoSend: false);
         animationMessage.parameters = m_ParameterWriter.ToArray();
         if (base.hasAuthority || ClientScene.readyConnection != null)
         {
             ClientScene.readyConnection.Send(40, animationMessage);
         }
         else if (base.isServer && !base.localPlayerAuthority)
         {
             NetworkServer.SendToReady(base.gameObject, 40, animationMessage);
         }
     }
 }
Example #2
0
        void FixedUpdate()
        {
            if (!sendMessagesAllowed)
            {
                return;
            }

            if (m_ParameterWriter == null)
            {
                m_ParameterWriter = new NetworkWriter();
            }

            CheckSendRate();

            int   stateHash;
            float normalizedTime;

            if (!CheckAnimStateChanged(out stateHash, out normalizedTime))
            {
                return;
            }

            var animMsg = new AnimationMessage();

            animMsg.netId          = netId;
            animMsg.stateHash      = stateHash;
            animMsg.normalizedTime = normalizedTime;

            m_ParameterWriter.SeekZero();
            WriteParameters(m_ParameterWriter, false);
            animMsg.parameters = m_ParameterWriter.ToArray();

            SendMessage(MsgType.Animation, animMsg);
        }
        void FixedUpdate()
        {
            // check for authority
            if (m_ParameterWriter == null)
            {
                return;
            }

            CheckSendRate();

            int   stateHash;
            float normalizedTime;

            if (!CheckAnimStateChanged(out stateHash, out normalizedTime))
            {
                return;
            }

            var animMsg = new AnimationMessage();

            animMsg.netId          = netId;
            animMsg.stateHash      = stateHash;
            animMsg.normalizedTime = normalizedTime;

            m_ParameterWriter.SeekZero();
            WriteParameters(m_ParameterWriter, false);
            animMsg.parameters = m_ParameterWriter.ToArray();

            if (hasAuthority || ClientScene.readyConnection != null)
            {
                ClientScene.readyConnection.Send(MsgType.Animation, animMsg);
                return;
            }

            // for server-based objects
            if (isServer && !localPlayerAuthority)
            {
                NetworkServer.SendToReady(gameObject, MsgType.Animation, animMsg);
            }
        }
Example #4
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 ();
            }
        }
    }
Example #5
0
 public void WriteSyncVessel(NetworkWriter nw)
 {
     nw.SeekZero();
     nw.Write((ushort)ServerMessageType.SyncVessel);
     nw.Write(Index);
     nw.Write(interiorPosition);
 }
Example #6
0
    public void RemovePlayer(ServerPlayer player, NetworkWriter nw)
    {
        int id = playersOnBoard.IndexOf(player);
        playersOnBoard.RemoveAt(id);
        player.currentVessel = null;
        if (playersOnBoard.Count == 0) {
            noPlayers = true;
        }

        if (GameManager.Instance.Initialized) {
            nw.SeekZero();
            nw.Write((ushort)ServerMessageType.RemovePlayer);
            nw.Write((ushort)id);
            for (int i = 0; i < playersOnBoard.Count; i++) {
                //send
            }
        }
    }
Example #7
0
    public void AddPlayer(ServerPlayer player, Vector2 position, NetworkWriter nw)
    {
        if (!interiorExists) {
            AllocateInteriorSpace();
        }

        player.transform.position = (Vector3)(interiorPosition + position);

        player.currentVessel = this;
        playersOnBoard.Add(player);
        player.ChunkI = WorldToChunkI((Vector2)player.transform.position);
        InstantiateNearbyChunks(player);

        noPlayers = false;
        timeEmpty = 0.0f;

        if (GameManager.Instance.Initialized) {
            WriteSyncVessel(nw);
            //send
        }
        if (GameManager.Instance.Initialized) {
            nw.SeekZero();
            nw.Write((ushort)ServerMessageType.AddPlayer);
            nw.Write((ushort)(playersOnBoard.Count - 1));
            nw.Write((Vector2)player.transform.position);
            //TODO: write player info here
            for (int i = 0; i < playersOnBoard.Count; i++) {
                //send
            }
        }
    }