FTP文件管理项目 项目日报(五)

FTP文件管理项目 项目日报(五)

上次日报少写一篇,补到这次。

进度

今日计划 实际完成
登录,注册,找回密码,修改密码的发包和解包 登录发包解包,注册,找回密码发包

问题

暂无

解决方案

暂无

技术点

不定长包的打包和解包

//包体基类
class PacketBase
{
public:
    PacketBase();
    ~PacketBase();
    packet_header_t *gethead;
    packet_tali_t *gettail;
    bool pack();       //打包
    bool unpack();      //解包
    char *getBody();    //返回Body
    char *getData();    //返回Data
    int getSize();      //返回包体长度 m_Size
    void setSize(int size);     //设置包体长度
    PacketBase& operator = (const PacketBase &packet);

protected:
    char m_Data[MAX_PACKET_LENTH];
    int m_Size;
    packet_header_t Head;
    packet_tali_t Tail;
    char *Body;
};

//包体基类构造函数
PacketBase::PacketBase()
{
    memset(&Head, 0, sizeof(Head));
    memset(&Tail, 0, sizeof(Tail));
}

//打包函数
bool PacketBase::pack()
{
    memcpy(m_Data, &this->Head, sizeof(packet_header_t));
    memcpy(m_Data + sizeof(packet_header_t), this->Body, sizeof(Body)); //这个Body长度在封包的时候定
    memcpy(m_Data + sizeof(packet_header_t) + sizeof(Body), &this->Tail, sizeof(packet_tali_t));
    return true;
}

//解包函数
bool PacketBase::unpack()
{
    Body = new char[m_Size];    //防止越界可以在这里下手
    memcpy(&this->Head, m_Data, sizeof(packet_header_t));
    memcpy(Body, m_Data + sizeof(packet_header_t), sizeof(Body));
    memcpy(&this->Tail, m_Data + sizeof(packet_header_t) + sizeof(Body), sizeof(packet_tali_t));
    return true;
}

//获取(保护属性的包体数据)接口函数
char * PacketBase::getBody()
{
    return Body;
}

//获取数据接口函数
char * PacketBase::getData()
{
    return m_Data;
}

//获取包体长度接口函数
int PacketBase::getSize()
{
    return m_Size;
}

//设置包体长度接口函数
void PacketBase::setSize(int size)
{
    this->m_Size = size;
}

PacketBase & PacketBase::operator =(const PacketBase &packet)
{
    memset(m_Data, 0, MAX_PACKET_LENTH);

    if (m_Size != packet.m_Size)
    {
        m_Size = packet.m_Size;
    }
    memcpy(m_Data, packet.m_Data, packet.m_Size);
    return *this;
}

//(发)打包类(集成父类:打包基类)
class SendPack : public PacketBase
{
public:
    SendPack();
    bool send_loginpack(int id,int pwd);
    bool send_registpack(int id,int pwd,char *phone);
    bool send_findpack(int id,char *phone);

private:
};

//发送登录包
bool SendPack::send_loginpack(int id, int pwd)
{
    int sz = sizeof(Login_t);
    this->setSize(sz);
    this->Body = new char[sz];

    //包头赋值
    Head.funcId = 0x01;
    Head.optid = 0x00;
    Head.syn = 0x04;
    //填充包体
    Login_t *body = (Login_t *)Body;

    body->id=id;
    body->pwd=pwd;

    qDebug()<<"打包的id是"<id;
    qDebug()<<"打包的pwd是"<pwd;

    //包尾
    Tail.pack_tail=0x05;

    return this->pack();
}

//发送注册包
bool SendPack::send_registpack(int id, int pwd, char *phone)
{
    int sz = sizeof(Register_t);
    this->setSize(sz);
    this->Body = new char[sz];

    //包头赋值
    Head.funcId = 0x02;
    Head.optid = 0x00;
    Head.syn = 0x04;

    Register_t *body = (Register_t *)Body;

    body->id = id;
    body->pwd = pwd;
    strcpy(body->phone,phone);

    qDebug()<<"打包的id是"<id;
    qDebug()<<"打包的pwd是"<pwd;
    qDebug()<<"打包的电话是"<phone;

    //包尾
    Tail.pack_tail=0x05;

    return this->pack();
}

//发送找回密码包
bool SendPack::send_findpack(int id, char *phone)
{
    int sz = sizeof(F_Pwd_t);
    this->setSize(sz);
    this->Body = new char[sz];

    //包头赋值
    Head.funcId = 0x03;
    Head.optid = 0x00;
    Head.syn = 0x04;

    F_Pwd_t *body = (F_Pwd_t*)Body;

    body->id = id;
    strcpy(body->phone,phone);

    //包尾
    Tail.pack_tail=0x05;

    return this->pack();
}

你可能感兴趣的:(FTP文件管理项目(本地云),Qt)