tcpObject.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. bool isBufferEmpty();
  48. void emptyBuffer();
  49. static TCPObject *find(NetSocket tag);
  50. // onReceive gets called continuously until all bytes are processed
  51. // return # of bytes processed each time.
  52. virtual U32 onReceive(U8 *buffer, U32 bufferLen); // process a buffer of raw packet data
  53. virtual bool processLine(U8 *line); // process a complete line of text... default action is to call into script
  54. virtual void onDNSResolved();
  55. virtual void onDNSFailed();
  56. virtual void onConnected();
  57. virtual void onConnectFailed();
  58. virtual void onConnectionRequest(const NetAddress *addr, U32 connectId);
  59. virtual void onDisconnect();
  60. void connect(const char *address);
  61. void listen(U16 port);
  62. //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
  63. void openAndConnect(const char *address);
  64. void disconnect();
  65. State getState() { return mState; }
  66. //Luma: Encode data before sending via TCP so that only valid URL characters are sent
  67. U8 *URLEncodeData(U8 *pData, U32 iDataSize, U32 *piNewDataSize);
  68. bool processArguments(S32 argc, const char **argv);
  69. void send(const U8 *buffer, U32 bufferLen);
  70. void addToTable(NetSocket newTag);
  71. void removeFromTable();
  72. void setPort(U16 port) { mPort = port; }
  73. bool onAdd();
  74. DECLARE_CONOBJECT(TCPObject);
  75. };
  76. #endif // _H_TCPOBJECT_