示例#1
0
        private void OnInstantiate(Socket socket, byte[] data)
        {
            C2S_Instantiate clientCmd = NetworkSerializer.Deserialize <C2S_Instantiate>(data);

            //新增对象数据
            instances[clientCmd.TypeId].Add(instanceId, new Entity(clients[socket], clientCmd.X, clientCmd.Y));

            S2C_Instantiate cmd = new S2C_Instantiate();

            cmd.Frame           = clientCmd.Frame;
            cmd.Instance.TypeId = clientCmd.TypeId;
            cmd.Instance.Id     = instanceId;
            cmd.Instance.X      = clientCmd.X;
            cmd.Instance.Y      = clientCmd.Y;
            //自增
            instanceId++;
            //广播所有人
            foreach (var client in clients.Keys)
            {
                if (client == socket) //调用者拥有IsLocal属性
                {
                    cmd.Instance.IsLocal = true;
                }
                else
                {
                    cmd.Instance.IsLocal = false;
                }
                Send(client, (ushort)NetworkRole.Server, (ushort)GameCmd.Instantiate, cmd);
            }
        }
示例#2
0
        public void Register(ushort cmd1, ushort cmd2, CallbackEvent _event)
        {
            int key = NetworkSerializer.EnumToKey(cmd1, cmd2);

            if (events.ContainsKey(key))
            {
                return;
            }
            events.Add(key, _event);
        }
示例#3
0
        private void DestroyCallback(byte[] data)
        {
            S2C_Destroy cmd = NetworkSerializer.Deserialize <S2C_Destroy>(data);
            //获取场景实例
            GameObject instance = instances[cmd.TypeId][cmd.Id];

            //从列表中删除
            instances[cmd.TypeId].Remove(cmd.Id);
            //从场景中删除
            global::Destroy.Object.Destroy(instance);
            //从他人物体中移除
            otherInstances.Remove(cmd.Id);
        }
示例#4
0
        private void JoinCallback(byte[] data)
        {
            S2C_Join join = NetworkSerializer.Deserialize <S2C_Join>(data);

            this.join = true;
            frame     = join.Frame;
            id        = join.YourId;
            if (join.Instances == null) //表示当前没有游戏物体
            {
                return;
            }
            //生成所有游戏物体
            foreach (var instance in join.Instances)
            {
                CreatInstance(instance);
            }
        }
示例#5
0
        private void OnMove(Socket socket, byte[] data)
        {
            C2S_Move move = NetworkSerializer.Deserialize <C2S_Move>(data);

            if (move.Entities == null) //表示该玩家没有创建移动的游戏物体
            {
                return;
            }
            //玩家的实体
            foreach (var entity in move.Entities)
            {
                //获得场景中所有实例
                foreach (var dict in instances.Values)
                {
                    dict[entity.Id] = entity; //赋值
                }
            }
        }
示例#6
0
        private void MoveCallback(byte[] data)
        {
            S2C_Move move = NetworkSerializer.Deserialize <S2C_Move>(data);

            frame = move.Frame;
            if (move.Entities == null) //表示当前没有游戏物体
            {
                return;
            }
            //实现其他物体同步移动
            foreach (var entity in move.Entities)
            {
                //只同步他人
                Vector2Int pos = new Vector2Int(entity.X, entity.Y);
                if (otherInstances.ContainsKey(entity.Id))
                {
                    otherInstances[entity.Id].transform.Position = pos;
                }
            }
        }
示例#7
0
        public void Update()
        {
            //接受消息
            if (client.Available > 0)
            {
                NetworkSerializer.UnpackTCPMessage(client, out ushort cmd1, out ushort cmd2, out byte[] data);
                int key = NetworkSerializer.EnumToKey(cmd1, cmd2);

                if (events.ContainsKey(key))
                {
                    events[key](data);
                }
            }

            //发送消息
            while (messages.Count > 0)
            {
                byte[] data = messages.Dequeue();
                client.Send(data);
            }
        }
示例#8
0
        private void OnDestroy(Socket socket, byte[] data)
        {
            C2S_Destroy clientCmd = NetworkSerializer.Deserialize <C2S_Destroy>(data);

            //移除对象数据
            instances[clientCmd.TypeId].Remove(clientCmd.Id);

            S2C_Destroy cmd = new S2C_Destroy();

            cmd.Frame  = clientCmd.Frame;
            cmd.TypeId = clientCmd.TypeId;
            cmd.Id     = clientCmd.Id;
            //广播除了发送者之外的人
            foreach (var client in clients.Keys)
            {
                if (client == socket)
                {
                    continue;
                }
                Send(client, (ushort)NetworkRole.Server, (ushort)GameCmd.Destroy, cmd);
            }
        }
示例#9
0
        public void Update()
        {
            //接收消息
            if (udp.Available > 0)
            {
                IPEndPoint iPEndPoint = null;
                byte[]     data       = udp.Receive(ref iPEndPoint);

                NetworkSerializer.UnpackUDPMessage(data, out ushort cmd1, out ushort cmd2, out byte[] msgData);
                int key = NetworkSerializer.EnumToKey(cmd1, cmd2);

                if (events.ContainsKey(key))
                {
                    events[key](msgData);
                }
            }

            //发送消息
            while (messages.Count > 0)
            {
                Message message = messages.Dequeue();
                message.Send(udp);
            }
        }
示例#10
0
 public void Send <T>(ushort cmd1, ushort cmd2, T message)
 {
     byte[] data = NetworkSerializer.PackTCPMessage(cmd1, cmd2, message);
     messages.Enqueue(data);
 }
示例#11
0
 public void Send(ushort cmd1, ushort cmd2, byte[] data)
 {
     byte[] packData = NetworkSerializer.PackSimpleTCPMessage(cmd1, cmd2, data);
     messages.Enqueue(packData);
 }
示例#12
0
        public void Update()
        {
            //异步接收客户端
            if (accept)
            {
                try
                {
                    acceptAsync = server.BeginAccept(null, null);
                    accept      = false;
                }
                catch (Exception) { }
            }
            if (acceptAsync.IsCompleted)
            {
                try
                {
                    Socket socket = server.EndAccept(acceptAsync);
                    clients.Add(new Client(true, socket));
                    OnConnected?.Invoke(socket);
                }
                catch (Exception) { }
                finally { accept = true; }
            }

            //异步接收消息
            foreach (Client client in clients)
            {
                if (!client.Connected) //不接受断开连接的消息
                {
                    continue;
                }
                Socket socket = client.Socket;

                if (socket.Available > 0)
                {
                    try
                    {
                        NetworkSerializer.UnpackTCPMessage(socket, out ushort cmd1, out ushort cmd2, out byte[] data);
                        int key = NetworkSerializer.EnumToKey(cmd1, cmd2);

                        if (events.ContainsKey(key))
                        {
                            events[key](socket, data);
                        }
                    }
                    catch (Exception ex)
                    {
                        socket.Close();
                        client.Connected = false;
                        OnDisconnected?.Invoke(ex.Message, socket);
                    }
                }
            }

            //异步发送消息
            while (messages.Count > 0)
            {
                Message message = messages.Dequeue();
                Socket  socket  = message.Socket;

                bool pass = false;
                foreach (Client client in clients)
                {
                    if (client.Socket == socket && client.Connected) //存在该客户端并且该客户端激活
                    {
                        pass = true;
                        break;
                    }
                }
                if (!pass)
                {
                    continue;
                }

                try
                {
                    message.Send();
                }
                catch (Exception ex)
                {
                    socket.Close();
                    //禁用该Socket
                    foreach (Client client in clients)
                    {
                        if (client.Socket == socket)
                        {
                            client.Connected = false;
                        }
                    }
                    OnDisconnected?.Invoke(ex.Message, socket);
                    break;
                }
            }
        }
示例#13
0
 public void Send <T>(Socket client, ushort cmd1, ushort cmd2, T message)
 {
     byte[] data = NetworkSerializer.PackTCPMessage(cmd1, cmd2, message);
     messages.Enqueue(new Message(client, data));
 }
示例#14
0
 public void Send(Socket client, ushort cmd1, ushort cmd2, byte[] data)
 {
     byte[] packData = NetworkSerializer.PackSimpleTCPMessage(cmd1, cmd2, data);
     messages.Enqueue(new Message(client, packData));
 }
示例#15
0
 public void Send <T>(string ip, int port, ushort cmd1, ushort cmd2, T message)
 {
     byte[] data = NetworkSerializer.PackUDPMessage(cmd1, cmd2, message);
     messages.Enqueue(new Message(new IPEndPoint(IPAddress.Parse(ip), port), data));
 }
示例#16
0
        private void OnInstantiated(byte[] data)
        {
            S2C_Instantiate cmd = NetworkSerializer.Deserialize <S2C_Instantiate>(data);

            CreatInstance(cmd.Instance);
        }