| 1 |
/* |
| 2 |
* TCPProtoSocketAgent.h |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
#ifndef TCPProtoSocketAgent_h |
| 7 |
#define TCPProtoSocketAgent_h |
| 8 |
|
| 9 |
#include <agent.h> |
| 10 |
#include <ip.h> |
| 11 |
|
| 12 |
#include "./tcp/TCPSocketAgent.h" |
| 13 |
#include "./tcp/TCPServerSocketAgent.h" |
| 14 |
#include "./tcp/TCPSocketFactory.h" |
| 15 |
#include "nsProtoSimAgent.h" |
| 16 |
#include "protoDebug.h" |
| 17 |
|
| 18 |
/** |
| 19 |
* @class NsTCPProtoSocketAgent |
| 20 |
* |
| 21 |
* @brief TCP implementation of the NSSocketProxy |
| 22 |
* class for providing TCP support within Protolib. |
| 23 |
* |
| 24 |
* This class provides a hub for interfacing with |
| 25 |
* the underlying TCP toolkit for providing access |
| 26 |
* to the TCP support. NsProtoTCPSocketAgent |
| 27 |
* essentially auto-detects which sockets it should |
| 28 |
* be (i.e. a client or a server) and then instantiates |
| 29 |
* the underlying implementations in order to provide |
| 30 |
* that behaviour, which are either a TCPSocketAgent |
| 31 |
* or a TCPServerSocketAgent: |
| 32 |
*/ |
| 33 |
|
| 34 |
class NsTCPProtoSocketAgent : public Agent, public TCPSocketAgentListener, public NsProtoSimAgent::NSSocketProxy { |
| 35 |
private: |
| 36 |
// either a server or a TCP pipe i.e. one to one connection. Once connected we deal with TCPSockets directly. |
| 37 |
// every socket becomes a TCPSocket after the first connection request. The server side doesn't care whether |
| 38 |
// its a server or not, it just receives and writes to a TCP pipe. |
| 39 |
|
| 40 |
TCPServerSocketAgent *serverSocket; |
| 41 |
TCPSocketAgent *tcpPipe; |
| 42 |
|
| 43 |
// for clients, the state changes from NotInitialised to TcpSocket |
| 44 |
// for servers, the state changes from NotInitialised to TcpServerSocket, then finally to TcpSocket once a connection |
| 45 |
// has been made |
| 46 |
enum SocketType {TcpServerSocket, TcpSocket, NotInitialised}; |
| 47 |
|
| 48 |
SocketType socketType_; |
| 49 |
|
| 50 |
bool isOpen; |
| 51 |
bool outputNotification; |
| 52 |
|
| 53 |
public: |
| 54 |
NsTCPProtoSocketAgent(); |
| 55 |
|
| 56 |
bool SendTo(const char* buffer, unsigned int& numBytes, const ProtoAddress& dstAddr); |
| 57 |
|
| 58 |
// implemnted from NSSocketProxy for TCP |
| 59 |
|
| 60 |
bool Connect(const ProtoAddress& theAddress); |
| 61 |
|
| 62 |
bool Accept(ProtoSocket* theSocket); |
| 63 |
|
| 64 |
bool Listen(UINT16 thePort); |
| 65 |
|
| 66 |
bool Bind(UINT16& thePort) { |
| 67 |
port()=thePort; |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
bool SetOutputNotification(bool outputnotify); |
| 72 |
|
| 73 |
// Custom Close method for our TCP sockets ... |
| 74 |
bool Close(); |
| 75 |
bool Shutdown(); |
| 76 |
|
| 77 |
// What do we do here for groups? |
| 78 |
|
| 79 |
bool JoinGroup(const ProtoAddress& groupAddr) { return true; } |
| 80 |
bool LeaveGroup(const ProtoAddress& groupAddr) { return true; } |
| 81 |
|
| 82 |
void setTCPSocketAgent(TCPSocketAgent *tcpSocketAgent) { tcpPipe=tcpSocketAgent; socketType_=TcpSocket;} |
| 83 |
|
| 84 |
TCPSocketAgent *getTCPSocketAgent() { return tcpPipe; } |
| 85 |
|
| 86 |
// My callback from the TCPSocketAgents |
| 87 |
|
| 88 |
bool tcpEventReceived(TCPEvent *event); // basic implementation here - override in your app specific one |
| 89 |
|
| 90 |
// override basic implementations to pass along to real agent: |
| 91 |
|
| 92 |
bool SetTxBufferSize(unsigned int bufferSize) { if (tcpPipe) tcpPipe->setTxBufferSize(bufferSize); else return false; return true; } |
| 93 |
unsigned int GetTxBufferSize() { if (tcpPipe) return tcpPipe->getTxBufferSize(); else return 0; } |
| 94 |
bool SetRxBufferSize(unsigned int bufferSize) { if (tcpPipe) tcpPipe->setRxBufferSize(bufferSize); else return false; return true; } |
| 95 |
unsigned int GetRxBufferSize() { if (tcpPipe) return tcpPipe->getRxBufferSize(); else return 0; } |
| 96 |
|
| 97 |
}; // end class TCPProtoSocketAgent |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
#endif |