-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTastyClient.cpp
executable file
·88 lines (75 loc) · 2.5 KB
/
TastyClient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "TastyClient.h"
#include <cerrno>
/**************************************************************/
CTastyClient::CTastyClient(string serverIPAddress, unsigned int serverPort) :
CTastyStream(serverIPAddress, serverPort),
_isConnected(false)
/**************************************************************/
{
}
/**************************************************************/
CTastyClient::~CTastyClient()
/**************************************************************/
{
}
/**************************************************************/
void CTastyClient::Close()
/**************************************************************/
{
CloseLowLevelSocket();
IsConnected(false);
}
/**************************************************************/
TASTY_RC CTastyClient::EnsureConnectionToServerExists()
/**************************************************************/
{
if(!CreateLowLevelSocketIfNecessary())
return(TASTY_NOLOWLEVELSOCKET);
if(!EstablishConnectionToServerIfNecessary())
return(TASTY_NOCONNECTION);
return(TASTY_OK);
}
/**************************************************************/
bool CTastyClient::EstablishConnectionToServerIfNecessary()
/**************************************************************/
{
if(!IsConnected())
{
int rc = LowLevelSocket()->Connect();
IsConnected(0 == rc);
if(!IsConnected())
{
#ifndef WIN32
if((errno != EINPROGRESS))
#else
if((errno != WSAEINPROGRESS))
#endif
Close(); //Note: Can't reuse same low level FD if error code is not one of the ones in the above if condition
}
}
return(IsConnected());
}
/**************************************************************/
TASTY_RC CTastyClient::Receive(TastyMessage& msg)
/**************************************************************/
{
TASTY_RC connectRC = EnsureConnectionToServerExists();
if(TASTY_OK != connectRC)
return(connectRC);
TASTY_RC rc = ReceiveCommon(*(LowLevelSocket()), msg);
if(rc==TASTY_NOCONNECTION)
IsConnected(false);
return(rc);
}
/**************************************************************/
TASTY_RC CTastyClient::Send(TastyMessage& msg)
/**************************************************************/
{
TASTY_RC connectRC = EnsureConnectionToServerExists();
if(TASTY_OK != connectRC)
return(connectRC);
TASTY_RC rc = SendCommon(*(LowLevelSocket()), msg);
if(rc==TASTY_NOCONNECTION)
IsConnected(false);
return(rc);
}