public override void SendUpdate()
        {
            ProfilerShort.Begin("ClientState.WriteAcks");

            // Client ACK Packet - reliable
            SendStream.ResetWrite();

            // ACK Header
            // Write last state sync packet id
            SendStream.WriteByte(m_lastStateSyncPacketId);

            // Write ACKs
            byte num = (byte)m_acks.Count;

            SendStream.WriteByte(num);
            for (int i = 0; i < num; i++)
            {
                SendStream.WriteByte(m_acks[i]);
            }
            SendStream.Terminate();
            m_acks.Clear();
            ProfilerShort.End();
            m_callback.SendClientAcks(SendStream);

            // Client Update Packet
            SendStream.ResetWrite();

            m_clientPacketId++;
            SendStream.WriteByte(m_clientPacketId);
            SendStream.WriteDouble(MyTimeSpan.FromTicks(Stopwatch.GetTimestamp()).Milliseconds);
            ProfilerShort.Begin("ClientState.Serialize");
            if (VRage.MyCompilationSymbols.EnableNetworkPacketTracking)
            {
                Trace.MyTrace.Send(Trace.TraceWindow.MPackets, "Send client update: ");
            }
            // Write Client state
            ClientState.Serialize(SendStream, false);
            ProfilerShort.End();
            SendStream.Terminate();

            ProfilerShort.Begin("SendClientUpdate");
            m_callback.SendClientUpdate(SendStream);
            ProfilerShort.End();

            //Client.SendMessageToServer(m_sendStream, PacketReliabilityEnum.UNRELIABLE, PacketPriorityEnum.IMMEDIATE_PRIORITY, MyChannelEnum.StateDataSync);
        }
 public void SendClientConnected(ref ConnectedClientDataMsg msg)
 {
     SendStream.ResetWrite();
     VRage.Serialization.MySerializer.Write <ConnectedClientDataMsg>(SendStream, ref msg);
     m_callback.SendConnectRequest(SendStream);
 }
        void SetReplicableReady(NetworkId networkId, IMyReplicable replicable, bool loaded)
        {
            try
            {
                MyPendingReplicable pendingReplicable;
                if (m_pendingReplicables.TryGetValue(networkId, out pendingReplicable))
                {
                    m_pendingReplicables.Remove(networkId);

                    if (loaded)
                    {
                        var ids = pendingReplicable.StateGroupIds;

                        AddNetworkObjectClient(networkId, replicable);

                        using (m_tmpGroups)
                        {
                            IMyStreamableReplicable streamable = replicable as IMyStreamableReplicable;
                            if (streamable != null && pendingReplicable.IsStreaming)
                            {
                                var group = streamable.GetStreamingStateGroup();
                                m_tmpGroups.Add(group);
                            }

                            replicable.GetStateGroups(m_tmpGroups);
                            Debug.Assert(ids.Count == m_tmpGroups.Count,
                                         "Number of state groups on client and server for replicable does not match");
                            for (int i = 0; i < m_tmpGroups.Count; i++)
                            {
                                if (m_tmpGroups[i] != replicable && m_tmpGroups[i].GroupType != StateGroupEnum.Streaming)
                                {
                                    AddNetworkObjectClient(ids[i], m_tmpGroups[i]);
                                }
                            }
                        }
                        m_eventBuffer.ProcessEvents(networkId, m_eventHandler, m_isBlockedHandler, NetworkId.Invalid);
                    }
                    else
                    {
                        MyLog.Default.WriteLine("Failed to create replicable ! Type : " + replicable.ToString());
                        m_eventBuffer.RemoveEvents(networkId);

                        IMyStreamableReplicable streamable = replicable as IMyStreamableReplicable;
                        if (streamable != null && pendingReplicable.IsStreaming)
                        {
                            var group = streamable.GetStreamingStateGroup();
                            group.Destroy();
                            NetworkId streaingGroupId;
                            if (TryGetNetworkIdByObject(group, out streaingGroupId))
                            {
                                RemoveNetworkedObject(group);
                            }
                            MyLog.Default.WriteLine("removing streaming group for not loaded replicable !");
                        }
                    }

                    SendStream.ResetWrite();
                    SendStream.WriteNetworkId(networkId);
                    SendStream.WriteBool(loaded);
                    SendStream.Terminate();
                    m_callback.SendReplicableReady(SendStream);
                }
                else
                {
                    m_pendingReplicables.Remove(networkId);
                    using (m_tmpGroups)
                    {
                        IMyStreamableReplicable streamable = replicable as IMyStreamableReplicable;
                        if (streamable != null && streamable.NeedsToBeStreamed)
                        {
                            var group = streamable.GetStreamingStateGroup();
                            m_tmpGroups.Add(group);
                            MyLog.Default.WriteLine("removing streaming group for not loaded replicable !");
                        }

                        replicable.GetStateGroups(m_tmpGroups);
                        foreach (var g in m_tmpGroups)
                        {
                            if (g != null)
                            // when terminal repblicable fails to attach to block its state group is null becouase its created inside hook method.
                            {
                                g.Destroy();
                            }
                        }
                    }
                    replicable.OnDestroy();
                }
            }
            catch (Exception ex)
            {
                MyLog.Default.WriteLine(ex);
                throw;
                //m_callback.DisconnectFromHost();
            }
        }