Skip to content

Commit d32f603

Browse files
committed
New TextProtocol system
1 parent 179bb38 commit d32f603

File tree

10 files changed

+119
-96
lines changed

10 files changed

+119
-96
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ project(MogNetwork)
44

55
set(CMAKE_BUILD_TYPE Release)
66

7-
set(CMAKE_CXX_FLAGS "-g3 -W -Wall -pedantic -ansi -fPIC -m32")
7+
set(CMAKE_CXX_FLAGS "-g3 -W -Wall -pedantic -ansi -fPIC")
88

99
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
1010

1111
set(SOURCES
1212
src/AProtocolListener.cpp
1313
src/BinaryProtocol.cpp
14-
src/LineProtocol.cpp
14+
src/TextProtocol.cpp
1515
src/UnixCondVar.cpp
1616
src/WinCondVar.cpp
1717
src/IpAddress.cpp

include/mognetwork/LineProtocolFactory.hpp

Lines changed: 0 additions & 28 deletions
This file was deleted.

include/mognetwork/LineProtocol.hh renamed to include/mognetwork/TextProtocol.hh

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
66
//
77
// Started on Fri Apr 17 15:34:03 2015 Moghrabi Alexandre
8-
// Last update Tue Apr 28 10:53:49 2015 Moghrabi Alexandre
8+
// Last update Tue May 5 12:55:24 2015 Moghrabi Alexandre
99
//
1010

1111
/*!
12-
* \file LineProtocol
13-
* \brief Line Protocol of the library
12+
* \file TextProtocol
13+
* \brief Text Protocol of the library
1414
* \author AlexMog
1515
* \version 0.2
1616
*/
1717

18-
#ifndef MOGNETWORK_LINEPROTOCOL_HH
19-
# define MOGNETWORK_LINEPROTOCOL_HH
18+
#ifndef MOGNETWORK_TEXTPROTOCOL_HH
19+
# define MOGNETWORK_TEXTPROTOCOL_HH
2020

2121
# include <iostream>
2222
# include "AProtocolListener.hh"
@@ -26,31 +26,28 @@ namespace mognetwork
2626
namespace protocol
2727
{
2828
/*!
29-
* \class LineProtocol
29+
* \class TextProtocol
3030
* \brief Defines and execute the Binary protocol
3131
*/
32-
class LineProtocol : public AProtocolListener
32+
class TextProtocol : public AProtocolListener
3333
{
3434
public:
35-
LineProtocol(TcpSocket* socket);
36-
virtual ~LineProtocol();
35+
TextProtocol(TcpSocket* socket, char* separator);
36+
virtual ~TextProtocol();
3737

3838
public:
3939
virtual Socket::Status onReadTrigger();
4040
virtual bool datasFullyReceived();
4141
virtual void onSendDatas(const char* data, std::size_t size, TcpSocket::Data& dataToSend);
4242
virtual Socket::Status onReadAllTrigger(TcpSocket::Data& data);
43-
virtual TcpSocket::ReadedDatas& getReadedDatas() {return *m_fullDatas;}
44-
virtual void flushReader()
45-
{
46-
m_pendingDatas = TcpSocket::ReadedDatas();
47-
m_fullDatas = NULL;
48-
}
43+
virtual TcpSocket::ReadedDatas& getReadedDatas();
44+
virtual void flushReader();
4945

5046
private:
5147
TcpSocket::ReadedDatas* m_fullDatas;
48+
char* m_separator;
5249
};
5350
}
5451
}
5552

56-
#endif /* !MOGNETWORK_LINEPROTOCOL_HH */
53+
#endif /* !MOGNETWORK_TEXTPROTOCOL_HH */
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// BinaryProtocolFactory.hh for BinaryProtocolFactory in /home/alexmog/projets/LibNet
3+
//
4+
// Made by Moghrabi Alexandre
5+
6+
//
7+
// Started on Fri Apr 17 17:20:30 2015 Moghrabi Alexandre
8+
// Last update Tue May 5 12:29:50 2015 Moghrabi Alexandre
9+
//
10+
11+
#ifndef MOGNETWORK_TEXTPROTOCOLFACTORY_HPP
12+
# define MOGNETWORK_TEXTPROTOCOLFACTORY_HPP
13+
14+
# include "TextProtocol.hh"
15+
# include "IProtocolFactory.hh"
16+
17+
namespace mognetwork
18+
{
19+
class TextProtocolFactory : public IProtocolFactory
20+
{
21+
public:
22+
TextProtocolFactory(char* separator) : m_separator(separator) {}
23+
24+
public:
25+
virtual protocol::AProtocolListener* getNewObject(TcpSocket* socket)
26+
{
27+
return (new protocol::TextProtocol(socket, m_separator));
28+
}
29+
30+
private:
31+
char* m_separator;
32+
};
33+
}
34+
#endif /* !MOGNETWORK_TEXTPROTOCOLFACTORY_HPP */

src/TcpASIOServer.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
66
//
77
// Started on Mon Nov 17 17:38:14 2014 Moghrabi Alexandre
8-
// Last update Mon Apr 20 15:47:49 2015 Moghrabi Alexandre
8+
// Last update Tue May 5 12:36:22 2015 Moghrabi Alexandre
99
//
1010

1111
#include "mognetwork/Mutex.hh"
1212
#include "mognetwork/TcpASIOServer.hh"
1313
#include "mognetwork/BinaryProtocolFactory.hpp"
14-
#include "mognetwork/LineProtocolFactory.hpp"
14+
#include "mognetwork/TextProtocolFactory.hpp"
1515

1616
namespace mognetwork
1717
{
@@ -21,10 +21,12 @@ namespace mognetwork
2121
if (protocolType == Binary)
2222
m_protocolFactory = new BinaryProtocolFactory;
2323
else if (protocolType == LinePerLine)
24-
m_protocolFactory = new LineProtocolFactory;
24+
m_protocolFactory = new TextProtocolFactory((char*)"\n");
25+
else if (protocolType == Telnet)
26+
m_protocolFactory = new TextProtocolFactory((char*)"\r\n");
2527
else
2628
m_protocolFactory = NULL;
27-
init();
29+
init();
2830
}
2931

3032
void TcpASIOServer::init()

src/LineProtocol.cpp renamed to src/TextProtocol.cpp

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,38 @@
55
66
//
77
// Started on Mon Apr 20 02:40:36 2015 Moghrabi Alexandre
8-
// Last update Tue Apr 28 10:53:32 2015 Moghrabi Alexandre
8+
// Last update Tue May 5 13:07:10 2015 Moghrabi Alexandre
99
//
1010

1111
#include <cstring>
1212
#include <iostream>
1313
#include "mognetwork/TcpASIOServer.hh"
1414
#include "mognetwork/TcpASIOListener.hh"
15-
#include "mognetwork/LineProtocol.hh"
15+
#include "mognetwork/TextProtocol.hh"
1616

1717
namespace mognetwork
1818
{
1919
namespace protocol
2020
{
21-
LineProtocol::LineProtocol(TcpSocket* socket) :
22-
AProtocolListener(socket), m_fullDatas(NULL) {}
21+
TextProtocol::TextProtocol(TcpSocket* socket, char* separator) :
22+
AProtocolListener(socket), m_fullDatas(NULL), m_separator(separator) {}
2323

24-
LineProtocol::~LineProtocol() {}
24+
TextProtocol::~TextProtocol() {}
2525

26-
Socket::Status LineProtocol::onReadTrigger()
26+
void TextProtocol::flushReader()
27+
{
28+
m_pendingDatas = TcpSocket::ReadedDatas();
29+
if (m_fullDatas != NULL)
30+
delete m_fullDatas;
31+
m_fullDatas = NULL;
32+
}
33+
34+
TcpSocket::ReadedDatas& TextProtocol::getReadedDatas()
35+
{
36+
return *m_fullDatas;
37+
}
38+
39+
Socket::Status TextProtocol::onReadTrigger()
2740
{
2841
std::size_t readed;
2942
char buffer[1024];
@@ -33,7 +46,6 @@ namespace mognetwork
3346
return status;
3447
buffer[readed++] = 0;
3548
m_pendingDatas.datas.resize(m_pendingDatas.datas.size() + readed);
36-
// char* begin = &m_pendingDatas.datas[0] + m_pendingDatas.datas.size() - readed;
3749
std::strcat(&m_pendingDatas.datas[0], buffer);
3850
char* l;
3951
while ((l = std::strstr(&m_pendingDatas.datas[0], "\n")) != NULL)
@@ -54,59 +66,66 @@ namespace mognetwork
5466
for (int i = 0; i < size + 1; ++i)
5567
m_pendingDatas.datas.erase(m_pendingDatas.datas.begin());
5668
m_socket->setAllDataReaded(m_fullDatas);
57-
for (std::list<ITcpASIOListenerHandler*>::iterator it = m_socket->getServer()->getServerListener()->getListeners().begin(); it != m_socket->getServer()->getServerListener()->getListeners().end(); ++it)
69+
for (std::list<ITcpASIOListenerHandler*>::iterator it = m_socket->getServer()->getServerListener()->getListeners().begin();
70+
it != m_socket->getServer()->getServerListener()->getListeners().end();
71+
++it)
5872
(*it)->onReceivedData(*m_socket);
5973
m_fullDatas = NULL;
6074
}
6175
return Socket::Waiting;
6276
}
6377

64-
bool LineProtocol::datasFullyReceived()
78+
bool TextProtocol::datasFullyReceived()
6579
{
6680
return m_fullDatas != NULL
6781
&& m_fullDatas->datas.size() > 0
6882
&& std::strstr(&m_fullDatas->datas[0], "\n") != NULL;
6983
}
7084

71-
void LineProtocol::onSendDatas(const char* data, std::size_t size, TcpSocket::Data& dataToSend)
85+
void TextProtocol::onSendDatas(const char* data, std::size_t size, TcpSocket::Data& dataToSend)
7286
{
7387
dataToSend.resize(size + 1);
7488
std::memcpy(&dataToSend.front(), data, size);
7589
std::memcpy(&dataToSend.front() + size, "\n", 1);
7690
}
7791

78-
Socket::Status LineProtocol::onReadAllTrigger(TcpSocket::Data& data)
92+
Socket::Status TextProtocol::onReadAllTrigger(TcpSocket::Data& data)
7993
{
80-
/* std::size_t readed;
81-
char buffer[1024];
82-
char* l;
83-
std::vector<char> _readed;
84-
85-
while ((l = std::strstr(&m_pendingDatas.datas[0], "\n")) == NULL)
94+
bool received = false;
95+
96+
while (!received)
8697
{
87-
Socket::Status status = m_socket->receive(buffer, 1024, readed, 0);
98+
std::size_t readed;
99+
char buffer[1024];
100+
101+
Socket::Status status = m_socket->receive(buffer, 1023, readed, 0);
88102
if (status != Socket::Ok)
89103
return status;
90-
_readed.resize(_readed.size() + readed);
91-
char* begin = &_readed[0] + _readed.size() - readed;
92-
std::memcpy(begin, buffer, readed);
104+
buffer[readed++] = 0;
105+
m_pendingDatas.datas.resize(m_pendingDatas.datas.size() + readed);
106+
std::strcat(&m_pendingDatas.datas[0], buffer);
107+
char* l;
108+
if ((l = std::strstr(&m_pendingDatas.datas[0], "\n")) != NULL)
109+
{
110+
int size = l - &m_pendingDatas.datas[0];
111+
if (size == 0)
112+
{
113+
m_pendingDatas.datas.clear();
114+
break;
115+
}
116+
117+
data.resize(l - &m_pendingDatas.datas[0] + 2);
118+
std::memcpy(&data[0],
119+
&m_pendingDatas.datas[0],
120+
l - &m_pendingDatas.datas[0]);
121+
data[data.size() - 1] = 0;
122+
if (m_pendingDatas.datas.size() != size + 2)
123+
for (int i = 0; i < size + 1; ++i)
124+
m_pendingDatas.datas.erase(m_pendingDatas.datas.begin());
125+
received = true;
126+
}
93127
}
94-
if (l != NULL)
95-
{
96-
size_t tmp = m_fullDatas.datas.size();
97-
m_fullDatas.datas.resize(m_fullDatas.datas.size() +
98-
l - &m_pendingDatas.datas[0] + 2);
99-
std::memcpy(&m_fullDatas.datas[0] + tmp,
100-
&m_pendingDatas.datas[0],
101-
l - &m_pendingDatas.datas[0] + 1);
102-
m_fullDatas.datas[m_fullDatas.datas.size() - 1] = 0;
103-
if (m_pendingDatas.datas.size() != l - &m_pendingDatas.datas[0] + 2)
104-
m_pendingDatas.datas.erase(m_pendingDatas.datas.begin(), m_pendingDatas.datas.begin() + strlen(l) - 1);
105-
else
106-
m_pendingDatas.datas.clear();
107-
return Socket::Ok;
108-
}*/
109-
return Socket::Error;
128+
return Socket::Ok;
110129
}
111130
}
112131
}
-10.7 KB
Binary file not shown.

test/server_client_line_communication/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ class Listener : public mognetwork::ITcpASIOListenerHandler
1919
Listener(mognetwork::TcpASIOWriter* writer) : m_writer(writer) {}
2020
void onConnect(mognetwork::TcpSocket& client)
2121
{
22-
std::cout << "New client connected." << std::endl;client.asyncSend("LOL\n", 4);
23-
m_writer->triggerData();
22+
std::cout << "New client connected." << std::endl;
2423
}
2524

2625
void onReceivedData(mognetwork::TcpSocket& client)
2726
{
2827
mognetwork::TcpSocket::ReadedDatas* datas = client.getDatasReaded();
2928

3029
std::cout << "Received: " << &datas->datas[0] << std::endl;
30+
client.asyncSend("LOL\n\0", 5);
31+
m_writer->triggerData();
3132
delete datas;
32-
//delete datas;
3333
}
3434

3535
void onDisconnect(mognetwork::TcpSocket& client) {std::cout << "Client disconnected." << std::endl;}

test/server_client_line_communication/main2.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <mognetwork/Packet.hh>
22
#include <mognetwork/TcpSocket.hh>
3-
#include <mognetwork/LineProtocol.hh>
3+
#include <mognetwork/TextProtocol.hh>
44
#include <stdio.h>
55
#include <iostream>
66
#include <exception>
@@ -12,7 +12,7 @@
1212
int main(int ac, char **av)
1313
{
1414
mognetwork::TcpSocket socket;
15-
socket.setProtocolListener(new mognetwork::protocol::LineProtocol(&socket));
15+
socket.setProtocolListener(new mognetwork::protocol::TextProtocol(&socket, (char*)"\n"));
1616
mognetwork::IpAddress ip("127.0.0.1");
1717
int i = NUMBER_OF_PACKET_SEND;
1818
struct timeval t1, t2;
@@ -30,19 +30,18 @@ int main(int ac, char **av)
3030
memset(buffer, 0, sizeof(buffer));
3131
socket.send("c\nb", 4);
3232
gettimeofday(&t1, NULL);
33-
/* if (socket.receiveAll(*datas) != mognetwork::Socket::Ok)
33+
if (socket.receiveAll(*datas) != mognetwork::Socket::Ok)
3434
{
3535
std::cout << "ERROR" << std::endl;
3636
perror("WTF?");
3737
return 1;
38-
}*/
38+
}
3939
gettimeofday(&t2, NULL);
4040
latency = ((double) (t2.tv_usec - t1.tv_usec) / 1000 +
4141
(double) (t2.tv_sec - t1.tv_sec));
42+
std::cout << "RECEIVED: S: '" << datas->size() << "' D: '" << (&(*datas)[0]) << "'" << std::endl;
4243
std::cout << "Lattency: " << latency << "ms" << std::endl;
4344
totalTime += latency;
44-
// mognetwork::Packet p(datas);
45-
// std::cout << "RECEIVED: S: '" << datas->size() << "' D: '" << (&(*datas)[0]) << "'" << std::endl;
4645
i--;
4746
delete datas;
4847
sleep(1);
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)