Пример #1
0
        public byte[] Serialize()
        {
            UniStreamWriter writer = new UniStreamWriter();

            try {
                writer.WriteShort(Sender);
                writer.WriteShortArray(Recipients);
                writer.WriteString(Tag);
                writer.WriteBytes(Payload);
            }
            catch (Exception e) {
                UnityEngine.Debug.LogError("Packet serialization error : " + e.Message);
            }

            return(writer.Bytes);
        }
Пример #2
0
        void OnDisconnected(NetworkEvent netEvent)
        {
            if (NodeState == State.Client)
            {
                NodeState = State.Uninitialized;
                Deinit();
            }
            else if (NodeState == State.Server)
            {
                OnLeave.TryInvoke(netEvent.ConnectionId);
                var dId = netEvent.ConnectionId;
                ConnectionIds.Remove(netEvent.ConnectionId);

                var payload = new UniStreamWriter().WriteShort(dId.id).Bytes;

                // Clients are not aware of each other as this is a star network
                // Send a reliable reserved message to everyone to announce the disconnection
                Send(Packet.From(CId).With(ReservedTags.ClientLeft, payload), true);
            }
        }
Пример #3
0
        void OnNewConnection(NetworkEvent netEvent)
        {
            ConnectionId newCId = netEvent.ConnectionId;

            ConnectionIds.Add(newCId);

            if (NodeState == State.Uninitialized)
            {
                OnJoin.TryInvoke(newCId);

                // Add server as a connection on the client end
                ConnectionIds.Add(new ConnectionId(0));
                NodeState = State.Client;
            }
            else if (NodeState == State.Server)
            {
                OnJoin.TryInvoke(newCId);
                foreach (var id in ConnectionIds)
                {
                    if (id.id == 0 || id.id == newCId.id)
                    {
                        continue;
                    }

                    byte[] payload;

                    // Announce the new connection to the old ones and vice-versa
                    payload = new UniStreamWriter().WriteShort(newCId.id).Bytes;
                    Send(Packet.From(this).To(id).With(ReservedTags.ClientJoined, payload), true);

                    payload = new UniStreamWriter().WriteShort(id.id).Bytes;
                    Send(Packet.From(this).To(newCId).With(ReservedTags.ClientJoined, payload), true);
                }
            }

            m_ConnectCallback.TryInvoke(newCId);
            m_ConnectCallback = null;
        }