knetfile.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef KNETFILE_H
  2. #define KNETFILE_H
  3. #include <stdint.h>
  4. #include <fcntl.h>
  5. #ifndef _WIN32
  6. #define netread(fd, ptr, len) read(fd, ptr, len)
  7. #define netwrite(fd, ptr, len) write(fd, ptr, len)
  8. #define netclose(fd) close(fd)
  9. #else
  10. #include <winsock2.h>
  11. #define netread(fd, ptr, len) recv(fd, ptr, len, 0)
  12. #define netwrite(fd, ptr, len) send(fd, ptr, len, 0)
  13. #define netclose(fd) closesocket(fd)
  14. #endif
  15. // FIXME: currently I/O is unbuffered
  16. #define KNF_TYPE_LOCAL 1
  17. #define KNF_TYPE_FTP 2
  18. #define KNF_TYPE_HTTP 3
  19. typedef struct knetFile_s {
  20. int type, fd;
  21. int64_t offset;
  22. char *host, *port;
  23. // the following are for FTP only
  24. int ctrl_fd, pasv_ip[4], pasv_port, max_response, no_reconnect, is_ready;
  25. char *response, *retr, *size_cmd;
  26. int64_t seek_offset; // for lazy seek
  27. int64_t file_size;
  28. // the following are for HTTP only
  29. char *path, *http_host;
  30. } knetFile;
  31. #define knet_tell(fp) ((fp)->offset)
  32. #define knet_fileno(fp) ((fp)->fd)
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #ifdef _WIN32
  37. int knet_win32_init();
  38. void knet_win32_destroy();
  39. #endif
  40. knetFile *knet_open(const char *fn, const char *mode);
  41. /*
  42. This only works with local files.
  43. */
  44. knetFile *knet_dopen(int fd, const char *mode);
  45. /*
  46. If ->is_ready==0, this routine updates ->fd; otherwise, it simply
  47. reads from ->fd.
  48. */
  49. off_t knet_read(knetFile *fp, void *buf, off_t len);
  50. /*
  51. This routine only sets ->offset and ->is_ready=0. It does not
  52. communicate with the FTP server.
  53. */
  54. off_t knet_seek(knetFile *fp, int64_t off, int whence);
  55. int knet_close(knetFile *fp);
  56. #ifdef __cplusplus
  57. }
  58. #endif
  59. #endif