tcpObject.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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(bool, onPacket, (const char* data));
  35. DECLARE_CALLBACK(void, onEndReceive, ());
  36. DECLARE_CALLBACK(void, onDNSResolved,());
  37. DECLARE_CALLBACK(void, onDNSFailed, ());
  38. DECLARE_CALLBACK(void, onConnected, ());
  39. DECLARE_CALLBACK(void, onConnectFailed, ());
  40. DECLARE_CALLBACK(void, onDisconnect, ());
  41. private:
  42. NetSocket mTag;
  43. TCPObject *mNext;
  44. enum { TableSize = 256, TableMask = 0xFF };
  45. static TCPObject *table[TableSize];
  46. State mState;
  47. protected:
  48. typedef SimObject Parent;
  49. U8 *mBuffer;
  50. U32 mBufferSize;
  51. U16 mPort;
  52. public:
  53. TCPObject();
  54. virtual ~TCPObject();
  55. void parseLine(U8 *buffer, U32 *start, U32 bufferLen);
  56. bool finishLastLine();
  57. bool isBufferEmpty();
  58. void emptyBuffer();
  59. static TCPObject *find(NetSocket tag);
  60. // onReceive gets called continuously until all bytes are processed
  61. // return # of bytes processed each time.
  62. virtual U32 onReceive(U8 *buffer, U32 bufferLen); // process a buffer of raw packet data
  63. virtual bool processLine(UTF8 *line); // process a complete line of text... default action is to call into script
  64. virtual void onDNSResolved();
  65. virtual void onDNSFailed();
  66. virtual void onConnected();
  67. virtual void onConnectFailed();
  68. virtual void onConnectionRequest(const NetAddress *addr, U32 connectId);
  69. virtual void onDisconnect();
  70. void connect(const char *address);
  71. void listen(U16 port);
  72. void disconnect();
  73. State getState() { return mState; }
  74. bool processArguments(S32 argc, ConsoleValue *argv);
  75. void send(const U8 *buffer, U32 bufferLen);
  76. ///Send an entire file over tcp
  77. ///@arg fileName Full path to file you want to send
  78. ///@return true if file was sent, false if not (file doesn't exist)
  79. bool sendFile(const char* fileName);
  80. void addToTable(NetSocket newTag);
  81. void removeFromTable();
  82. void setPort(U16 port) { mPort = port; }
  83. bool onAdd();
  84. DECLARE_CONOBJECT(TCPObject);
  85. };
  86. #endif // _H_TCPOBJECT_