コード例 #1
0
ファイル: Program.cs プロジェクト: VokunGahrotLaas/CsNet
        protected override void OnReceive(ServerClient client)
        {
            PacketString packet = client.Receive <PacketString>();

            packet.Read(out string message);
            Console.WriteLine($"Server: {client.Address}:{client.Port} sent '{message}'");
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: VokunGahrotLaas/CsNet
        protected override void OnReceive()
        {
            PacketString packet = Receive <PacketString>();

            packet.Read(out string message);
            Console.WriteLine($"Client: received '{message}'");
        }
コード例 #3
0
    private void ExitClient(DataClient client)
    {
        PacketString packet = new PacketString();

        packet.key   = "other ex";
        packet.value = client.id;
        SendBroad(packet);

        Console.WriteLine(client.socket.RemoteEndPoint + " disconnect");
        client.socket.Close();
        m_clientList.Remove(client);
    }
コード例 #4
0
    public static void Process(Queue <Packet> queue, Client client)
    {
        while (queue.Count > 0)
        {
            Packet item = queue.Dequeue();

            if (item is PacketMessage)
            {
                PacketMessage packet = (PacketMessage)item;
            }
            if (item is PacketString)
            {
                PacketString packet = (PacketString)item;
                if (packet.key == "your id")
                {
                    Control con = (Control)PoolingManage.Pooling.Create("Control", Vector3.zero);
                    client.SetID(packet.value);
                    con.id = client.ID;
                }
                if (packet.key == "other id")
                {
                    if (packet.value == client.ID || GlobalData.Instance.m_list.Find(rhs => rhs.id == packet.value) != null)
                    {
                        continue;
                    }

                    Control con = (Control)PoolingManage.Pooling.Create("Control", Vector3.zero);
                    con.id = packet.value;
                    GlobalData.Instance.m_list.Add(con);
                }
                if (packet.key == "other ex")
                {
                    Control con = GlobalData.Instance.m_list.Find(rhs => rhs.id == packet.value);
                    GlobalData.Instance.m_list.Remove(con);
                    con.Remove();
                }
                if (packet.key == "ping")
                {
                    UIPing[] ping = PoolingManage.UIHud.GetList <UIPing>();
                    if (ping.Length > 0)
                    {
                        ping[0].Check();
                    }
                    Client.Instance.Send(packet);
                }
            }
            if (item is PacketTransform)
            {
                PacketTransform packet = (PacketTransform)item;
                if (packet.id == client.ID)
                {
                    continue;
                }

                Control con = GlobalData.Instance.m_list.Find(rhs => rhs.id == packet.id);
                if (con != null)
                {
                    con.transform.position    = packet.position;
                    con.transform.eulerAngles = packet.rotation;
                }
            }
            if (item is PacketMove)
            {
                PacketMove packet = (PacketMove)item;
                if (packet.id == client.ID)
                {
                    continue;
                }
                Control con = GlobalData.Instance.m_list.Find(rhs => rhs.id == packet.id);
                if (con != null)
                {
                    con.BeforeForward = packet.forward;
                }
            }
        }
    }
コード例 #5
0
    private void Receive(DataClient client)
    {
        Console.WriteLine(client.id + " connect");

        client.stream = new NetworkStream(client.socket);

        {
            PacketString packet = new PacketString();
            packet.key   = "your id";
            packet.value = client.id;
            Send(packet, client.id);
        }
        {
            PacketString packet = new PacketString();
            packet.key = "ping";
            Send(packet, client.id);
        }

        for (int i = 0; i < m_clientList.Count; ++i)
        {
            PacketString packet = new PacketString();
            packet.key   = "other id";
            packet.value = m_clientList[i].id;
            SendBroad(packet);
        }

        byte[] buff = new byte[BUFF_SIZ];

        while (true)
        {
            try
            {
                int length = client.stream.Read(buff, 0, buff.Length);
                if (length <= 0)
                {
                    ExitClient(client);
                    break;
                }

                int begin = 0;

                while (begin < length)
                {
                    Packet pac = PacketManager.GetPacket(PacketManager.GetPacketType(buff, begin), buff, begin);
                    begin += Marshal.SizeOf(pac);

                    if (pac is PacketTransform)
                    {
                        PacketTransform packet = (PacketTransform)pac;
                        SendBroad(packet, client);
                    }
                    if (pac is PacketMessage)
                    {
                        PacketMessage packet = (PacketMessage)pac;
                        Console.WriteLine(packet.data);
                    }
                    if (pac is PacketString)
                    {
                        PacketString packet = (PacketString)pac;
                        if (packet.key == "ping")
                        {
                            Send(packet, client.id);
                        }
                    }
                    if (pac is PacketMove)
                    {
                        PacketMove packet = (PacketMove)pac;
                        SendBroad(packet);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                ExitClient(client);
                break;
            }
        }
    }