Skip to content

Commit

Permalink
All is working
Browse files Browse the repository at this point in the history
  • Loading branch information
corigan01 committed Apr 29, 2020
1 parent 94c8690 commit 4c66693
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 83 deletions.
9 changes: 2 additions & 7 deletions CrossSockets/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <cstdio>
#include <sstream>
#include <algorithm>
#include <excpt.h>


using namespace std::this_thread; // sleep_for, sleep_until
Expand All @@ -63,7 +64,7 @@ using namespace std::chrono; // nanoseconds, system_clock, seconds
#endif // _WIN32

#ifdef __linux

#error "Linux is not suppored yet, we still use chrono for timing :("
#endif // __linux

using std::cout;
Expand Down Expand Up @@ -94,12 +95,6 @@ using std::endl;
}*/

// Defining Helpful macros
#define FOR(varname, iterations) for (int varname = 0; varname < iterations; varname++)
#define xstr(x) str(x)
#define str(x) #x
#define glue(x, y) x ## y

struct Server
{
std::string IP = "127.0.0.1";
Expand Down
29 changes: 17 additions & 12 deletions CrossSockets/CS_COM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,21 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "Base.h"
#include "CS_COM.h"

CS_COM::CS_COM(SOCKET *sock) {
Displ.out(D_INFO, "Stack Size of block is " + std::to_string(IO_SIZE) + " Bytes");

CS_COM::CS_COM(SOCKET *sock, bool NoOutput) {
SendingThr = std::thread(&CS_COM::SendThread, this);
RecvingThr = std::thread(&CS_COM::RecvThread, this);
this->sock = sock;

NoOut = NoOutput;

if (!NoOut) Displ.out(D_INFO, "Stack Size of block is " + std::to_string(IO_SIZE) + " Bytes");
}

CS_COM::~CS_COM() {
KillThreads = true;



SendingThr.join();
RecvingThr.join();

Expand Down Expand Up @@ -75,8 +79,8 @@ void CS_COM::Send(std::string Send) {


for (auto i : BrokenUp) {
//Displ.out(D_INFO, i);
//Displ.out(D_INFO, std::to_string(Built.length()) + " Bytes");
if(!NoOut) Displ.out(D_INFO, i);
//if(!NoOut) Displ.out(D_INFO, std::to_string(Built.length()) + " Bytes");

SendingQue.push_back(i);
}
Expand All @@ -89,7 +93,7 @@ std::vector<std::string> CS_COM::Recv() {
if (RecvingQue.size() > 0) {
auto Sendout = RecvingQue;
RecvingQue.clear();

return Sendout;
}

Expand All @@ -101,11 +105,12 @@ std::vector<std::string> CS_COM::Recv() {
void CS_COM::SendThread() {
while (!KillThreads) {
sleep_for(16ms);
for (auto i : SendingQue) {
send((SOCKET)*sock, i.c_str(), i.length() + 1, 0);

for (int i = 0; i < SendingQue.size(); i++) {
send((SOCKET)*sock, SendingQue[i].c_str(), SendingQue[i].length() + 1, 0);
}

SendingQue.clear();

}


Expand All @@ -122,13 +127,13 @@ void CS_COM::RecvThread() {
int bytesReceived = recv((SOCKET)*sock, buf, IO_SIZE + 20, 0);
if (bytesReceived == SOCKET_ERROR)
{
Displ.out(D_ERROR, "Error in recv, Quitting Thread...");
if(!NoOut) Displ.out(D_ERROR, "Error in recv, Quitting Thread...");
ComError = true;
break;
}
if (bytesReceived == 0)
{
Displ.out(D_WARNING, "Disconnected...");
if(!NoOut) Displ.out(D_WARNING, "Disconnected...");
ComError = true;
break;
}
Expand Down Expand Up @@ -173,7 +178,7 @@ void CS_COM::RecvThread() {

RecvingQue.push_back(str);

Displ.out(D_INFO, str);
if(!NoOut) Displ.out(D_INFO, str);


}
Expand Down
4 changes: 2 additions & 2 deletions CrossSockets/CS_COM.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
class CS_COM
{
public:
CS_COM(SOCKET *sock);
CS_COM(SOCKET* sock, bool NoOutput = false);
~CS_COM(/*Global*/);

//protected:
Expand All @@ -73,7 +73,7 @@ class CS_COM
SOCKET *sock;

bool ComError = false;

bool NoOut = false;
bool KillThreads = false;

void SendThread();
Expand Down
130 changes: 130 additions & 0 deletions CrossSockets/CS_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,133 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "Base.h"
#include "CS_Client.h"

CS_Client::CS_Client(int port, std::string Ip, int TryConnectionCount, bool AddStringBuildBack) {
Port = port;
IP = Ip;
BuildBack = AddStringBuildBack;
TryCount = TryConnectionCount;

StartClient();
}

CS_Client::~CS_Client() {
StopClient();
}


void CS_Client::StopClient() {
while (true) {
sleep_for(66ms);
if (ClientsThread.joinable()) {
ClientsThread.join();
break;
}
}
dis.out(D_WARNING, "Stopped Threads");
}

void CS_Client::StartClient() {
dis.out(D_INFO, "Started Threads");

ClientsThread = std::thread(&CS_Client::ClientThread, this);

}

std::vector<std::string> CS_Client::GetRec() {
return Recv;
}

void CS_Client::Send(std::string data) {
Sendv.push_back(data);
}


void CS_Client::ClientThread() {
__START:

Sleep(100);
dis.out(D_INFO, "Client STARTING ...");
dis.out(D_INFO, "Client Starting Connection: %s::%d");

dis.out(D_INFO, "Client Windows Platform - Using WinSock");

WSAData Data;
WORD ver = MAKEWORD(2, 2);

int WS_START = WSAStartup(ver, &Data);
if (WS_START != 0) {
dis.out(D_ERROR, "Client Unable To Start WinSock. ERROR #%d");

return;
}

// Create The Socket
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
{
dis.out(D_ERROR, "Client Can't create socket, Err #%s");
WSACleanup();
return;
}

dis.out(D_INFO, "Client Winsock Started!");
dis.out(D_INFO, "Client Starting hint structure...");

// Fill in a hint structure
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(Port);
inet_pton(AF_INET, IP.c_str(), &hint.sin_addr);

// Connect to server

dis.out(D_INFO, "Client Setup complete!");

int TryCounting = 0;
do {
sleep_for(33ms);
dis.out(D_INFO, "Client Conneting...");
int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint));
if (connResult == SOCKET_ERROR)
{
dis.out(D_WARNING, "Client Connection Failed, Retrying...");
TryCounting++;
}
else {
break;
}
if (TryCounting >= TryCount) {
StopAll = true;
dis.out(D_ERROR, "Client Connection Failed! Server Not Found! Stopping Threads...");
return;
}
} while (!StopAll);


CS_COM com(&sock, false);
dis.out(D_INFO, "Client CONNECTED");

while (!StopAll) {
sleep_for(33ms);


for (auto i : com.Recv()) {
Recv.push_back(i);
}

for (auto i : Sendv) {
com.Send(i);
}

Sendv.clear();

if (com.Error()) {
dis.out(D_ERROR, "Error, restarting Client...");
goto __START;
}

}


}
27 changes: 26 additions & 1 deletion CrossSockets/CS_Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,34 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
// ---------------- CODE ---------------- \\
#include "Base.h"
#include "displayout.h"
#include "CS_COM.h"


class CS_Client
{
};
public:
CS_Client(int port, std::string Ip, int TryConnectionCount = 10, bool AddStringBuildBack = false);
~CS_Client();

void StopClient();
void StartClient();

std::vector<std::string> GetRec();
void Send(std::string data);


private:
int Port;
std::string IP;
bool BuildBack;
int TryCount;
displayout dis;
std::thread ClientsThread;
std::vector<std::string> Recv;
std::vector<std::string> Sendv;

bool StopAll = false;

void ClientThread();
};
Loading

0 comments on commit 4c66693

Please sign in to comment.