-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathSysSocket.cpp
More file actions
246 lines (192 loc) · 6.42 KB
/
Copy pathSysSocket.cpp
File metadata and controls
246 lines (192 loc) · 6.42 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//==============================================================================
//
// SysSocket.cpp
//
// Copyright (C) 2013-2025 Greg Utas
//
// This file is part of the Robust Services Core (RSC).
//
// RSC is free software: you can redistribute it and/or modify it under the
// terms of the Lesser GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// RSC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the Lesser GNU General Public License
// along with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#include "SysSocket.h"
#include <iosfwd>
#include <sstream>
#include "Debug.h"
#include "IpBuffer.h"
#include "Log.h"
#include "NwLogs.h"
#include "NwTrace.h"
#include "NwTracer.h"
#include "Singleton.h"
#include "SysIpL3Addr.h"
#include "Tool.h"
#include "TraceBuffer.h"
using namespace NodeBase;
using std::ostream;
using std::string;
//------------------------------------------------------------------------------
namespace NetworkBase
{
SysSocket::SysSocket(SysSocket_t socket, ipport_t port) :
socket_(socket),
error_(0),
port_(port),
blocking_(true),
tracing_(false)
{
Debug::ft("SysSocket.ctor(wrap)");
}
//------------------------------------------------------------------------------
SysSocket::~SysSocket()
{
Debug::ftnt("SysSocket.dtor");
TraceEvent(NwTrace::Delete, 0);
}
//------------------------------------------------------------------------------
void SysSocket::Display(ostream& stream,
const string& prefix, const Flags& options) const
{
Dynamic::Display(stream, prefix, options);
stream << prefix << "socket : " << socket_ << CRLF;
stream << prefix << "error : " << error_ << CRLF;
stream << prefix << "port : " << port_ << CRLF;
stream << prefix << "blocking : " << blocking_ << CRLF;
stream << prefix << "tracing : " << tracing_ << CRLF;
}
//------------------------------------------------------------------------------
nwerr_t SysSocket::OutputLog(LogId id, c_string func, nwerr_t errval)
{
Debug::ft("SysSocket.OutputLog(errval)");
// Generate a log that appends information about this socket. To
// prevent log floods, don't generate the same log twice in a row
// on the same socket.
//
if(error_ != errval)
{
std::ostringstream stream;
error_ = errval;
stream << CRLF << Log::Tab << to_str();
OutputNwLog(id, func, errval, stream.str().c_str());
}
return -1;
}
//------------------------------------------------------------------------------
void SysSocket::OutputLog(LogId id, c_string func, const IpBuffer* buff) const
{
Debug::ft("SysSocket.OutputLog(buff)");
// Generate a log that appends BUFF's addresses.
//
std::ostringstream stream;
stream << CRLF;
if(buff != nullptr)
{
stream << Log::Tab << "txAddr=" << buff->TxAddr().to_str(true) << CRLF;
stream << Log::Tab << "rxAddr=" << buff->RxAddr().to_str(true);
}
else
{
stream << Log::Tab << to_str();
}
OutputNwLog(id, func, GetError(), stream.str().c_str());
}
//------------------------------------------------------------------------------
void SysSocket::Patch(sel_t selector, void* arguments)
{
Dynamic::Patch(selector, arguments);
}
//------------------------------------------------------------------------------
fn_name SysSocket_SendBuff = "SysSocket.SendBuff";
SysSocket::SendRc SysSocket::SendBuff(IpBuffer& buff)
{
Debug::ft(SysSocket_SendBuff);
Debug::SwLog(SysSocket_SendBuff, strOver(this), 0);
return SendFailed;
}
//------------------------------------------------------------------------------
nwerr_t SysSocket::SetError(nwerr_t errval)
{
Debug::ft("SysSocket.SetError(errval)");
error_ = errval;
return -1;
}
//------------------------------------------------------------------------------
bool SysSocket::SetTracing(bool tracing)
{
tracing_ = tracing;
return tracing;
}
//------------------------------------------------------------------------------
string SysSocket::to_str() const
{
std::ostringstream stream;
stream << "socket=" << socket_;
if(port_ != NilIpPort) stream << " port=" << port_;
return stream.str();
}
//------------------------------------------------------------------------------
bool SysSocket::Trace(TraceStatus status)
{
auto tracer = Singleton<TraceBuffer>::Instance();
if(!tracer->ToolIsOn(NetworkTracer)) return false;
if(status == TraceIncluded) return SetTracing(true);
if(status == TraceExcluded) return false;
return SetTracing(tracer->FilterIsOn(TraceAll));
}
//------------------------------------------------------------------------------
bool SysSocket::TraceEnabled()
{
return (Debug::TraceOn() ? true : SetTracing(false));
}
//------------------------------------------------------------------------------
NwTrace* SysSocket::TraceEvent(TraceRecordId rid, word data)
{
Debug::ft("SysSocket.TraceEvent");
if(!TraceEnabled()) return nullptr;
if(tracing_)
{
auto buff = Singleton<TraceBuffer>::Instance();
auto rec = new NwTrace(rid, this, data);
if(buff->Insert(rec)) return rec;
}
return nullptr;
}
//------------------------------------------------------------------------------
NwTrace* SysSocket::TracePeer
(TraceRecordId rid, ipport_t port, const SysIpL3Addr& peer, word data)
{
Debug::ft("SysSocket.TracePeer");
if(!TraceEnabled()) return nullptr;
auto nwt = Singleton<NwTracer>::Instance();
if(tracing_ || Trace(nwt->PortStatus(port)) || Trace(nwt->PeerStatus(peer)))
{
auto buff = Singleton<TraceBuffer>::Instance();
auto rec = new NwTrace(rid, this, data, port, peer);
if(buff->Insert(rec)) return rec;
}
return nullptr;
}
//------------------------------------------------------------------------------
NwTrace* SysSocket::TracePort(TraceRecordId rid, ipport_t port, word data)
{
Debug::ft("SysSocket.TracePort");
if(!TraceEnabled()) return nullptr;
if(tracing_ || Trace(Singleton<NwTracer>::Instance()->PortStatus(port)))
{
auto buff = Singleton<TraceBuffer>::Instance();
auto rec = new NwTrace(rid, this, data, port);
if(buff->Insert(rec)) return rec;
}
return nullptr;
}
}