Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yodalee
Last active December 27, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yodalee/7248136 to your computer and use it in GitHub Desktop.
Save yodalee/7248136 to your computer and use it in GitHub Desktop.
分享一下我寫的內容,是沒講得太清楚啦lol
//先解釋一下助教寫的login流程
//csiebox_server.c:login()
info->conn_fd = conn_fd;
server->client[conn_fd] = info; //有關這個client的資料都存在這裡,例如家目錄
header.res.status = CSIEBOX_PROTOCOL_STATUS_OK;
header.res.client_id = info->conn_fd; //把client_id送給client
//........
send_message(conn_fd, &header, sizeof(header)); //送出,反正送用send_message,等資料用recv_message
//csiebox_client.c:login();
if (recv_message(client->conn_fd, &header, sizeof(header))) { 收訊息
if (header.res.magic == CSIEBOX_PROTOCOL_MAGIC_RES &&
header.res.op == CSIEBOX_PROTOCOL_OP_LOGIN &&
header.res.status == CSIEBOX_PROTOCOL_STATUS_OK) {
client->client_id = header.res.client_id; //把server送來的client id記下來
}
}
//下面是我寫sendmeta的部分:
csiebox_protocol_meta req;
memset(&req, 0, sizeof(req));
req.message.header.req.magic = CSIEBOX_PROTOCOL_MAGIC_REQ;
req.message.header.req.op = CSIEBOX_PROTOCOL_OP_SYNC_META;
req.message.header.req.client_id = client->client_id; //用這個讓server知道「安安先生你哪位?」
req.message.header.req.datalen = sizeof(req) - sizeof(req.message.header);
req.message.body.pathlen = strlen(syncfile); //filename length
send_message(client->conn_fd, &req, sizeof(req)) //送header,!!!senddata1!!!
send_message(client->conn_fd, (void*)syncfile, strlen(syncfile)) //送檔名, !!!senddata2!!!
//相對的server接收到!!!senddata1!!!後,會在handle_request中,跳到case CSIEBOX_PROTOCOL_OP_SYNC_META的部分:
//所以就可以進到
csiebox_client_info* info =
(csiebox_client_info*)malloc(sizeof(csiebox_client_info));
memset(info, 0, sizeof(csiebox_client_info));
int length = meta->message.body.pathlen;
int client_id = meta->message.header.req.client_id;
info = server->client[client_id]; //有了client id就可以取得記錄的使用者資料
recv_message(conn_fd, filepath, length); //知道filename length,就等client 傳送!!!senddata2!!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment