Created
December 21, 2018 09:09
-
-
Save chriship/36a6b2ae89f5147000a6a209b99bda4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofMain.h" | |
#include "ofxNetwork.h" | |
namespace ads { | |
class TcpClient | |
{ | |
public: | |
TcpClient() { | |
//weConnected.setSerializable(false); | |
} | |
void setup(string ip, int port) { | |
mIp = ip; | |
mPort = port; | |
setup(); | |
} | |
void setup(){ | |
ofSetLogLevel(OF_LOG_VERBOSE); | |
ofLogVerbose("skodaTCPClient") << "setup"; | |
weConnected = tcpClient.setup(mIp, mPort); | |
tcpClient.setMessageDelimiter("\n"); | |
ofLogVerbose("skodaTCPClient") << "connected"; | |
connectTime = 0; | |
deltaTime = 0; | |
//ofAddListener(ofEvents().update, this, &TcpClient::update); | |
} | |
void update() { | |
if (weConnected) { | |
if (!tcpClient.isConnected()) { | |
ofLogVerbose("skodaTCPClient") << "disconnected"; | |
weConnected = false; | |
} | |
} | |
else { | |
//if we are not connected lets try and reconnect every 5 seconds | |
deltaTime = ofGetElapsedTimeMillis() - connectTime; | |
if (deltaTime > 5000) { | |
ofLogVerbose("skodaTCPClient") << "retrying connect..."; | |
weConnected = tcpClient.setup(mIp, mPort); | |
tcpClient.setMessageDelimiter("\n"); | |
ofLogVerbose("skodaTCPClient") << (weConnected ? "SUCCESS" : "FAIL"); | |
connectTime = ofGetElapsedTimeMillis(); | |
} | |
} | |
} | |
void close() { | |
tcpClient.close(); | |
} | |
void sendMessage(string msg) { | |
tcpClient.send(msg); | |
} | |
ofEvent<string> tcpMessageReceived; | |
private: | |
ofxTCPClient tcpClient; | |
string msgTx, msgRx; | |
int connectTime; | |
int deltaTime; | |
public: | |
bool weConnected{ false }; | |
ofParameter<int> mPort{ "port", 11111, 0, 99999 }; | |
ofParameter<string> mIp{ "IP Address", "127.0.0.1" }; | |
ofParameterGroup prm{ "TCP Client", mPort, mIp}; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment