public static void SendUpdate(ref UdpBitStream bitstream, ref UdpBitStream outstream) { // Send the bitstream to the UNET writer writer.StartMessage(masterMsgTypeId); writer.WriteUncountedByteArray(NSTMaster.bitstreamByteArray, bitstream.BytesUsed); writer.FinishMessage(); // if this is the server - send to all. if (NetworkServer.active) { writer.SendPayloadArrayToAllClients(masterMsgTypeId, Channels.DefaultUnreliable); //NetworkServer.connections[0].FlushChannels(); // If this is the server as client, run the ReceiveUpdate since local won't get this run. //if (NetworkClient.active) NSTMaster.ReceiveUpdate(ref bitstream, ref outstream, false, 0); } // if this is a client send to server. else { // TODO: find reliable way to cache this condition - Is here to eliminate some shut down warnings, and not critical if (cachedNetworkClient != null && cachedNetworkClient.isConnected) { NetworkManager.singleton.client.SendWriter(writer, Channels.DefaultUnreliable); } //NetworkManager.singleton.client.connection.FlushChannels(); } }
public static CompressedElement ReadCompressedPosFromBitstream(ref UdpBitStream bitstream, IncludedAxes ia, BitCullingLevel bcl) { return new CompressedElement( (ia.IsXYZ(0)) ? (bitstream.ReadUInt(axisRanges[0].BitsAtCullLevel(bcl))) : 0, (ia.IsXYZ(1)) ? (bitstream.ReadUInt(axisRanges[1].BitsAtCullLevel(bcl))) : 0, (ia.IsXYZ(2)) ? (bitstream.ReadUInt(axisRanges[2].BitsAtCullLevel(bcl))) : 0); }
public static uint WriteAxisToBitstream(this float val, int axis, ref UdpBitStream bitstream, bool lowerBitsOnly = false) { uint compressedAxis = val.CompressAxis(axis); bitstream.WriteUInt(val.CompressAxis(axis), lowerBitsOnly ? axisRanges[axis].lowerBits : axisRanges[axis].bits); return(compressedAxis); }
public static void Send(ref UdpBitStream bitstream, int updateCounter) { // If this is the server and it is due to ping... ping //TODO this hard coded 60 will likely go. //TODO this should probably be offset by 1 to keep frame zero from becoming massive with all the keyframes of NST if (MasterNetAdapter.ServerIsActive && updateCounter % 20 == 0 && updateCounter != 60) { svrPingInitiateTime = Time.time; bitstream.WriteBool(true); } // If this is a client, and it has rcvd a server ping, it needs to respond next tick. else if (clientNeedsToRespondToPing) { bitstream.WriteBool(true); // write how long the client waited to reply in ms (1023 ms max with 10 bits) bitstream.WriteInt((int)((Time.time - clntPingArriveTime) * 1000), 10); clientNeedsToRespondToPing = false; } // nothing happening this pass. else { bitstream.WriteBool(false); } }
public static void SendBitstreamToThisConn(this NetworkConnection conn, ref UdpBitStream bitstream, int channel = Channels.DefaultUnreliable) { reusablewriter.StartMessage(NSTSettings.single.masterMsgTypeId); reusablewriter.WriteUncountedByteArray(bitstream.Data, bitstream.BytesUsed); reusablewriter.FinishMessage(); conn.SendWriter(reusablewriter, channel); }
public static CompressedElement ReadCompressedPosFromBitstream(ref UdpBitStream bitstream, IncludedAxes ia, bool lowerBitsOnly = false) { return(new CompressedElement( (ia.IsXYZ(0)) ? (bitstream.ReadUInt(lowerBitsOnly ? axisRanges[0].lowerBits : axisRanges[0].bits)) : 0, (ia.IsXYZ(1)) ? (bitstream.ReadUInt(lowerBitsOnly ? axisRanges[1].lowerBits : axisRanges[1].bits)) : 0, (ia.IsXYZ(2)) ? (bitstream.ReadUInt(lowerBitsOnly ? axisRanges[2].lowerBits : axisRanges[2].bits)) : 0)); }
// callback from NST, extract transform elements public void NSTBitstreamIncomingFirst(Frame frame, Frame currFrame, ref UdpBitStream bitstream, bool isServer) { for (int eid = 0; eid < elementCount; ++eid) { transformElements[eid].frames[frame.frameid].hasChanged = transformElements[eid].Read(ref bitstream, frame, currFrame); } }
/// <summary> /// Ping all owned NSTs for any due updates. Passes the bitstream to that NST to write to. /// </summary> public static void PollAllForUpdates() { //Debug.Log("<b><color=green>Mstr Ping</color></b> " + (Time.time - tempMasterPollTime)); //tempMasterPollTime = Time.time; UdpBitStream bitstream = new UdpBitStream(bitstreamByteArray); UdpBitStream outstream = new UdpBitStream(); bool foundUpdate = false; for (int i = 0; i < NetworkSyncTransform.allNsts.Count; i++) { foundUpdate |= NetworkSyncTransform.allNsts[i].PollForUpdate(ref bitstream); } // No Updates, we are done. if (!foundUpdate) { return; } // Write the end of stream marker of 00 bitstream.WriteBool(false); mna.SendUpdate(ref bitstream, ref outstream); //Debug.Log("<b><color=blue>Mstr Snd</color></b> " + (Time.time - tempMasterSendTime)); //tempMasterSendTime = Time.time; }
public override void MirrorToClients(ref UdpBitStream outstream, Frame frame, bool hasChanged) { // Write the used flag (if this is not a forced update) and determine if an update needs to be written. if (WriteUpdateFlag(ref outstream, frame, hasChanged) == false) { return; } if (rotationType == RotationType.Quaternion) { outstream.WriteULong(frame.elements[index].compTrans, totalBitsForQuat); } else { for (int i = 0; i < 3; i++) { if (includedAxes.IsXYZ(i)) { outstream.WriteUInt(frame.elements[index].compTrans[i], axes[i].bits); } } } lastSentCompressed = frame.elements[index].compTrans; }
public void Unpack(byte[] data) { var stream = new UdpBitStream(data, data.Length); id = stream.ReadInt(); //position = stream.ReadVector3(); }
public bool Write(ref UdpBitStream bitstream, Frame frame) { ElementFrame e = frames[frame.frameid]; e.compXform = Compress(); e.xform = Localized; CompressedElement newComp = e.compXform; bool forceUpdate = IsUpdateForced(frame); // For frames between forced updates, we need to first send a flag bit for if this element is being sent if (!forceUpdate) { bool hasChanged = !CompressedElement.Compare(newComp, lastSentCompressed) && sendCullMask.OnChanges(); bitstream.WriteBool(hasChanged); // if no changes have occured we are done. if (!hasChanged) { return(false); } } crusher.Write(e.compXform, bitstream.Data, ref bitstream.ptr); lastSentCompressed = newComp; lastSentTransform = e.xform; return(true); }
public bool Read(ref UdpBitStream bitstream, Frame frame, Frame currentFrame) { ElementFrame e = frames[frame.frameid]; bool forcedUpdate = IsUpdateForced(frame); bool applyToGhost = ShouldApplyToGhost(frame); bool isCurrentFrame = frame == currentFrame; // Only read for the sent bit if not forced, there is no check bit for forced updates (since all clients and server know it is forced) bool hasChanged = forcedUpdate || bitstream.ReadBool(); if (!hasChanged) { // Leave the transform as is if this is the current frame and hasn't changed - it has already been extrapolated and is mid-lerp // So leave it alone. Otherwise sete it to GenericX.NULL just to make debugging easier. Eventually can remove this. if (!isCurrentFrame) { e.xform = GenericX.NULL; e.compXform = CompressedElement.Empty; } return(false); } e.compXform = crusher.Read(bitstream.Data, ref bitstream.ptr); e.xform = Decompress(e.compXform); if (applyToGhost) { Apply(e.xform, ghostGO); } return(true); }
/// <summary> /// Write the current scene ID into the bitstream. /// </summary> /// <param name="bitstream"></param> public static void Serialize(ref UdpBitStream bitstream) { if (includeSceneIndexInUpdates) { bitstream.WriteInt(_currentSceneIndex, bitsForSceneIndex); } }
public override bool Write(ref UdpBitStream bitstream, Frame frame) { // Base class does some forceUpdate checking, keep it around. bool forceUpdate = IsUpdateForced(frame); ElementFrame e = frames[frame.frameid]; e.compXform = Compress(); e.xform = Localized; CompressedElement newComp = e.compXform; if (rotationType == RotationType.Quaternion) { // For frames between forced updates, we need to first send a flag bit for if this element is being sent if (!forceUpdate) { bool hasChanged = newComp.quat != lastSentCompressed.quat && sendCullMask.OnChanges(); bitstream.WriteBool(hasChanged); // if no changes have occured we are done. if (!hasChanged) { return(false); } } bitstream.WriteULong(newComp.quat, totalBitsForQuat); lastSentCompressed.quat = newComp.quat; lastSentTransform = e.xform; return(true); } else { // For frames between forced updates, we need to first send a flag bit for if this element is being sent if (!forceUpdate) { bool hasChanged = !CompressedElement.Compare(newComp, lastSentCompressed) && sendCullMask.OnChanges(); bitstream.WriteBool(hasChanged); // if no changes have occured we are done. if (!hasChanged) { return(false); } } for (int axis = 0; axis < 3; axis++) { if (includedAxes.IsXYZ(axis)) { bitstream.WriteUInt(newComp[axis], axes[axis].bits); lastSentCompressed[axis] = newComp[axis]; } } return(true); } }
public static uint WriteAxisToBitstream(this float val, int axis, ref UdpBitStream bitstream, BitCullingLevel bcl) { uint compressedAxis = val.CompressAxis(axis); bitstream.WriteUInt(val.CompressAxis(axis), axisRanges[axis].GetBitsAtCullLevel(bcl)); return(compressedAxis); }
public void NSTBitstreamMirrorFirst(Frame frame, ref UdpBitStream outstream, bool isServer, bool waitingForTeleportConfirm) { for (int eid = 0; eid < transformElements.Length; eid++) { transformElements[eid].MirrorToClients(ref outstream, frame, frame.elements[eid].hasChanged); // masks[eid].GetBitInMask(frame.frameid)); } }
// callback from NST, extract transform elements public void NSTBitstreamIncomingFirst(Frame frame, Frame currFrame, ref UdpBitStream bitstream, bool isServer, bool waitingForTeleportConfirm) { for (int eid = 0; eid < transformElements.Length; eid++) { frame.elements[eid].hasChanged = transformElements[eid].Read(ref bitstream, frame, currFrame); } }
/// <summary> /// Clients receive reports about health as part of their incoming streams. The server will have added them to its outgoing/mirror streams. /// </summary> public void NSTBitstreamIncomingFirst(Frame frame, Frame currFrame, ref UdpBitStream bitstream, bool isServer) { bool iAmActingAuthority = na.IAmActingAuthority; // If this is the server and has authority, it will receieve its own outgoings... account for that. // TODO: Cache this down the line with callbacks from the net engine to notify of ownership changes bool noIncomingExpected = MasterNetAdapter.ServerIsActive && iAmActingAuthority && (NetLibrarySettings.single.defaultAuthority == DefaultAuthority.ServerAuthority || MasterNetAdapter.NET_MODEL == NetworkModel.ServerClient) && !na.IsMine; // if we are authority, any incoming messages from others will not include a health. Only the authority should write health. if (noIncomingExpected) { return; } if (UpdateDue(frame.frameid)) { for (int i = 0; i < vitalsCount; ++i) { Vital v = vitals[i]; int incval = bitstream.ReadInt(v.bitsForStat); if (!iAmActingAuthority) { v.Value = incval; } } if (!iAmActingAuthority) { UpdateMonitors(); } } }
public override bool ReadFromBitstream(ref UdpBitStream bitstream, MsgType msgType, Frame targetFrame, int i, bool forcedUpdate, bool isKeyframe) { // Only read for the sent bit if not forced, there is no check bit for forced updates (since all clients and server know it is forced) bool hasChanged = forcedUpdate || bitstream.ReadBool(); if (!hasChanged) { targetFrame.rotations[i] = GenericX.NULL; } else if (rotationType == XType.Quaternion) { targetFrame.rotations[i] = bitstream.ReadULong(totalBitsForQuat).DecompressBitBufferToQuat(totalBitsForQuat); } else { targetFrame.rotations[i] = new GenericX( (rotationType.IsX()) ? (bitstream.ReadUInt(xyzBits[0]) * xyzUnmult[0] + xyzMin[0]) : 0, (rotationType.IsY()) ? (bitstream.ReadUInt(xyzBits[1]) * xyzUnmult[1] + xyzMin[1]) : 0, (rotationType.IsZ()) ? (bitstream.ReadUInt(xyzBits[2]) * xyzUnmult[2] + xyzMin[2]) : 0, rotationType); } return(hasChanged); }
public static void ReadCheck(ref UdpBitStream bitStream, string tag) { val = bitStream.ReadByte(TEST_BITS_SIZE); if (val != TEST_VAL) { UnityEngine.Debug.LogError("INTEGRITY FAIL " + tag + " " + Utilities.BitUtilities.BitTools.PrintBitMask(val)); } }
public static void ReadCheck(ref UdpBitStream bitStream, ref UdpBitStream outstream, string tag, bool mirror = false) { ReadCheck(ref bitStream, tag); if (mirror) { WriteCheck(ref outstream); } }
public void NSTBitstreamMirrorFirst(Frame frame, ref UdpBitStream outstream, bool waitingForTeleportConfirm) { for (int eid = 0; eid < elementCount; ++eid) { TransformElement te = transformElements[eid]; te.MirrorToClients(ref outstream, frame, te.frames[frame.frameid].hasChanged); // masks[eid].GetBitInMask(frame.frameid)); } }
public byte[] Pack() { byte[] data = new byte[16]; var stream = new UdpBitStream(data, data.Length); stream.WriteInt(id); //stream.WriteVector3(position); return(data); }
public override bool Unpack(ref UdpBitStream buffer, ref object o) { pair p = new pair(); p.seq = buffer.ReadUInt(32); p.val = buffer.ReadUInt(32); o = p; return(true); }
public override bool Pack(ref UdpBitStream buffer, ref object o) { pair p = (pair)o; buffer.WriteUInt(p.seq, 32); buffer.WriteUInt(p.val, 32); UdpLog.User("sending {0} (seq: {1})", p.val, p.seq); return(true); }
public void NSTBitstreamOutgoingFirst(Frame frame, ref UdpBitStream bitstream) { for (int eid = 0; eid < elementCount; ++eid) { transformElements[eid].Write(ref bitstream, frame); // Write to the local buffer transformElements[eid].frames[frame.frameid].xform = transformElements[eid].Localized; } }
/// <summary> /// Server adds health info to altered outgoing streams that have come in from players. /// </summary> public void NSTBitstreamMirrorFirst(Frame frame, ref UdpBitStream outstream, bool waitingForTeleportConfirm) { if (UpdateDue(frame.frameid)) { for (int i = 0; i < vitalsCount; ++i) { outstream.WriteInt((int)vitals[i].Value, vitals[i].bitsForStat); } } }
public void NSTBitstreamOutgoingFirst(Frame frame, ref UdpBitStream bitstream) { for (int elementid = 0; elementid < transformElements.Length; elementid++) { transformElements[elementid].Write(ref bitstream, frame); // Write to the local buffer frame.elements[elementid].transform = transformElements[elementid].Localized; } }
public override bool Unpack(ref UdpBitStream stream, ref object o) { // read length and create array, then read bytes into array byte[] bytes = new byte[stream.ReadInt()]; stream.ReadByteArray(bytes); // convert bytes to string o = Encoding.UTF8.GetString(bytes); return(true); }
public void MirrorToClients(ref UdpBitStream outstream, Frame frame, bool hasChanged) { // Write the used flag (if this is not a forced update) and determine if an update needs to be written. if (WriteUpdateFlag(ref outstream, frame, hasChanged) == false) { return; } ElementFrame e = frames[frame.frameid]; crusher.Write(e.compXform, outstream.Data, ref outstream.ptr); }
public void Pack(UdpBitStream buffer, UdpSocket socket) { buffer.WriteUShort(PadSequence(ObjSequence), 16); buffer.WriteUShort(PadSequence(AckSequence), 16); buffer.WriteULong(AckHistory, socket.Config.AckRedundancy); if (socket.Config.CalculateNetworkPing) { buffer.WriteUShort(AckTime, 16); } if (socket.Config.WritePacketBitSize) { buffer.WriteUShort(BitSize, 16); } }
public void Unpack(UdpBitStream buffer, UdpSocket socket) { ObjSequence = TrimSequence(buffer.ReadUShort(16)); AckSequence = TrimSequence(buffer.ReadUShort(16)); AckHistory = buffer.ReadULong(socket.Config.AckRedundancy); if (socket.Config.CalculateNetworkPing) { AckTime = buffer.ReadUShort(16); } if (socket.Config.WritePacketBitSize) { BitSize = buffer.ReadUShort(16); } }
public override bool Pack(ref UdpBitStream buffer, ref object o) { pair p = (pair) o; buffer.WriteUInt(p.seq, 32); buffer.WriteUInt(p.val, 32); UdpLog.User("sending {0} (seq: {1})", p.val, p.seq); return true; }
public override bool Unpack(ref UdpBitStream buffer, ref object o) { pair p = new pair(); p.seq = buffer.ReadUInt(32); p.val = buffer.ReadUInt(32); o = p; return true; }