Skip to content

Commit 2432f68

Browse files
author
Igor
committed
communication development
1 parent a6a012d commit 2432f68

12 files changed

+336
-275
lines changed

.idea/tokenwizard.iml

+14-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+239-191
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BaseHeader.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ using std::endl;
2727
#include <boost/make_shared.hpp>
2828
#include <boost/asio.hpp>
2929

30-
#include "../../concurrent-guard/cg_shared_ptr.h"
30+
#include "concurrent-guard/cg_shared_ptr.h"
3131

3232
using boost::make_shared;
3333
using boost::asio::ip::tcp;
3434

3535

36-
#endif //TOKENWIZARD_BASEHEADER_H
36+
#endif //TOKENWIZARD_BASEHEADER_H

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ project(tokenwizard)
44
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -O0")
55

66
FIND_PACKAGE( Boost COMPONENTS system filesystem REQUIRED )
7-
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
7+
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} "/work/")
88
LINK_DIRECTORIES(convserv /usr/local/ssl/lib)
99
list(APPEND CMAKE_MODULE_PATH "")
1010

CommandHandlers/GetTokenCommandHandler.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,25 @@
33
//
44

55
#include "GetTokenCommandHandler.h"
6+
#include "../UnorderedTokenMap.h"
7+
#include "../TcpSession.h"
8+
9+
extern Packet tokenDoesNotExistsPacket;
10+
11+
void GetTokenCommandHandler::Handle(char *bytes, size_t size, TcpSession &sessionContext) {
12+
//Deserialize
13+
cout << "GetToken" << endl;
14+
char* token = bytes + 1;
15+
cout << token << endl;
16+
17+
//Handle
18+
auto ptr = tokenMap.try_get(token);
19+
if(ptr) {
20+
cout << "token exists" << endl;
21+
sessionContext.SendPacket<decltype(ptr)>(ptr->data, move(ptr));
22+
}
23+
else {
24+
cout << "Unknown token" << endl;
25+
sessionContext.SendPacket(&tokenDoesNotExistsPacket);
26+
}
27+
}
+7-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#ifndef TOKENWIZARD_GETTOKENCOMMANDHANDLER_H
22
#define TOKENWIZARD_GETTOKENCOMMANDHANDLER_H
33

4-
54
#include "../CommandHandler.h"
65
#include "../BaseHeader.h"
6+
#include "../Definitions.h"
7+
8+
template <int tokenLength, int mapSizeExponent>
9+
class UnorderedTokenMap;
10+
11+
extern UnorderedTokenMap<TOKEN_LENGTH, 5> tokenMap;
712

813
class GetTokenCommandHandler: public CommandHandler {
914
public:
10-
void Handle(char* bytes, size_t size, TcpSession& sessionContext) {
11-
cout << "GetToken" << endl;
12-
char* token = bytes + 1;
13-
cout << token << endl;
14-
}
15+
void Handle(char* bytes, size_t size, TcpSession& sessionContext);
1516
};
1617

17-
1818
#endif //TOKENWIZARD_GETTOKENCOMMANDHANDLER_H

HandlerSelector.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class HandlerSelector {
1717
assignedHandlers[(unsigned char)token] = handlerPtr;
1818
}
1919

20-
CommandHandler* getParser(char token) {
20+
CommandHandler* getHandler(char token) {
2121
assert(assignedHandlers[(unsigned char)token] != nullptr);
2222
return assignedHandlers[(unsigned char)token];
2323
}

TcpSession.cpp

+1-13
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ TcpSession::~TcpSession() {
1212
cout << "Session has been destroyed!" << endl;
1313
}
1414

15-
template <class T>
16-
void TcpSession::SendPacket(Packet* packet, T&& smart_pointer){
17-
auto self = shared_from_this();
18-
boost::asio::async_write(mSocket, boost::asio::buffer(packet->data(), packet->size()),
19-
[self, packet, smart_pointer](boost::system::error_code ec, std::size_t l)
20-
{
21-
if(!ec) {
22-
cout << "Sent!" << endl;
23-
}
24-
});
25-
}
26-
2715
void TcpSession::BeginCommunication() {
28-
do_read();
16+
read_loop();
2917
}

TcpSession.h

+38-39
Original file line numberDiff line numberDiff line change
@@ -12,81 +12,80 @@
1212
extern HandlerSelector handlerSelector;
1313

1414
class TcpSession: public std::enable_shared_from_this<TcpSession> {
15-
boost::asio::io_service& mIoService;
15+
boost::asio::io_service &mIoService;
1616
tcp::socket mSocket;
1717
enum {
1818
maxLength = 65536
1919
};
2020
char mData[maxLength];
2121

22-
void HandleCmd(char* bytes, size_t size) {
23-
auto handlerPtr = handlerSelector.getParser(*bytes);
22+
void handlePacket(char *bytes, size_t size) {
23+
auto handlerPtr = handlerSelector.getHandler(*bytes);
2424
assert(handlerPtr);
25-
if(handlerPtr)
25+
if (handlerPtr)
2626
handlerPtr->Handle(bytes, size, *this);
2727
}
2828

29-
char* writeOffset;
30-
char* readOffset;
31-
29+
char *writeOffset;
30+
char *readOffset;
3231
size_t currSize = 0;
3332

34-
void HandleRead(size_t written) {
33+
void readHandler(size_t written) {
3534
currSize += written;
3635
writeOffset += written;
37-
while(true) {
36+
while (true) {
3837
uint32_t packetSize = fixEndianness(*reinterpret_cast<uint32_t *>(readOffset));
3938
if ((packetSize + 4) <= currSize) {
40-
HandleCmd(readOffset + 4, packetSize);
39+
handlePacket(readOffset + 4, packetSize);
4140
readOffset += 4 + packetSize;
4241
currSize -= 4 + packetSize;
43-
}
44-
else
42+
} else
4543
break;
4644
}
47-
if(currSize) {
45+
if (currSize) {
4846
memmove(mData, readOffset, currSize);
4947
}
5048
readOffset = mData;
51-
writeOffset = readOffset+currSize;
49+
writeOffset = readOffset + currSize;
5250
}
5351

54-
void do_read()
55-
{
52+
void read_loop() {
5653
auto self(shared_from_this());
5754
mSocket.async_read_some(boost::asio::buffer(writeOffset, maxLength - currSize),
58-
[this, self](boost::system::error_code ec, std::size_t length)
59-
{
60-
if (!ec)
61-
{
62-
HandleRead(length);
63-
do_read();
55+
[this, self](boost::system::error_code ec, std::size_t length) {
56+
if (!ec) {
57+
readHandler(length);
58+
read_loop();
6459
}
6560
});
6661
}
6762

6863

6964
public:
70-
template <class T>
71-
void SendPacket(Packet* packet, T&& smart_pointer);
65+
template<class T>
66+
void SendPacket(Packet *packet, T &&smart_pointer) {
67+
auto self = shared_from_this();
68+
boost::asio::async_write(mSocket, boost::asio::buffer(packet -> data(), packet->size()),
69+
[self, packet, smart_pointer](boost::system::error_code ec, std::size_t l)
70+
{
71+
if (!ec) {
72+
cout << "Sent!" << endl;
73+
}
74+
});
75+
}
76+
void SendPacket(Packet *packet) {
77+
auto self = shared_from_this();
78+
boost::asio::async_write(mSocket, boost::asio::buffer(packet -> data(), packet->size()),
79+
[self, packet](boost::system::error_code ec, std::size_t l)
80+
{
81+
if (!ec) {
82+
cout << "Sent!" << endl;
83+
}
84+
});
85+
}
7286
TcpSession(tcp::socket socket, boost::asio::io_service& _io_service);
7387
void BeginCommunication();
7488
~TcpSession();
7589
};
7690

77-
/*
78-
*
79-
void do_write(std::size_t length)
80-
{
81-
auto self(shared_from_this());
82-
boost::asio::async_write(mSocket, boost::asio::buffer(mData, length),
83-
[this, self](boost::system::error_code ec, std::size_t )
84-
{
85-
if (!ec)
86-
{
87-
do_read();
88-
}
89-
});
90-
}
91-
* */
9291
#endif //TOKENWIZARD_SESSION_H

UnorderedTokenMap.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ class UnorderedTokenMap {
4848

4949
cg_shared_ptr<Token<tokenLength>> try_get(char* token) {
5050
uint32_t offset = Int32Encoder::decode64Based(token);
51-
return table[offset].try_get();
51+
if(offset >= mSize)
52+
return nullptr;
53+
auto res = table[offset].try_get();
54+
if (res) {
55+
if(strcmp(token, res->token) != 0)
56+
res = nullptr;
57+
}
58+
return res;
5259
}
5360
};
5461

main.cpp

+2-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
bool myEndianness = detectEndianness();
1414

15-
1615
boost::asio::io_service primary_io_service;
1716

1817
UnorderedTokenMap<TOKEN_LENGTH, 5> tokenMap;
@@ -24,16 +23,9 @@ Packet tokenDoesNotExistsPacket((char)ServerResponse::tokenDoesNotExists);
2423
volatile char c;
2524
int main(int argc, char* argv[]) {
2625
handlerSelector.assignHandler((char)ClientCommand::getToken, new GetTokenCommandHandler());
27-
/*
28-
* cout << time(0) << endl<<endl;
2926
string data = "lalkasffsdfsdfsadfsdaklfjsalkfjlsdajflsdkjfkldf";
30-
for(int i = 0; i < 1*1000*1000*100; ++i) {
31-
auto res = tokenMap.genToken(data.c_str(), data.size());
32-
//cout << res->token[0] << endl;
33-
}
34-
cout << c << "time " << time(0) << endl;
35-
return 0;
36-
*/
27+
auto res = tokenMap.genToken(data.c_str(), data.size());
28+
cout << res->token << endl;
3729

3830
try {
3931
if (argc != 2) {

misc/IntPow.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ inline int int_pow(int base, int exp)
1414
return result;
1515
}
1616

17-
#endif //TOKENWIZARD_INTPOW_H
17+
#endif //TOKENWIZARD_INTPOW_H

0 commit comments

Comments
 (0)