Exemplo n.º 1
0
    public static byte[] req_encode(ref Request request)
    {
        /* encode header */
        byte[] htype = BitConverter.GetBytes((int)request.header.req_type);
        byte[] hsize = BitConverter.GetBytes(request.header.size);
        byte[] hkey  = StringToByteArray(request.header.key, Protocol.HEADER_KEY_LENGTH);

        int oft = htype.Length + hsize.Length + HEADER_KEY_LENGTH;

        byte[] header = new byte[oft];
        System.Buffer.BlockCopy(htype, 0, header, 0, htype.Length);
        System.Buffer.BlockCopy(hkey, 0, header, htype.Length + hsize.Length, HEADER_KEY_LENGTH);

        /* encode data */
        if (request.header.req_type == reqType.req_cnt)
        {
            RData_Connect data   = (RData_Connect)Marshal.PtrToStructure(request.data, typeof(RData_Connect));
            byte[]        rtype  = BitConverter.GetBytes((int)data.conn_type);
            byte[]        rname  = StringToByteArray(data.name, Protocol.NICKNAME_LENGTH);
            byte[]        rpsswd = StringToByteArray(data.name, Protocol.PASSWORD_LENGTH);

            byte[] content = new byte[oft + rtype.Length + NICKNAME_LENGTH + NICKNAME_LENGTH];
            System.Buffer.BlockCopy(header, 0, content, 0, oft);
            System.Buffer.BlockCopy(rtype, 0, content, oft, rtype.Length);
            System.Buffer.BlockCopy(rname, 0, content, oft + rtype.Length, rname.Length);
            System.Buffer.BlockCopy(rpsswd, 0, content, oft + rtype.Length + rname.Length, rpsswd.Length);


            request.header.size = (ulong)(rtype.Length + rname.Length + rpsswd.Length);

            hsize = BitConverter.GetBytes(request.header.size);
            System.Buffer.BlockCopy(hsize, 0, content, htype.Length, hsize.Length);

            return(content);
        }
        else if (request.header.req_type == reqType.req_snd)
        {
            RData_File data  = (RData_File)Marshal.PtrToStructure(request.data, typeof(RData_File));
            byte[]     rid   = BitConverter.GetBytes(data.id);
            byte[]     rtype = BitConverter.GetBytes((int)data.file_type);
            byte[]     rsize = BitConverter.GetBytes(data.size);
            byte[]     rdata = StringToByteArray(data.data, (int)data.size);

            byte[] content = new byte[oft + rid.Length + rtype.Length + rsize.Length + rdata.Length];
            System.Buffer.BlockCopy(header, 0, content, 0, oft);
            System.Buffer.BlockCopy(rid, 0, content, oft, rid.Length);
            System.Buffer.BlockCopy(rtype, 0, content, oft + rid.Length, rtype.Length);
            System.Buffer.BlockCopy(rsize, 0, content, oft + rid.Length + rtype.Length, rsize.Length);
            System.Buffer.BlockCopy(rdata, 0, content, oft + rid.Length + rtype.Length + rsize.Length, rdata.Length);

            request.header.size = (ulong)(rid.Length + rtype.Length + rsize.Length + rdata.Length);
            hsize = BitConverter.GetBytes(request.header.size);
            System.Buffer.BlockCopy(hsize, 0, content, htype.Length, hsize.Length);

            return(content);
        }
        else if (request.header.req_type == reqType.req_rcv)
        {
            request.header.size = (ulong)0;
            hsize = BitConverter.GetBytes(request.header.size);
            System.Buffer.BlockCopy(hsize, 0, header, htype.Length, hsize.Length);

            return(header);
        }
        // else if (request.header.req_type == reqType.req_res)
        return(new byte[0]);
    }