Pushed code across

This commit is contained in:
Fred Nicolson 2016-12-06 23:07:54 +00:00
parent 2502e2e7a0
commit 4f9502df23
12 changed files with 293 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
cmake-build-debug

8
CMakeLists.txt Normal file
View File

@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.6)
project(frnetlib)
include_directories(include)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp src/TcpSocket.cpp include/TcpSocket.h src/TcpListener.cpp include/TcpListener.h src/Socket.cpp include/Socket.h src/Packet.cpp include/Packet.h include/NetworkEncoding.h)
add_executable(frnetlib ${SOURCE_FILES})

51
include/NetworkEncoding.h Normal file
View File

@ -0,0 +1,51 @@
//
// Created by fred on 06/12/16.
//
#ifndef FRNETLIB_NETWORKENCODING_H
#define FRNETLIB_NETWORKENCODING_H
#include <netinet/in.h>
#include <cstring>
#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
inline float htonf(float val)
{
uint32_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = htonl(ret);
memcpy(&val, &ret, sizeof(val));
return val;
}
inline float ntohf(float val)
{
uint32_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = ntohl(ret);
memcpy(&val, &ret, sizeof(val));
return val;
}
inline double htond(double val)
{
uint64_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = htonll(ret);
memcpy(&val, &ret, sizeof(val));
return val;
}
inline double ntohd(double val)
{
uint64_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = ntohll(ret);
memcpy(&val, &ret, sizeof(val));
return val;
}
#endif //FRNETLIB_NETWORKENCODING_H

139
include/Packet.h Normal file
View File

@ -0,0 +1,139 @@
//
// Created by fred on 06/12/16.
//
#ifndef FRNETLIB_PACKET_H
#define FRNETLIB_PACKET_H
#include <string>
#include <netinet/in.h>
#include <cstring>
#include "NetworkEncoding.h"
class Packet
{
public:
/*!
* Gets the data added to the packet
*
* @return A string containing all of the data added to the packet
*/
inline const std::string &construct_packet()
{
return buffer;
}
/*
* Adds a 16bit variable to the packet
*/
inline Packet &operator<<(uint16_t var)
{
buffer.resize(buffer.size() + sizeof(var));
var = htons(var);
memcpy(&buffer[buffer.size() - sizeof(var)], &var, sizeof(var));
return *this;
}
/*
* Extracts a 16bit variable from the packet
*/
inline Packet &operator>>(uint16_t &var)
{
memcpy(&var, &buffer[0], sizeof(var));
buffer.erase(0, sizeof(var));
var = ntohs(var);
return *this;
}
/*
* Adds a 32bit variable to the packet
*/
inline Packet &operator<<(uint32_t var)
{
buffer.resize(buffer.size() + sizeof(var));
var = htonl(var);
memcpy(&buffer[buffer.size() - sizeof(var)], &var, sizeof(var));
return *this;
}
/*
* Extracts a 32bit variable from the packet
*/
inline Packet &operator>>(uint32_t &var)
{
memcpy(&var, &buffer[0], sizeof(var));
buffer.erase(0, sizeof(var));
var = ntohl(var);
return *this;
}
/*
* Adds a 64bit variable to the packet
*/
inline Packet &operator<<(uint64_t var)
{
buffer.resize(buffer.size() + sizeof(var));
var = htonll(var);
memcpy(&buffer[buffer.size() - sizeof(var)], &var, sizeof(var));
return *this;
}
/*
* Extracts a 64bit variable from the packet
*/
inline Packet &operator>>(uint64_t &var)
{
memcpy(&var, &buffer[0], sizeof(var));
buffer.erase(0, sizeof(var));
var = ntohll(var);
return *this;
}
/*
* Adds a float variable to the packet
*/
inline Packet &operator<<(float var)
{
buffer.resize(buffer.size() + sizeof(var));
var = htonf(var);
memcpy(&buffer[buffer.size() - sizeof(var)], &var, sizeof(var));
return *this;
}
/*
* Extracts a float variable from the packet
*/
inline Packet &operator>>(float &var)
{
memcpy(&var, &buffer[0], sizeof(var));
buffer.erase(0, sizeof(var));
var = ntohf(var);
return *this;
}
/*
* Adds a double variable to the packet
*/
inline Packet &operator<<(double var)
{
buffer.resize(buffer.size() + sizeof(var));
var = htond(var);
memcpy(&buffer[buffer.size() - sizeof(var)], &var, sizeof(var));
return *this;
}
/*
* Extracts a double variable from the packet
*/
inline Packet &operator>>(double &var)
{
memcpy(&var, &buffer[0], sizeof(var));
buffer.erase(0, sizeof(var));
var = ntohd(var);
return *this;
}
private:
std::string buffer; //Packet data buffer
};
#endif //FRNETLIB_PACKET_H

17
include/Socket.h Normal file
View File

@ -0,0 +1,17 @@
//
// Created by fred on 06/12/16.
//
#ifndef FRNETLIB_SOCKET_H
#define FRNETLIB_SOCKET_H
class Socket
{
public:
private:
};
#endif //FRNETLIB_SOCKET_H

15
include/TcpListener.h Normal file
View File

@ -0,0 +1,15 @@
//
// Created by fred on 06/12/16.
//
#ifndef FRNETLIB_TCPLISTENER_H
#define FRNETLIB_TCPLISTENER_H
class TcpListener
{
};
#endif //FRNETLIB_TCPLISTENER_H

17
include/TcpSocket.h Normal file
View File

@ -0,0 +1,17 @@
//
// Created by fred on 06/12/16.
//
#ifndef FRNETLIB_TCPSOCKET_H
#define FRNETLIB_TCPSOCKET_H
namespace fr
{
class TcpSocket
{
};
}
#endif //FRNETLIB_TCPSOCKET_H

19
main.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <iostream>
#include <Packet.h>
int main()
{
Packet packet;
packet << (uint16_t)15000 << (uint16_t)200 << (uint32_t)9299221 << (uint64_t)9223372036854775807 << (float)1.22 << (double)192.212;
std::cout << packet.construct_packet() << std::endl;
uint16_t result, result2;
uint32_t result3;
uint64_t result4;
float result5;
double result6;
packet >> result >> result2 >> result3 >> result4 >> result5 >> result6;
std::cout << result << ", " << result2 << ", " << result3 << ", " << result4 << ", " << result5 << ", " << result6 << std::endl;
return 0;
}

5
src/Packet.cpp Normal file
View File

@ -0,0 +1,5 @@
//
// Created by fred on 06/12/16.
//
#include "Packet.h"

5
src/Socket.cpp Normal file
View File

@ -0,0 +1,5 @@
//
// Created by fred on 06/12/16.
//
#include "Socket.h"

5
src/TcpListener.cpp Normal file
View File

@ -0,0 +1,5 @@
//
// Created by fred on 06/12/16.
//
#include "TcpListener.h"

10
src/TcpSocket.cpp Normal file
View File

@ -0,0 +1,10 @@
//
// Created by fred on 06/12/16.
//
#include "TcpSocket.h"
namespace fr
{
}