#include "TTTSocketCommunication.h" TTTSocketCommunication::TTTSocketCommunication(string p) { client = NULL; server = NULL; bool connected = false; //Das ist ein bisschen tricky //Zuerst muss geschaut werden, ob der Partner bereits läuft. //Dieser ist dann der Server und diese Instanz läuft als Client //Läuft der Partner noch nicht, ist diese Instanz der Server //Port des Servers ist 50000 //Erstmal als Client testen client = new Socket(p, LOCALPORT); connected = client->connect(); cerr << "Verbunden als Client: " << connected << endl; if ( !connected ) { //Gegenstation läuft noch nicht, also zum Server werden cerr << "Bin Server...warte auf Gegenstation" << endl; server = new ServerSocket(LOCALPORT); if ( client != NULL ) delete client; master = true; slave = false; client = server->accept(); cerr << "Gegenstation aktiv!" << endl; connected = true; } else { master = false; slave = true; } } TTTSocketCommunication::~TTTSocketCommunication(void) { if ( client != NULL ) { client->close(); delete client; } if ( server !=NULL ) { server->close(); delete server; } } bool TTTSocketCommunication::getMaster(void) { return master; } bool TTTSocketCommunication::open(void) { //return port->connect(); return connected; } void TTTSocketCommunication::setStationActive() { //port->setDTR(true); } bool TTTSocketCommunication::isOtherStationActive() { //return port->isDSR(); return connected; } void TTTSocketCommunication::sendPlayerName1(string n1) { client->write(n1+"\n"); } string TTTSocketCommunication::readPlayerName2(void) { return client->readLine(); } void TTTSocketCommunication::sendBeginningPlayer(int bp) { client->write(bp); } int TTTSocketCommunication::readBeginningPlayer() { return client->read(); } void TTTSocketCommunication::sendWinState(int ws) { client->write(ws); } int TTTSocketCommunication::readWinState() { return client->read(); } void TTTSocketCommunication::sendMove(COORD move) { client->write(move.X); client->write(move.Y); } COORD TTTSocketCommunication::readMove(void) { COORD move; move.X = client->read(); move.Y = client->read(); return move; }