Skip to content

Commit

Permalink
Changed the Displayout section, the display out is still in WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
corigan01 committed Mar 13, 2020
1 parent 05a1db1 commit d1fd465
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 47 deletions.
3 changes: 2 additions & 1 deletion CrossSockets/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <thread>
#include <fstream>
#include <ostream>
#include <atomic>
#include <time.h>
#include <cstdio>
#include <sstream>
Expand All @@ -63,7 +64,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.



#define EOR printf("[DEBUGING USE ONLY] PASSED LINE: %d. ERROR NOT BEFORE PRINT!\n", __LINE__)
#define EOR std::cout << "[DEBUGING USE ONLY] PASSED LINE: " << __LINE__ << std::endl;

#ifndef _STD
#define _STD ::std::
Expand Down
173 changes: 132 additions & 41 deletions CrossSockets/CPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,13 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "CPSocket.h"

/*CPSocket();
~CPSocket();
void StartClient();
void StartServer();
bool IsConnected();
bool IsAuth();
void SendString();
std::string GetString();
private:
std::thread SocketThreadRx;
std::thread SocketThreadTx;
Server ConnectionProp;
bool IsConnectedTx = false;
bool IsConnectedRx = false;
bool IsAuthTx = false;
bool IsAuthRx = false;
void ClientTx();
void ClientRx();
void ServerTx();
void ServerRx();*/

#define AuthString "AUTH - SSC HyperSocketLink (COM METHOD START - SECURE SOCKET COMMUNICATION)"
#define bufferSize 1000000
#define ConnectionTryCount 10

#define DISABLE_AUTH 0

// == CPSocket Methods
// initing
CPSocket::CPSocket() { srand(time(0)); AUTH_NUMBER = (((rand() + rand()) / rand()) * rand() + rand() + rand()); }
Expand All @@ -78,6 +49,7 @@ CPSocket::~CPSocket() {

bool SocketThreadRxDone = false;
bool SocketThreadTxDone = false;


displayout(D_WARNING, "CPSocket Closing...");

Expand Down Expand Up @@ -413,28 +385,37 @@ void CPSocket::ServerManager() {
}

}

void CPSocket::ServerRx(int id) {
Sleep(100);

displayout(D_INFO, "[RX] STARTING ...");
displayout(D_INFO, "[RX] Starting Connection: %s::%d", ConnectionProp.IP, ConnectionProp.InBoundPort);

displayout(D_INFO, "[RX] Windows Platform - Using WinSock");

// Initialze winsock
WSADATA wsData;
WORD ver = MAKEWORD(2, 2);

int wsOk = WSAStartup(ver, &wsData);
if (wsOk != 0)
{
std::cerr << "Can't Initialize winsock! Quitting" << std::endl;
displayout(D_ERROR, "[RX] Unable To Start WinSock. ERROR #%d");

}

// Create a socket
SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
if (listening == INVALID_SOCKET)
{
std::cerr << "Can't create a socket! Quitting" << std::endl;
displayout(D_ERROR, "[RX] Unable To Start WinSock. ERROR #%d");

}

displayout(D_INFO, "[RX] Winsock Started!");
displayout(D_INFO, "[RX] Starting hint structure...");

// txtd the ip address and port to a socket
sockaddr_in hint;
hint.sin_family = AF_INET;
Expand All @@ -450,6 +431,10 @@ void CPSocket::ServerRx(int id) {
sockaddr_in client;
int clientSize = sizeof(client);

displayout(D_INFO, "[TX] Setup complete!");

displayout(D_INFO, "[TX] Conneting...");

SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize);

char host[NI_MAXHOST]; // Client's remote name
Expand All @@ -458,6 +443,8 @@ void CPSocket::ServerRx(int id) {
ZeroMemory(host, NI_MAXHOST); // same as memset(host, 0, NI_MAXHOST);
ZeroMemory(service, NI_MAXSERV);



if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
{
displayout(D_INFO, (host + std::string(" Connected on Port: ") + service).c_str());
Expand All @@ -469,6 +456,8 @@ void CPSocket::ServerRx(int id) {
displayout(D_INFO, (host + std::string(" Connected on Port: ") + std::to_string(ntohs(client.sin_port))).c_str());
}

displayout(D_INFO, "[RX] CONNECTED");

// Close listening socket
closesocket(listening);

Expand Down Expand Up @@ -500,11 +489,12 @@ void CPSocket::ServerRx(int id) {

while (!ThreadShouldStop)
{

std::string GetString = RxV(clientSocket);
displayout(D_INFO, GetString.c_str());




if (GetString.size() > 0) {
RxQue.push_back(GetString);
}

}

Expand All @@ -523,13 +513,114 @@ void CPSocket::ServerRx(int id) {
void CPSocket::ServerTx(int id) {
Sleep(115);

displayout(D_INFO, "[TX] STARTING ...");
displayout(D_INFO, "[TX] Starting Connection: %s::%d", ConnectionProp.IP, ConnectionProp.InBoundPort);

displayout(D_INFO, "[TX] Windows Platform - Using WinSock");

// Initialze winsock
WSADATA wsData;
WORD ver = MAKEWORD(2, 2);

while (!ThreadShouldStop) {

Sleep(10);
int wsOk = WSAStartup(ver, &wsData);
if (wsOk != 0)
{
displayout(D_ERROR, "[TX] Unable To Start WinSock. ERROR #%d");

}

// Create a socket
SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
if (listening == INVALID_SOCKET)
{
displayout(D_ERROR, "[TX] Unable To Start WinSock. ERROR #%d");

}
displayout(D_INFO, "[TX] Winsock Started!");
displayout(D_INFO, "[TX] Starting hint structure...");


// txtd the ip address and port to a socket
sockaddr_in hint;
hint.sin_family = AF_INET;
hint.sin_port = htons(ConnectionProp.OutBound);
hint.sin_addr.S_un.S_addr = INADDR_ANY; // Could also use inet_pton ....


bind(listening, (sockaddr*)&hint, sizeof(hint));

// Tell Winsock the socket is for listening
listen(listening, SOMAXCONN);

// Wait for a connection
sockaddr_in client;
int clientSize = sizeof(client);

displayout(D_INFO, "[TX] Setup complete!");

displayout(D_INFO, "[TX] Conneting...");

SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize);


char host[NI_MAXHOST]; // Client's remote name
char service[NI_MAXSERV]; // Service (i.e. port) the client is connect on

ZeroMemory(host, NI_MAXHOST); // same as memset(host, 0, NI_MAXHOST);
ZeroMemory(service, NI_MAXSERV);


if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
{
displayout(D_INFO, (host + std::string(" Connected on Port: ") + service).c_str());
HostNames[LookUpArrayId(id)] = host;
}
else
{
inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST);
displayout(D_INFO, (host + std::string(" Connected on Port: ") + std::to_string(ntohs(client.sin_port))).c_str());
}

displayout(D_INFO, "[TX] CONNECTED");

// Close listening socket
closesocket(listening);

// While loop: accept and echo message back to client


ServersFull = true;

#ifndef DISABLE_AUTH
// Start Authing
if (!ThreadShouldStop) {
displayout(D_INFO, "[TX] Begining Auth...");


while (!ThreadShouldStop) {
TxV(clientSocket, AuthString);


if (IsAuthSSC) {
break;
}
}
}

#endif // !DISABLE_AUTH

while (!ThreadShouldStop)
{
if (TxQue.size() > 0) {
TxV(clientSocket, TxQue[0]);
displayout(D_LOG, TxQue[0].c_str());
TxQue.erase(TxQue.begin());
}
}

// Close the socket
closesocket(clientSocket);


}

Expand Down Expand Up @@ -559,4 +650,4 @@ std::string CPSocket::RxV(SOCKET sock) {

void CPSocket::TxV(SOCKET COM, std::string Data) {
send(COM, Data.c_str(), Data.length() + 1, 0);
}
}
2 changes: 1 addition & 1 deletion CrossSockets/CPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class CPSocket
void SendString(std::string RecStr);
std::vector<std::string> GetString();




private:
Expand Down
3 changes: 3 additions & 0 deletions CrossSockets/CrossSockets.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,18 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalOptions>/FORCE:MULTIPLE %(AdditionalOptions)</AdditionalOptions>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="CPSocket.cpp" />
<ClCompile Include="displayout.cpp" />
<ClCompile Include="Source.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Base.h" />
<ClInclude Include="CPSocket.h" />
<ClInclude Include="displayout.h" />
<ClInclude Include="Template.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
6 changes: 6 additions & 0 deletions CrossSockets/CrossSockets.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<ClCompile Include="Source.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="displayout.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Base.h">
Expand All @@ -32,5 +35,8 @@
<ClInclude Include="Template.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="displayout.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
7 changes: 3 additions & 4 deletions CrossSockets/Source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@


int main() {
CPSocket socket("127.0.0.1", 56040, 56040);
CPSocket socket("127.0.0.1", 56040, 56000);

//socket.StartClient();
socket.StartServer();

while (true) {
std::string Input = "";
std::cin >> Input;

if (Input == "STOP") break;
}

std::cout << "Sure" << std::endl;

socket.~CPSocket();

Expand Down
1 change: 1 addition & 0 deletions CrossSockets/displayout.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "displayout.h"
Loading

0 comments on commit d1fd465

Please sign in to comment.