| 496 } |
496 } |
| 497 } |
497 } |
| 498 |
498 |
| 499 static NetMove hton_move(const Move *move) { |
499 static NetMove hton_move(const Move *move) { |
| 500 return (NetMove) { |
500 return (NetMove) { |
| 501 .mtime_sec = htonl(move->movetime / 1000000ull), |
501 .mtime_sec = net_htonl(move->movetime / 1000000ull), |
| 502 .mtime_usec = htonl(move->movetime % 1000000ull), |
502 .mtime_usec = net_htonl(move->movetime % 1000000ull), |
| 503 .piece = move->piece, |
503 .piece = move->piece, |
| 504 .fromfile = move->fromfile, .fromrow = move->fromrow, |
504 .fromfile = move->fromfile, .fromrow = move->fromrow, |
| 505 .tofile = move->tofile, .torow = move->torow, |
505 .tofile = move->tofile, .torow = move->torow, |
| 506 .promotion = move->promotion |
506 .promotion = move->promotion |
| 507 }; |
507 }; |
| 509 |
509 |
| 510 static Move ntoh_move(const NetMove *move) { |
510 static Move ntoh_move(const NetMove *move) { |
| 511 return (Move) { |
511 return (Move) { |
| 512 .string = {0}, /* calculated before applying the move */ |
512 .string = {0}, /* calculated before applying the move */ |
| 513 .timestamp = {0}, /* planned to be removed */ |
513 .timestamp = {0}, /* planned to be removed */ |
| 514 .movetime = (uint64_t)move->mtime_sec * 1000000ull + move->mtime_usec, |
514 .movetime = (uint64_t)net_ntohl(move->mtime_sec) |
| |
515 * 1000000ull + net_ntohl(move->mtime_usec), |
| 515 .piece = move->piece, |
516 .piece = move->piece, |
| 516 .fromfile = move->fromfile, .fromrow = move->fromrow, |
517 .fromfile = move->fromfile, .fromrow = move->fromrow, |
| 517 .tofile = move->tofile, .torow = move->torow, |
518 .tofile = move->tofile, .torow = move->torow, |
| 518 .promotion = move->promotion, |
519 .promotion = move->promotion, |
| 519 /* flags are also calculated before applying the move */ |
520 /* flags are also calculated before applying the move */ |
| 644 gamestate->checkmate = code == NETCODE_CHECKMATE; |
645 gamestate->checkmate = code == NETCODE_CHECKMATE; |
| 645 gamestate->stalemate = code == NETCODE_STALEMATE; |
646 gamestate->stalemate = code == NETCODE_STALEMATE; |
| 646 if (code == NETCODE_DECLINE_MOVE) { |
647 if (code == NETCODE_DECLINE_MOVE) { |
| 647 uint32_t reason; |
648 uint32_t reason; |
| 648 net_recieve_data(opponent, &reason, sizeof(uint32_t)); |
649 net_recieve_data(opponent, &reason, sizeof(uint32_t)); |
| 649 reason = ntohl(reason); |
650 reason = net_ntohl(reason); |
| 650 eval_move_failed_msg(reason); |
651 eval_move_failed_msg(reason); |
| 651 } else if (code == NETCODE_ACCEPT |
652 } else if (code == NETCODE_ACCEPT |
| 652 || code == NETCODE_CHECK |
653 || code == NETCODE_CHECK |
| 653 || code == NETCODE_CHECKMATE |
654 || code == NETCODE_CHECKMATE |
| 654 || code == NETCODE_STALEMATE |
655 || code == NETCODE_STALEMATE |
| 1079 uint16_t mc = gamestate->movecount; |
1080 uint16_t mc = gamestate->movecount; |
| 1080 size_t pgndata_size = sizeof(GameInfo) + 2 + mc * NETMOVE_LEN; |
1081 size_t pgndata_size = sizeof(GameInfo) + 2 + mc * NETMOVE_LEN; |
| 1081 char *pgndata = malloc(pgndata_size); |
1082 char *pgndata = malloc(pgndata_size); |
| 1082 memcpy(pgndata, &(gamestate->info), sizeof(GameInfo)); |
1083 memcpy(pgndata, &(gamestate->info), sizeof(GameInfo)); |
| 1083 unsigned offset = sizeof(GameInfo); |
1084 unsigned offset = sizeof(GameInfo); |
| 1084 uint16_t mcn = htons(mc); |
1085 uint16_t mcn = net_htons(mc); |
| 1085 memcpy(pgndata+offset, &mcn, 2); |
1086 memcpy(pgndata+offset, &mcn, 2); |
| 1086 offset += 2; |
1087 offset += 2; |
| 1087 for (unsigned i = 0 ; i < mc ; i++) { |
1088 for (unsigned i = 0 ; i < mc ; i++) { |
| 1088 NetMove nmove = hton_move(gamestate->moves + i); |
1089 NetMove nmove = hton_move(gamestate->moves + i); |
| 1089 memcpy(pgndata+offset, &nmove, NETMOVE_LEN); |
1090 memcpy(pgndata+offset, &nmove, NETMOVE_LEN); |
| 1181 } |
1182 } |
| 1182 } else if (code == NETCODE_PGNDATA) { |
1183 } else if (code == NETCODE_PGNDATA) { |
| 1183 net_recieve_data(server.fd, &(gamestate->info), sizeof(GameInfo)); |
1184 net_recieve_data(server.fd, &(gamestate->info), sizeof(GameInfo)); |
| 1184 uint16_t mc; |
1185 uint16_t mc; |
| 1185 net_recieve_data(server.fd, &mc, sizeof(mc)); |
1186 net_recieve_data(server.fd, &mc, sizeof(mc)); |
| 1186 mc = ntohs(mc); |
1187 mc = net_ntohs(mc); |
| 1187 char *movedata = malloc(mc * NETMOVE_LEN); |
1188 char *movedata = malloc(mc * NETMOVE_LEN); |
| 1188 net_recieve_data(server.fd, movedata, mc*NETMOVE_LEN); |
1189 net_recieve_data(server.fd, movedata, mc*NETMOVE_LEN); |
| 1189 unsigned offset = 0; |
1190 unsigned offset = 0; |
| 1190 for (size_t i = 0 ; i < mc ; i++) { |
1191 for (size_t i = 0 ; i < mc ; i++) { |
| 1191 NetMove nmove; |
1192 NetMove nmove; |