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

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 <time.h>
  12. #include "msgsession.h"
  13. #include "udppacket.h"
  14. #include "icqlink.h"
  15. uint32 MsgSession::msgID = 0xffffffff;
  16. MsgSession::MsgSession(IcqLink *link, uint32 uin)
  17. : UdpSession(link, ICQ_SESSION_MSG, uin)
  18. {
  19. }
  20. uint32 MsgSession::sendMessage(uint8 type, const char *text)
  21. {
  22. time_t when = time(NULL);
  23. UdpOutPacket *out = createPacket(UDP_MSG_MESSAGE);
  24. *out << --msgID << type << (uint32) when << text;
  25. sendPacket(out);
  26. return msgID;
  27. }
  28. void MsgSession::onRecvMessage(UdpInPacket &in)
  29. {
  30. uint32 id, when;
  31. uint8 type;
  32. const char *text;
  33. in >> id >> type >> when >> text;
  34. UdpOutPacket *out = createPacket(UDP_MSG_MESSAGE_ACK, in.getSeq());
  35. *out << id;
  36. sendDirect(out);
  37. delete out;
  38. icqLink->onRecvMessage(type, uin, when, text, false);
  39. }
  40. void MsgSession::onMessageAck(UdpInPacket &in)
  41. {
  42. uint32 id;
  43. in >> id;
  44. icqLink->onAck(id);
  45. }
  46. bool MsgSession::onPacketReceived(UdpInPacket &in)
  47. {
  48. if (!UdpSession::onPacketReceived(in))
  49. return false;
  50. uint16 cmd = in.getCmd();
  51. switch (cmd) {
  52. case UDP_MSG_MESSAGE:
  53. onRecvMessage(in);
  54. break;
  55. case UDP_MSG_MESSAGE_ACK:
  56. onMessageAck(in);
  57. break;
  58. default:
  59. return false;
  60. }
  61. return true;
  62. }