Skip to content

Commit 02a50ad

Browse files
committed
First working state
1 parent ffbf60a commit 02a50ad

File tree

7 files changed

+76
-27
lines changed

7 files changed

+76
-27
lines changed

client/MainConsole.cpp

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,35 @@ namespace simpleApp
6565
SessionClient * currentSession = nullptr;
6666
console_state currentState;
6767

68-
auto switchState = [&currentState, &currentSession, &isExit](console_state newState)
68+
auto switchState = [&currentState, &currentSession](console_state newState)
6969
{
7070
// TODO
71+
72+
// TODO STDIN clean up
73+
74+
switch (newState)
75+
{
76+
case console_state::protocol_selection:
77+
if (currentSession != nullptr)
78+
{
79+
delete currentSession;
80+
currentSession = nullptr;
81+
}
82+
std::cout << "Select protocol: [u]dp or [t]cp" << std::endl <<
83+
" >> " << std::flush;
84+
break;
85+
86+
case console_state::address_input:
87+
std::cout << "Input server IP-address" << std::endl <<
88+
" >> " << std::flush;
89+
break;
90+
case console_state::connected:
91+
std::cout << "Connected. Input message" << std::endl <<
92+
" >> " << std::flush;
93+
break;
94+
}
95+
96+
currentState = newState;
7197
};
7298

7399
switchState(console_state::protocol_selection);
@@ -97,7 +123,7 @@ namespace simpleApp
97123
return -1;
98124
}
99125

100-
if (currentSession != nullptr)
126+
if (currentState == console_state::connected)
101127
{
102128
if (selectResult == 0)
103129
{
@@ -174,26 +200,26 @@ namespace simpleApp
174200

175201
if (FD_ISSET(STDIN_FILENO, &fd_in))
176202
{
177-
char buffer[MESSAGE_MAX_BUFFER - sizeof(msg_headers)];
203+
std::string line;
204+
std::getline(std::cin, line);
205+
206+
int len = line.size();
207+
const char* linePtr = line.c_str();
178208

179-
int len = 0;
180-
// TODO read from stdin
181-
// len = read(stdin, buffer);
182-
183209
if (len == 0)
184210
{
185211
std::cout << " >> " << std::flush;
186212
}
187213
else if (currentState == console_state::protocol_selection)
188214
{
189-
if (len != 1 || (buffer[0] != 'u' && buffer[0] != 't'))
215+
if (len != 1 || (linePtr[0] != 'u' && linePtr[0] != 't'))
190216
{
191217
std::cout << "Invalid input. [u]dp or [t]cp?" << std::endl <<
192218
" >> " << std::flush;
193219
}
194220
else
195221
{
196-
if (buffer[0] == 'u')
222+
if (linePtr[0] == 't')
197223
currentSession = new SessionTcp();
198224
else
199225
currentSession = new SessionUdp();
@@ -203,7 +229,7 @@ namespace simpleApp
203229
}
204230
else if (currentState == console_state::address_input)
205231
{
206-
auto address = inet_addr(buffer);
232+
auto address = inet_addr(linePtr);
207233

208234
if (address == INADDR_NONE)
209235
{
@@ -235,6 +261,9 @@ namespace simpleApp
235261
case session_status::recv_wrong_header:
236262
std::cout << "Received message corrupted";
237263
break;
264+
case session_status::server_error:
265+
std::cout << "Error on server-side";
266+
break;
238267
default:
239268
std::cout << "Unknown error";
240269
break;
@@ -251,7 +280,7 @@ namespace simpleApp
251280
}
252281
else if (currentState == console_state::connected)
253282
{
254-
auto result = currentSession->proceed(buffer, len);
283+
auto result = currentSession->proceed(linePtr, len);
255284
if (result.status == session_status::proceed_msg)
256285
{
257286
std::cout << "Received answer: '" << result.recvMsg << "'" << std::endl <<
@@ -283,10 +312,19 @@ namespace simpleApp
283312

284313
switchState(console_state::protocol_selection);
285314
}
286-
}
315+
}
316+
317+
// TODO clear cin
287318
}
288319
}
289320

321+
if (currentSession != nullptr)
322+
{
323+
delete currentSession;
324+
}
325+
326+
// TODO clear cin
327+
290328
return 0;
291329
}
292330
}

client/SessionClient.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace simpleApp
5353
return session_result(session_status::init_success);
5454
}
5555

56-
session_result SessionClient::proceed(char* sendMsg, size_t msgSize)
56+
session_result SessionClient::proceed(const char* sendMsg, size_t msgSize)
5757
{
5858
if(sendMsg == nullptr || msgSize == 0)
5959
{
@@ -89,7 +89,7 @@ namespace simpleApp
8989
}
9090
else if (sendMsg != nullptr && msgSize != 0)
9191
{
92-
if (this->sendMessage(msg_headers::client_msg, sendMsg, msgSize) == -1)
92+
if (this->sendMessage(msg_headers::client_msg, (char *)sendMsg, msgSize) == -1)
9393
{
9494
return session_result(session_status::proceed_msg_send_fail, errno);
9595
}

client/SessionClient.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace simpleApp
1717
virtual ~SessionClient();
1818

1919
virtual session_result init(in_addr_t address) = 0;
20-
session_result proceed(char* sendMsg = nullptr, size_t msgSize = 0);
20+
session_result proceed(const char* sendMsg = nullptr, size_t msgSize = 0);
2121
virtual session_result sendConnup();
2222
socket_t getSocket();
2323

client/SessionUdp.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ namespace simpleApp
5353
}
5454

5555
msg_headers header = *reinterpret_cast<msg_headers*>(buffer);
56-
56+
57+
if (header == msg_headers::err_connstart)
58+
{
59+
this->sessionClose();
60+
return session_result(session_status::server_error);
61+
}
62+
5763
if (header != msg_headers::accept_connstart)
5864
{
5965
this->sessionClose();

client/session_result.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace simpleApp
3434
connup_timeout,
3535
connup_not_req,
3636

37+
server_error,
3738
unknown_status
3839
};
3940

server/SessionUdp.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace simpleApp
5858
if (len != sizeof(msg_headers))
5959
return session_result(session_status::init_udp_wrong_length);
6060

61-
if (*(reinterpret_cast<msg_headers *>(msgBuff)) != msg_headers::client_connup)
61+
if (*(reinterpret_cast<msg_headers *>(msgBuff)) != msg_headers::req_connstart)
6262
return session_result(session_status::init_udp_wrong_header);
6363

6464
auto initSocket = [this, &address]()
@@ -81,7 +81,7 @@ namespace simpleApp
8181
timerEvent.data.fd = this->timerfd;
8282
timerEvent.data.ptr = this;
8383

84-
if (epoll_ctl(this->epollfd, EPOLL_CTL_DEL, this->timerfd, &timerEvent) == -1)
84+
if (epoll_ctl(this->epollfd, EPOLL_CTL_ADD, this->timerfd, &timerEvent) == -1)
8585
{
8686
auto result = session_result(session_status::init_udp_timer_fail, errno);
8787
close(this->timerfd);
@@ -111,14 +111,6 @@ namespace simpleApp
111111
this->_socket = -1;
112112
return result;
113113
}
114-
115-
if (connect(this->_socket, (sockaddr *)&address, sizeof(sockaddr_in)) == -1)
116-
{
117-
auto result = session_result(session_status::init_socket_setup_fail, errno);
118-
close(this->_socket);
119-
this->_socket = -1;
120-
return result;
121-
}
122114

123115
sockaddr_in socketBindAddress;
124116
bzero(&socketBindAddress, sizeof(socketBindAddress));
@@ -134,6 +126,14 @@ namespace simpleApp
134126
this->_socket = -1;
135127
return result;
136128
}
129+
130+
if (connect(this->_socket, (sockaddr *)&address, sizeof(sockaddr_in)) == -1)
131+
{
132+
auto result = session_result(session_status::init_socket_setup_fail, errno);
133+
close(this->_socket);
134+
this->_socket = -1;
135+
return result;
136+
}
137137

138138
epoll_event socketEvent;
139139
socketEvent.data.fd = this->_socket;

server/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,9 @@ int main(int argc, char** argv)
5555
return -1;
5656
}
5757

58-
return server.serverLoop();
58+
auto result = server.serverLoop();
59+
60+
tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
61+
62+
return result;
5963
}

0 commit comments

Comments
 (0)