-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTastyStream.cpp
executable file
·91 lines (82 loc) · 2.98 KB
/
TastyStream.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
89
90
91
#include "TastyStream.h"
/************************************************************/
CTastyStream::CTastyStream(string ipAddress, unsigned int port) :
_ipAddress(ipAddress),
_lowLevelSocket(NULL),
_port(port),
_timeoutInMicroseconds(0)
/************************************************************/
{
}
/************************************************************/
CTastyStream::~CTastyStream()
/************************************************************/
{
}
/**************************************************************/
bool CTastyStream::CreateLowLevelSocketIfNecessary()
/**************************************************************/
{
if(NULL==LowLevelSocket())
{
LowLevelSocket(CTastySocket::Create(IPAddress(), Port()));
}
return(NULL!=LowLevelSocket());
}
/**************************************************************/
void CTastyStream::CloseLowLevelSocket()
/**************************************************************/
{
if(NULL!=LowLevelSocket())
{
LowLevelSocket()->Close();
delete(LowLevelSocket());
LowLevelSocket(NULL);
}
}
/**************************************************************/
TASTY_RC CTastyStream::ReceiveCommon(CTastySocket& sock, TastyMessage& msg)
/**************************************************************/
{
if(1 != sock.SelectRead(TimeoutInMicroseconds())) //If no data available, no point calling recv
return(TASTY_NODATAONBUFFER);
if(msg._bytesRemaining==0)
return(TASTY_OK); //Message already received
int bytesReceived = sock.Receive(&(msg._buffer[msg._messageSize - msg._bytesRemaining]), msg._bytesRemaining);
if(bytesReceived>0)
{
msg._bytesRemaining = msg._bytesRemaining - bytesReceived;
if(msg._bytesRemaining==0)
return(TASTY_OK);
else if(msg._bytesRemaining>0)
return(TASTY_PENDING);
else
throw; //shouldn't happen, something wrong with internal counting
}
else if(bytesReceived==0) //Connection shutdown
return(TASTY_NOCONNECTION);
else
return(TASTY_ERROR); //Check errno, something else went wrong
}
/**************************************************************/
TASTY_RC CTastyStream::SendCommon(CTastySocket& sock, TastyMessage& msg)
/**************************************************************/
{
if(msg._bytesRemaining==0)
return(TASTY_OK); //Message already sent
int bytesSent = sock.Send(&(msg._buffer[msg._messageSize - msg._bytesRemaining]), msg._bytesRemaining);
if(bytesSent>0)
{
msg._bytesRemaining = msg._bytesRemaining - bytesSent;
if(msg._bytesRemaining==0)
return(TASTY_OK);
else if(msg._bytesRemaining>0)
return(TASTY_PENDING);
else
throw; //shouldn't happen, something wrong with internal counting
}
else if(bytesSent==0) //Connection shutdown
return(TASTY_NOCONNECTION);
else
return(TASTY_ERROR); //Check errno, something else went wrong
}