void handleOrder(porder po,char* path,int cfd) { // printf("path:%s\n",path); if(ORDER_PWD == po->order) { data tbuf; memset(&tbuf,0,sizeof(tbuf)); recvN(cfd,(char*)&tbuf.len,sizeof(tbuf.len)); recvN(cfd,tbuf.buf,tbuf.len); printf("%s\n",tbuf.buf); } else if(ORDER_LS == po->order) { data tbuf; memset(&tbuf,0,sizeof(tbuf)); recvN(cfd,(char*)&tbuf.len,sizeof(tbuf.len)); recvN(cfd,tbuf.buf,tbuf.len); printf("%-5s%-20s%-10s\n","type","name","size"); printf("%s\n",tbuf.buf); } else if(ORDER_CD == po->order) { data tbuf; memset(&tbuf,0,sizeof(tbuf)); recvN(cfd,(char*)&tbuf.len,sizeof(tbuf.len)); recvN(cfd,tbuf.buf,tbuf.len); printf("%s\n",tbuf.buf); } else if(ORDER_PUTS == po->order) { sendFile(cfd,path,po->info); } else if(ORDER_GETS == po->order) { recvFile(cfd,path,po->info); } else if(ORDER_RM == po->order) { data tbuf; memset(&tbuf,0,sizeof(tbuf)); tbuf.len = strlen(po->info); strcpy(tbuf.buf,po->info); sendN(cfd,(char*)&tbuf,tbuf.len+sizeof(tbuf.len)); memset(&tbuf,0,sizeof(tbuf)); recvN(cfd,(char*)&tbuf.len,sizeof(tbuf.len)); recvN(cfd,tbuf.buf,tbuf.len); printf("%s\n",tbuf.buf); } else if(ORDER_EXIT == po->order) { printf("bye bye!\n"); close(cfd); exit(0); } else { printf("command not found\n"); } }
int handleSingleConnection(int acceptHandle) { int32_t count = 0; int32_t group = 0; int32_t type = 0; int failed = 0; int bytesRecv; int32_t msgSize; memset(inBuffer, 0, BUFF_MAX + 1); memset(outBuffer, 0, BUFF_MAX + 1); bytesRecv = recvN(acceptHandle, inBuffer, 4, 0); if (bytesRecv != 4) { failed = 1; return failed; } msgSize = getInt32(inBuffer, 0); if (msgSize < 8 || msgSize >= (BUFF_MAX - 4)) { printf("tcpSvr: Invalid msgSize\n"); failed = 1; return failed; } bytesRecv = recvN(acceptHandle, inBuffer + 4, (int) msgSize, 0); if (bytesRecv != msgSize) { failed = 1; return failed; } group = getInt32(inBuffer, 4); type = getInt32(inBuffer, 8); count++; switch (group) { case MOT_FUNCTION_GROUP: failed = handleMotFunctionRequest(acceptHandle, inBuffer, outBuffer, type, msgSize); break; case SYS1_FUNCTION_GROUP: failed = handleSys1FunctionRequest(acceptHandle, inBuffer, outBuffer, type, msgSize); break; case FILE_CTRL_FUNCTION_GROUP: failed = handleFileFunctionRequest(acceptHandle, inBuffer, outBuffer, type, msgSize); break; case EX_FILE_CTRL_FUNCTION_GROUP: failed = handleExFileFunctionRequest(acceptHandle, inBuffer, outBuffer, type, msgSize); break; default: fprintf(stderr, "tcpSvr: unrecognized group =%d\n", group); failed = 1; break; } return failed; /* printf("tcpSvr: msgSize=%d\n", msgSize); printf("tcpSvr: group=%d\n", group); printf("tcpSvr: type=%d\n", type); printf("tcpSvr: count=%d\n", count); printf("tcpSvr: Closing acceptHandle=%d\n", acceptHandle); mpClose(acceptHandle); free(inBuffer); free(outBuffer); */ }