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