int ReturnResponse(int fd, struct ReqInfo* reqInfo) { char c; int res_fd; int flag; if (reqInfo->pageType == DYNAMIC) { servDynamic(fd, reqInfo); return 0; } /* */ res_fd = CheckResource(reqInfo); if (reqInfo->status == 200 && res_fd < 0) { if(errno == EACCES) /* Pemission denied */ reqInfo->status = 401; else /* Not Found */ reqInfo->status = 404; } if (reqInfo->type == FULL) OutputHttpHeaders(fd, reqInfo); if (reqInfo->status == 200) { flag = ReturnResource(fd, res_fd, reqInfo); if(flag == -1){ perror("Return Resouce fail !\n"); return -1; } } else ReturnErrorMsg(fd, reqInfo); if (res_fd > 0) close(res_fd); if (reqInfo != NULL) { free(reqInfo); reqInfo = NULL; } // FreeReqInfo(reqInfo); /* char content[] = "<head><head><title>index.html</title></head><body>hello world!</body>"; char response[512]; sprintf(response, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s", strlen(content), content); flag = write(fd, response, sizeof(response)); if(flag < 1){ perror("write fail !\n"); } */ return 0; }
void HandleCommand() { char* token; int unitID; CUnit* unit; int actionID; int resourceType; int x, y; char* typeID; CUnitType* type; int destID; CUnit* dest; token = NextToken(); if (!token) return; unitID = atoi(token); unit = GetUnitByID(unitID); if (!unit) { SendResponseMessage("Unknown unit.\n", 3); return; } token = NextToken(); if (!token) return; actionID = atoi(token); switch (actionID) { case 0: // NOOP // SendResponseMessage("OK\n", 3); break; case 1: // STOP SendCommandStopUnit(unit); // SendResponseMessage("OK\n", 3); break; case 2: // MoveTo <X> <Y> token = NextToken(); if (token == NULL) { SendResponseMessage("Bad arguments to Move Action.\n", 3); return; } x = atoi(token); token = NextToken(); if (token == NULL) { SendResponseMessage("Bad arguments to Move Action.\n", 3); return; } y = atoi(token); SendCommandMove(unit, x, y, 1); // SendResponseMessage("OK\n", 3); break; case 3: // Build <Type> <X> <Y> token = NextToken(); if (token == NULL) { SendResponseMessage("Bad arguments to Build Action.\n", 3); return; } typeID = token; type = UnitTypeByIdent(typeID); if (type == NULL) { SendResponseMessage("Unknown type to build.\n", 3); return; } // If it's a building, train the unit. if (unit->Type->Building) SendCommandTrainUnit(unit, type, 1); else // otherwise, build a building. { token = NextToken(); if (token == NULL) { SendResponseMessage("Bad arguments to Build Action.\n", 3); return; } x = atoi(token); token = NextToken(); if (token == NULL) { SendResponseMessage("Bad arguments to Build Action.\n", 3); return; } y = atoi(token); SendCommandBuildBuilding(unit, x, y, type, 1); } SendResponseMessage("OK\n", 3); break; case 4: // Attack <UnitID> token = NextToken(); if (!token) return; destID = atoi(token); dest = GetUnitByID(destID); if (!dest) { SendResponseMessage("Unknown unit to attack.\n", 3); return; } SendCommandAttack(unit, 0, 0, dest, 1); SendResponseMessage("OK\n", 3); break; case 5: // Repair <UnitID> token = NextToken(); if (!token) return; destID = atoi(token); dest = GetUnitByID(destID); if (!dest) { SendResponseMessage("Unknown unit to repair.\n", 3); return; } SendCommandRepair(unit, 0, 0, dest, 1); SendResponseMessage("OK\n", 3); break; case 6: // Harvest <ResourceType> token = NextToken(); if (!token) return; resourceType = atoi(token); // GOLD = 1, WOOD = 2 FindAndGatherResource(unit, resourceType); break; case 7: // Return resource ReturnResource(unit); break; case 8: // Harvest unit (gold/oil) <DestUnit> token = NextToken(); if (!token) { SendResponseMessage("Invalid harvest argument.\n", 3); return; } destID = atoi(token); dest = GetUnitByID(destID); if (dest == NoUnitP) { SendResponseMessage("Invalid resource unit ID.\n", 3); return; } SendCommandResource(unit, dest, 1); SendResponseMessage("OK\n", 3); break; case 9: // Harvest terrain (wood) <X> <Y> token = NextToken(); if (!token) { SendResponseMessage("Invalid harvest argument.\n", 3); return; } x = atoi(token); token = NextToken(); if (!token) { SendResponseMessage("Invalid harvest argument.\n", 3); return; } y = atoi(token); SendCommandResourceLoc(unit, x, y, 1); SendResponseMessage("OK\n", 3); break; case 10: // Harvest location <ResourceType> <X> <Y> token = NextToken(); if (!token) { SendResponseMessage("Invalid harvest argument.\n", 3); return; } resourceType = atoi(token); // GOLD = 1, WOOD = 2 token = NextToken(); if (!token) { SendResponseMessage("Invalid harvest argument.\n", 3); return; } x = atoi(token); token = NextToken(); if (!token) { SendResponseMessage("Invalid harvest argument.\n", 3); return; } y = atoi(token); HarvestResAtLoc(resourceType, x, y); break; default: SendResponseMessage("Unknown command.\n", 3); return; } }