filesession.cpp
上传用户:zslianheng
上传日期:2013-04-03
资源大小:946k
文件大小:3k
源码类别:

Linux/Unix编程

开发平台:

Visual C++

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *   This program is free software; you can redistribute it and/or modify  *
  4.  *   it under the terms of the GNU General Public License as published by  *
  5.  *   the Free Software Foundation; either version 2 of the License, or     *
  6.  *   (at your option) any later version.                                   *
  7.  *                                                                         *
  8.  *   copyright            : (C) 2002 by Zhang Yong                         *
  9.  *   email                : z-yong163@163.com                              *
  10.  ***************************************************************************/
  11. #include "stdafx.h"
  12. #include "filesession.h"
  13. #include "packet.h"
  14. enum {
  15. TCP_FILE_INFO = 0x1000,
  16. TCP_FILE_RECEIVE,
  17. TCP_FILE_DATA,
  18. };
  19. FileSession::FileSession(TcpSessionBase *s)
  20. {
  21. tcp = s;
  22. listener = NULL;
  23. bytesSent = 0;
  24. file = NULL;
  25. isSend = s->isSendSession();
  26. }
  27. FileSession::~FileSession()
  28. {
  29. if (file)
  30. fclose(file);
  31. if (tcp)
  32. tcp->destroy();
  33. }
  34. void FileSession::sendFileInfo(const char *path, const char *name, uint32 size)
  35. {
  36. fileSize = size;
  37. OutPacket *out = tcp->createPacket(TCP_FILE_INFO);
  38. *out << name << size;
  39. tcp->sendPacket(out);
  40. file = fopen(path, "rb");
  41. }
  42. void FileSession::onFileInfo(InPacket &in)
  43. {
  44. if (!listener)
  45. return;
  46. string name;
  47. in >> name >> fileSize;
  48. const char *path = listener->getPathName(name.c_str(), fileSize);
  49. if (path) {
  50. OutPacket *out = tcp->createPacket(TCP_FILE_RECEIVE);
  51. tcp->sendPacket(out);
  52. file = fopen(path, "wb");
  53. }
  54. }
  55. void FileSession::onFileReceive(InPacket &in)
  56. {
  57. if (listener)
  58. listener->onFileReceive();
  59. tcp->enableWrite();
  60. }
  61. void FileSession::onFileData(InPacket &in)
  62. {
  63. int n;
  64. const char *data = in.readData(n);
  65. n = fwrite(data, 1, n, file);
  66. bytesSent += n;
  67. if (listener)
  68. listener->onFileProgress(bytesSent);
  69. }
  70. bool FileSession::onPacketReceived(InPacket &in)
  71. {
  72. uint16 cmd = in.getCmd();
  73. switch (cmd) {
  74. case TCP_FILE_INFO:
  75. onFileInfo(in);
  76. break;
  77. case TCP_FILE_RECEIVE:
  78. onFileReceive(in);
  79. break;
  80. case TCP_FILE_DATA:
  81. onFileData(in);
  82. break;
  83. }
  84. return true;
  85. }
  86. void FileSession::onSend()
  87. {
  88. if (!isSend)
  89. return;
  90. if (file) {
  91. char buf[2048];
  92. int n = fread(buf, 1, sizeof(buf), file);
  93. OutPacket *out = tcp->createPacket(TCP_FILE_DATA);
  94. out->writeData(buf, n);
  95. if (tcp->sendPacket(out)) {
  96. bytesSent += n;
  97. if (listener)
  98. listener->onFileProgress(bytesSent);
  99. if (bytesSent >= fileSize) {
  100. fclose(file);
  101. file = NULL;
  102. }
  103. } else {
  104. fclose(file);
  105. file = NULL;
  106. }
  107. }
  108. if (!file) {
  109. tcp->shutdown();
  110. if (listener)
  111. listener->onFileFinished();
  112. } else
  113. tcp->enableWrite();
  114. }
  115. void FileSession::onClose()
  116. {
  117. if (file) {
  118. fclose(file);
  119. file = NULL;
  120. }
  121. if (listener)
  122. listener->onFileFinished();
  123. }