4h of work

This commit is contained in:
Anonymous275
2020-04-12 01:14:16 +03:00
parent e60790e185
commit 9a47f651fc
11 changed files with 548 additions and 342 deletions

View File

@@ -8,6 +8,7 @@
#include <cstdio>
#include "../logger.h"
void ParseData(ENetPacket*packet,ENetPeer*peer); //Data Parser
void OnConnect(ENetPeer*peer);
@@ -82,3 +83,64 @@ void ServerMain(int Port, int MaxClients) {
enet_deinitialize();
return;
}
void CreateNewThread(void*);
void TCPMain(int Port){
info("Starting TCP Server on port " + to_string(Port));
WSADATA wsaData;
int iResult;
sockaddr_in addr{};
SOCKET sock,client;
addr.sin_family = AF_INET;
addr.sin_port = htons(Port);
iResult = WSAStartup(MAKEWORD(2,2),&wsaData);
if(iResult)
{
printf("WSA startup failed");
return;
}
sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sock == INVALID_SOCKET)
{
printf("Invalid socket");
return;
}
iResult = bind(sock,(sockaddr*)&addr,sizeof(sockaddr_in ));
if(iResult)
{
printf("bind failed %lu",GetLastError());
return;
}
iResult = listen(sock,SOMAXCONN);
if(iResult)
{
printf("iResult failed %lu",GetLastError());
return;
}
while(client = accept(sock,nullptr,nullptr))
{
if(client == INVALID_SOCKET)
{
printf("invalid client socket\n");
continue;
}
CreateNewThread((void*)&client);
}
}

View File

@@ -0,0 +1,118 @@
///
/// Created by Anonymous275 on 4/11/2020
///
#include <thread>
#include <string>
#include <fstream>
#include <iostream>
#include <WinSock2.h>
#include "../Settings.hpp"
int ParseAndSend(SOCKET Client, std::string Data){
std::string Response, Packet;
char ID = Data.at(0);
int Prev = 0,DataSent = 0, Size = 0;
bool FileSent = true;
switch (ID){
case 'a' :
Response = FileList;
break;
case 'b' :
FileSent = false;
break;
}
std::string FLocation = Data.substr(1);
if(FileList.find(FLocation) == std::string::npos)return -1;
do {
if(!FileSent){
std::ifstream f;
f.open(FLocation.c_str(), std::ios::binary);
if(f.good()){
if(!Size){
Size = f.seekg(0, std::ios_base::end).tellg();
Response.resize(Size);
f.seekg(0, std::ios_base::beg);
f.read(&Response[0], Size);
f.close();
}else{f.close();}
if(DataSent != Size){
if((Size-DataSent) < 65535){
Packet = Response.substr(Prev,(Size-DataSent));
DataSent += (Size-DataSent);
Response.clear();
}else{
DataSent += 65535;
Packet = Response.substr(Prev,65535);
}
Prev = DataSent;
}else{
Size = DataSent = Prev = 0;
Response = "End of file";
FileSent = true;
Packet.clear();
}
}else{
FileSent = true;
Response = "Cannot Open File " + FLocation;
}
}
int iSendResult;
if(!Packet.empty())iSendResult = send(Client, Packet.c_str(), Packet.length(), 0);
else iSendResult = send(Client, Response.c_str(), Response.length(), 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(Client);
return -1;
}
}while(!FileSent);
return 0;
}
void Client(void* ClientData){
SOCKET Client = *(SOCKET*)ClientData;
printf("Client connected\n");
int iResult, iSendResult;
char recvbuf[65535];
int recvbuflen = 65535;
do {
iResult = recv(Client, recvbuf, recvbuflen, 0);
if (iResult > 0) {
//printf("Bytes received: %d\n", iResult);
std::string Data = recvbuf,Response;
Data.resize(iResult);
if(ParseAndSend(Client,Data) == -1)break;
// Echo the buffer back to the sender
/*iSendResult = send(Client, Response.c_str(), Response.length(), 0);
if (iSendResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(Client);
return;
}
printf("Bytes sent: %d\n", iSendResult);*/
}
else if (iResult == 0)
printf("Connection closing...\n");
else {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(Client);
break;
}
} while (iResult > 0);
std::cout << "Client Closed" << std::endl;
}
void CreateNewThread(void* ClientData){
std::cout << "New Client" << std::endl;
std::thread NewClient(Client,ClientData);
NewClient.detach();
}