Exemple #1
0
    private void Receive(int node, byte[] data)
    {
        PacketHeader           header           = new PacketHeader();
        PacketHeaderSerializer headerSerializer = new PacketHeaderSerializer();

        bool ret = headerSerializer.Deserialize(data, ref header);

        if (ret == false)
        {
            // Skip invalid header packet
            return;
        }

        int packetType = header.type;

        if (packetHandlers.ContainsKey(packetType) &&
            packetHandlers[packetType] != null)
        {
            int    headerSize = Marshal.SizeOf(typeof(PacketHeader));
            byte[] packetData = new byte[data.Length - headerSize];
            Buffer.BlockCopy(data, headerSize, packetData, 0, packetData.Length);

            packetHandlers[packetType](node, header.type, packetData);
        }
    }
Exemple #2
0
    public int SendUdp(int node, IPacket packet)
    {
        int sendSize = 0;

        if (sessionUdp == null)
        {
            return(sendSize);
        }

        PacketHeader           header           = new PacketHeader();
        PacketHeaderSerializer headerSerializer = new PacketHeaderSerializer();

        header.type = packet.GetPacketType();

        byte[] headerData = null;
        if (headerSerializer.Serialize(header) == true)
        {
            headerData = headerSerializer.GetSerializedData();
        }

        byte[] packetData = packet.GetData();

        byte[] data = new byte[headerData.Length + packetData.Length];

        int headerSize = Marshal.SizeOf(typeof(PacketHeader));

        Buffer.BlockCopy(headerData, 0, data, 0, headerSize);
        Buffer.BlockCopy(packetData, 0, data, headerSize, packetData.Length);

        sendSize = sessionUdp.Send(node, data, data.Length);

        if (sendSize < 0 && nodeStateHandler != null)
        {
            NetworkState state = new NetworkState();
            state.node   = node;
            state.type   = NetEventType.SendError;
            state.result = NetEventResult.Failure;
            nodeStateHandler.Invoke(ConnectionType.UDP, state);
        }

        return(sendSize);
    }