Ejemplo n.º 1
0
int send_file (int sock, char *filename) {
    FILE         *fp;
    byte          current_byte;
    int           read_len;
    unsigned int  file_length;
    unsigned int  i;

    if (send_line(sock, filename, strlen(filename)) < 0) return -1;

    // Open the file to determine the number of bytes that
    // need to be sent
    fp = fopen(filename, "rb");
    if (!fp) {
        if (send_uint32(sock, 0) < 0) return -1;
        return -1;
    }

    fseek(fp, 0L, SEEK_END);
    file_length = ftell(fp);

#if DEBUG
    printf("Sending %s | %d bytes | ... ", filename, file_length);
    fflush(stdout);
#endif

    // Send the file size to the client
    if (send_uint32(sock, file_length) < 0) return -1;

    // Send each byte to the client starting from the start
    rewind(fp);
    for (i = 0; i < file_length; ++i) {
        read_len = fread(&current_byte, 1, 1, fp);
        if (read_len <= 0) {

            // send 0 to keep the client in sync
            if (send_byte(sock, 0) < 0) return -1;
            continue;
        }

#if DEBUG
        printf("\rSending %s | %d bytes | %d%%... ", filename, file_length,
                (int) (100 * floor((i + 1) / file_length)));
        fflush(stdout);
#endif

        if (send_byte(sock, current_byte) < 0) return -1;
    }

    // Close the file
    fclose(fp);

#if DEBUG
    printf("Done.\n");
#endif
    return 0;
}
Ejemplo n.º 2
0
void GameObjectMessage::send(uint32_t object_id, GameObject *game_object) {
  send_uint32(socket_, object_id);
  send_vector(socket_, game_object->body().position());
  send_char(socket_, game_object->game_object_type());
  send_object_points(socket_, game_object->characteristic_points());
  send_bool(socket_, game_object->alive());
}