tcpObject.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TCPOBJECT_H_
  23. #define _TCPOBJECT_H_
  24. #ifndef _SIMBASE_H_
  25. #include "console/simBase.h"
  26. #endif
  27. #include "platform/platformNet.h"
  28. class TCPObject : public SimObject
  29. {
  30. public:
  31. enum State {Disconnected, DNSResolved, Connected, Listening };
  32. DECLARE_CALLBACK(void, onConnectionRequest, (const char* address, const char* ID));
  33. DECLARE_CALLBACK(void, onLine, (const char* line));
  34. DECLARE_CALLBACK(void, onDNSResolved,());
  35. DECLARE_CALLBACK(void, onDNSFailed, ());
  36. DECLARE_CALLBACK(void, onConnected, ());
  37. DECLARE_CALLBACK(void, onConnectFailed, ());
  38. DECLARE_CALLBACK(void, onDisconnect, ());
  39. private:
  40. NetSocket mTag;
  41. TCPObject *mNext;
  42. enum { TableSize = 256, TableMask = 0xFF };
  43. static TCPObject *table[TableSize];
  44. State mState;
  45. protected:
  46. typedef SimObject Parent;
  47. U8 *mBuffer;
  48. U32 mBufferSize;
  49. U16 mPort;
  50. public:
  51. TCPObject();
  52. virtual ~TCPObject();
  53. void parseLine(U8 *buffer, U32 *start, U32 bufferLen);
  54. void finishLastLine();
  55. static TCPObject *find(NetSocket tag);
  56. // onReceive gets called continuously until all bytes are processed
  57. // return # of bytes processed each time.
  58. virtual U32 onReceive(U8 *buffer, U32 bufferLen); // process a buffer of raw packet data
  59. virtual bool processLine(UTF8 *line); // process a complete line of text... default action is to call into script
  60. virtual void onDNSResolved();
  61. virtual void onDNSFailed();
  62. virtual void onConnected();
  63. virtual void onConnectFailed();
  64. virtual void onConnectionRequest(const NetAddress *addr, U32 connectId);
  65. virtual void onDisconnect();
  66. void connect(const char *address);
  67. void listen(U16 port);
  68. void disconnect();
  69. State getState() { return mState; }
  70. bool processArguments(S32 argc, ConsoleValueRef *argv);
  71. void send(const U8 *buffer, U32 bufferLen);
  72. void addToTable(NetSocket newTag);
  73. void removeFromTable();
  74. void setPort(U16 port) { mPort = port; }
  75. bool onAdd();
  76. DECLARE_CONOBJECT(TCPObject);
  77. };
  78. #endif // _H_TCPOBJECT_