tcpObject.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "sim/simBase.h"
  26. #endif
  27. class TCPObject : public SimObject
  28. {
  29. public:
  30. enum State {Disconnected, DNSResolved, Connected, Listening };
  31. private:
  32. NetSocket mTag;
  33. TCPObject *mNext;
  34. enum { TableSize = 256, TableMask = 0xFF };
  35. static TCPObject *table[TableSize];
  36. State mState;
  37. protected:
  38. typedef SimObject Parent;
  39. U8 *mBuffer;
  40. U32 mBufferSize;
  41. U16 mPort;
  42. public:
  43. TCPObject();
  44. virtual ~TCPObject();
  45. void parseLine(U8 *buffer, U32 *start, U32 bufferLen);
  46. void finishLastLine();
  47. static TCPObject *find(NetSocket tag);
  48. // onReceive gets called continuously until all bytes are processed
  49. // return # of bytes processed each time.
  50. virtual U32 onReceive(U8 *buffer, U32 bufferLen); // process a buffer of raw packet data
  51. virtual bool processLine(U8 *line); // process a complete line of text... default action is to call into script
  52. virtual void onDNSResolved();
  53. virtual void onDNSFailed();
  54. virtual void onConnected();
  55. virtual void onConnectFailed();
  56. virtual void onConnectionRequest(const NetAddress *addr, U32 connectId);
  57. virtual void onDisconnect();
  58. void connect(const char *address);
  59. void listen(U16 port);
  60. //Luma: Used to force networking to be opened before connecting... written specifically to handle GPRS/EDGE/3G situation on iPhone, but can be expanded to other platforms too
  61. void openAndConnect(const char *address);
  62. void disconnect();
  63. State getState() { return mState; }
  64. //Luma: Encode data before sending via TCP so that only valid URL characters are sent
  65. U8 *URLEncodeData(U8 *pData, U32 iDataSize, U32 *piNewDataSize);
  66. bool processArguments(S32 argc, const char **argv);
  67. void send(const U8 *buffer, U32 bufferLen);
  68. void addToTable(NetSocket newTag);
  69. void removeFromTable();
  70. void setPort(U16 port) { mPort = port; }
  71. bool onAdd();
  72. DECLARE_CONOBJECT(TCPObject);
  73. };
  74. #endif // _H_TCPOBJECT_