FileClient文件夹中文件:
FileClient.h:
#ifndef __FILE_CLIENT_H__
#define __FILE_CLIENT_H__
#include <map>
#include <string>
using std::map;
using std::string;
#include "../TMClient/TMClient.h"
class FileClient : public TMClient
{
public:
FileClient(const char * host, const char * serv);
~FileClient();
protected:
virtual bool handlecmd(char * data);
protected:
bool send(const char * data, int n);
bool recv(char * buff, int n);
void showusage();
private:
bool handle_ls_res();
bool handle_cd_res();
bool handle_mkdir_res();
bool handle_touch_res();
bool handle_rm_res();
bool handle_download_res();
bool handle_upload_res(char * data);
private:
bool handle_normal_res();
bool upsend(unsigned char state, char * buff, unsigned short uslen);
int upfile(char * filename, char * buff, unsigned short uslen);
int updir(char * pathname, char * buff, unsigned short uslen);
protected:
map<string, unsigned char> m_cmd;
};
#endif
FileClient.cpp:
#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <utility>
using std::pair;
#include "../ByteStream/ByteStream.h"
#include "../Head/Command.h"
#include "FileClient.h"
FileClient::FileClient(const char * host, const char * serv)
: TMClient(host, serv)
{
m_cmd.insert(pair<string, unsigned char>("ls", ls));
m_cmd.insert(pair<string, unsigned char>("cd", cd));
m_cmd.insert(pair<string, unsigned char>("mkdir", mk));
m_cmd.insert(pair<string, unsigned char>("touch", touch));
m_cmd.insert(pair<string, unsigned char>("rm", rm));
m_cmd.insert(pair<string, unsigned char>("download", download));
m_cmd.insert(pair<string, unsigned char>("upload", upload));
}
FileClient::~FileClient()
{
}
bool FileClient::handlecmd(char * data)
{
unsigned short uslen = strlen(data);
int fws;
for (fws = 0; fws < uslen; ++fws) {
if (' ' != data[fws]) {
break;
}
}
if (fws >= uslen) {
showusage();
return(true);
}
int fwe;
for (fwe = fws + 1; fwe < uslen; ++fwe) {
if (' ' == data[fwe]) {
break;
}
}
data[fwe] = '\0';
int lws;
for (lws = fwe + 1; lws < uslen; ++lws) {
if (' ' != data[lws]) {
break;
}
}
int lwe;
for (lwe = uslen - 1; lwe > lws; --lwe) {
if (' ' != data[lwe]) {
break;
}
}
data[lwe + 1] = '\0';
map<string, unsigned char>::iterator iter = m_cmd.find(data + fws);
if (m_cmd.end() == iter) {
showusage();