void SerializeObjectToBuffer(ushort msgType, IObjectSerializer serializer) { m_NetworkWriter.SeekZero(); m_NetworkWriter.StartMessage(msgType); serializer.Serialize(m_NetworkSyncWriter); m_NetworkWriter.FinishMessage(); }
void SerializeMessageToBuffer(ushort msgType, IMessageSerializer serializer) { m_NetworkWriter.SeekZero(); m_NetworkWriter.StartMessage(msgType); serializer.Serialize(m_NetworkWriter); m_NetworkWriter.FinishMessage(); }
internal int CopyPacketToNetworkWriter(ArraySegment <byte> packet, [NotNull] NetworkWriter writer) { if (writer == null) { throw new ArgumentNullException("writer"); } var arr = packet.Array; if (arr == null) { throw new ArgumentNullException("packet"); } writer.SeekZero(); writer.StartMessage(TypeCode); { //Length prefix the packet writer.WritePackedUInt32((uint)packet.Count); //Copy out the bytes. //You might think we could use `Write(buffer, offset, count)` here. You would be wrong! In that method the 'offset' is the //offset to write to in the packet! This is probably a bug in unity! for (var i = 0; i < packet.Count; i++) { writer.Write(arr[packet.Offset + i]); // } } writer.FinishMessage(); return(writer.Position); }
//This is called on the client to send the current inputs void SendInputs(ref List <Inputs> inp) { if (!isLocalPlayer || isServer) { return; } if (inputWriter == null) { inputWriter = new NetworkWriter(); } inputWriter.SeekZero(); inputWriter.StartMessage(inputMessage); int sz = inp.Count; inputWriter.WritePackedUInt32((uint)sz); for (int i = 0; i < sz; i++) { cInp = inp[i]; #if (CMD_CHECKSUM) cInp.checksum = GetCommandChecksum(cInp); #endif WriteInputs(ref inputWriter, cInp, prevInput); prevInput = cInp; } inputWriter.FinishMessage(); myClient.SendWriter(inputWriter, GetNetworkChannel()); inp.Clear(); }
public void OnSyncButtonClick() { if (m_ConnectionEstablished) { m_Writer.SeekZero(); m_Writer.Write((int)Protocol.SyncArchive1); CustomStarUsage.m_Instance.Searialize(m_Writer); byte error; for (int i = 0; i < m_ConnectionIds.Count; ++i) { NetworkTransport.Send(m_HostId, m_ConnectionIds[i], 0, m_Writer.AsArray(), m_Writer.Position, out error); if ((NetworkError)error != NetworkError.Ok) { Debug.LogError("Failed to send message: " + (NetworkError)error); } } m_Writer.SeekZero(); m_Writer.Write((int)Protocol.SyncArchive2); Days.m_Instance.Searialize(m_Writer); for (int i = 0; i < m_ConnectionIds.Count; ++i) { NetworkTransport.Send(m_HostId, m_ConnectionIds[i], 0, m_Writer.AsArray(), m_Writer.Position, out error); if ((NetworkError)error != NetworkError.Ok) { Debug.LogError("Failed to send message: " + (NetworkError)error); } } m_Writer.SeekZero(); m_Writer.Write((int)Protocol.SyncArchive3); UseStarHistory.m_Instance.Searialize(m_Writer); for (int i = 0; i < m_ConnectionIds.Count; ++i) { NetworkTransport.Send(m_HostId, m_ConnectionIds[i], 0, m_Writer.AsArray(), m_Writer.Position, out error); if ((NetworkError)error != NetworkError.Ok) { Debug.LogError("Failed to send message: " + (NetworkError)error); } } } }
private NetworkWriter __GetWriter() { if (__writer == null) { __writer = new NetworkWriter(); } else { __writer.SeekZero(); } return(__writer); }
public NetworkWriter RpcStart() { if (__writer == null) { __writer = new NetworkWriter(); } else { __writer.SeekZero(); } return(__writer); }
//Write the position and rotation to the data structure public virtual void Tick(ref RecordData data) { if (writer == null) { writer = new NetworkWriter(); } else { writer.SeekZero(); } writer.Write(transform.position); writer.Write(transform.rotation); data.bytes = writer.AsArray(); }
// Send sphere data every so often to those connected IEnumerator SendCoroutine() { // Serialize list NetworkWriter nw = new NetworkWriter(); Renderer rend; while (true) { yield return(new WaitForSeconds(0.1f)); // Anything to do? if (connectList.Count == 0 || sphereList.Count == 0) { continue; } // Reset stream nw.SeekZero(); foreach (var item in sphereList) { nw.Write(item.GetInstanceID()); nw.Write(item.transform.position); rend = item.GetComponent <Renderer>(); nw.Write(rend.material.color.r); nw.Write(rend.material.color.g); nw.Write(rend.material.color.b); // Don't pack too much // Fix me! Send more than one packet instead if (nw.Position > 1300) { break; } } // Send data out byte[] buffer = nw.ToArray(); byte error; //Debug.Log(string.Format("Sending data size {0}", buffer.Length)); foreach (var item in connectList) { NetworkTransport.Send(m_hostId, item, reliableChannel, buffer, buffer.Length, out error); } } }
//Write the position and rotation to the data structure public virtual void Tick(ref RecordData data) { if (writer == null) { writer = new NetworkWriter(); } else #if ENABLE_MIRROR { writer.Position = 0; } #else { writer.SeekZero(); } #endif writer.Write(transform.position); writer.Write(transform.rotation); #if ENABLE_MIRROR data.bytes = writer.ToArray(); #else data.bytes = writer.AsArray(); #endif }
// Send client position data every so often to those connected IEnumerator SendPositionCoroutine() { NetworkWriter nr = new NetworkWriter(); while (true) { yield return(new WaitForSeconds(0.1f)); // Anything to do? if (clientList.Count == 0) { continue; } // Reset stream nr.SeekZero(); nr.Write((byte)PacketTypeEnum.Position); foreach (var item in clientList) { nr.Write(item.obj.GetInstanceID()); nr.Write(item.obj.transform.position); // Don't pack too much // Fix me! Send more than one packet instead if (nr.Position > 1300) { break; } } // Send data out byte[] buffer = nr.ToArray(); byte error; //Debug.Log(string.Format("Sending data size {0}", buffer.Length)); foreach (var item in clientList) { NetworkTransport.Send(m_hostId, item.connectionId, reliableChannel, buffer, buffer.Length, out error); } } }
internal int CopyPacketToNetworkWriter(ArraySegment <byte> packet, [NotNull] NetworkWriter writer) { if (writer == null) { throw new ArgumentNullException("writer"); } byte[] array = packet.Array; if (array == null) { throw new ArgumentNullException("packet"); } writer.SeekZero(); writer.StartMessage(this.TypeCode); writer.WritePackedUInt32((uint)packet.Count); for (int i = 0; i < packet.Count; i++) { writer.Write(array[packet.Offset + i]); } writer.FinishMessage(); return((int)writer.Position); }
void OnGUI() { if (string.IsNullOrEmpty(Application.cloudProjectId)) { GUILayout.Label("You must set up the project first. See the Multiplayer tab in the Service Window"); } else { GUILayout.Label("Cloud Project ID: " + Application.cloudProjectId); } if (m_MatchJoined) { GUILayout.Label("Match joined '" + m_MatchName + "' on Matchmaker server"); } else if (m_MatchCreated) { GUILayout.Label("Match '" + m_MatchName + "' created on Matchmaker server"); } GUILayout.Label("Connection Established: " + m_ConnectionEstablished); if (m_MatchCreated || m_MatchJoined) { GUILayout.Label("Relay Server: " + m_MatchInfo.address + ":" + m_MatchInfo.port); GUILayout.Label("NetworkID: " + m_MatchInfo.networkId + " NodeID: " + m_MatchInfo.nodeId); GUILayout.BeginHorizontal(); GUILayout.Label("Outgoing message:"); m_NetworkMessage = GUILayout.TextField(m_NetworkMessage); GUILayout.EndHorizontal(); GUILayout.Label("Last incoming message: " + m_LastReceivedMessage); if (m_ConnectionEstablished && GUILayout.Button("Send message")) { m_Writer.SeekZero(); m_Writer.Write(m_NetworkMessage); byte error; for (int i = 0; i < m_ConnectionIds.Count; ++i) { NetworkTransport.Send(m_HostId, m_ConnectionIds[i], 0, m_Writer.AsArray(), m_Writer.Position, out error); if ((NetworkError)error != NetworkError.Ok) { Debug.LogError("Failed to send message: " + (NetworkError)error); } } } if (GUILayout.Button("Shutdown")) { m_NetworkMatch.DropConnection(m_MatchInfo.networkId, m_MatchInfo.nodeId, 0, OnConnectionDropped); } } else { if (GUILayout.Button("Create Room")) { m_NetworkMatch.CreateMatch(m_MatchName, 4, true, "", "", "", 0, 0, OnMatchCreate); } if (GUILayout.Button("Join first found match")) { m_NetworkMatch.ListMatches(0, 1, "", true, 0, 0, (success, info, matches) => { if (success && matches.Count > 0) { m_NetworkMatch.JoinMatch(matches[0].networkId, "", "", "", 0, 0, OnMatchJoined); } }); } if (GUILayout.Button("List rooms")) { m_NetworkMatch.ListMatches(0, 20, "", true, 0, 0, OnMatchList); } if (m_MatchList.Count > 0) { GUILayout.Label("Current rooms:"); } foreach (var match in m_MatchList) { if (GUILayout.Button(match.name)) { m_NetworkMatch.JoinMatch(match.networkId, "", "", "", 0, 0, OnMatchJoined); } } } }
byte[] MsgToByte(MessageBase msg) { _writer.SeekZero(); _writer.Write(msg); return(_writer.ToArray()); }
void SendInputs(Inputs inp, Results res) { #else void SendInputs(Inputs inp) { #endif if (!isLocalPlayer || isServer) { return; } if (inputWriter == null) { inputWriter = new NetworkWriter(); } if (inpSend == null) { inpSend = new InputSend(); } inputWriter.SeekZero(); inputWriter.StartMessage(inputMessage); #if (CLIENT_TRUST) inpRes = new InputResult(); inpRes.inp = inp; inpRes.res = res; inputWriter.Write(inpRes); #else inpSend.inp = inp; inputWriter.Write(inpSend); #endif inputWriter.FinishMessage(); myClient.SendWriter(inputWriter, GetNetworkChannel()); } void OnSendInputs(NetworkMessage msg) { #if (CLIENT_TRUST) inpRes = msg.ReadMessage <InputResult> (); Inputs inp = inpRes.inp; Results res = inpRes.res; #else Inputs inp = msg.ReadMessage <InputSend> ().inp; #endif #if (SIMULATE) #if (CLIENT_TRUST) StartCoroutine(SendInputsC(inp, res)); #else StartCoroutine(SendInputsC(inp)); #endif } #if (CLIENT_TRUST) IEnumerator SendInputsC(Inputs inp, Results res) { #else IEnumerator SendInputsC(Inputs inp) { #endif yield return(new WaitForSeconds(UnityEngine.Random.Range(0.21f, 0.28f))); #endif if (!isLocalPlayer) { if (clientInputs.Count > data.clientInputsBuffer) { clientInputs.RemoveAt(0); } if (!ClientInputsContainTimestamp(inp.timestamp)) { clientInputs.Add(inp); } #if (CLIENT_TRUST) tempResults = res; #endif currentTFixedUpdates += sendUpdates; if (data.debug && lastTick + 1 != inp.timestamp && lastTick != -1) { Debug.Log("Missing tick " + lastTick + 1); } lastTick = inp.timestamp; } } //Results part public override void OnDeserialize(NetworkReader reader, bool initialState)
/// <summary> /// Serializes a standard uNet message /// </summary> /// <param name="opCode"></param> /// <param name="message"></param> /// <returns></returns> public static IMessage Create(short opCode, MessageBase message) { _writer.SeekZero(); message.Serialize(_writer); return(_factory.Create(opCode, _writer.ToArray())); }