• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

polserver / polserver / 21108840797

18 Jan 2026 08:35AM UTC coverage: 60.508% (+0.02%) from 60.492%
21108840797

push

github

web-flow
ClangTidy readability-else-after-return (#857)

* trigger tidy

* Automated clang-tidy change: readability-else-after-return

* compile test

* rerun

* Automated clang-tidy change: readability-else-after-return

* trigger..

* Automated clang-tidy change: readability-else-after-return

* manually removed a few

* Automated clang-tidy change: readability-else-after-return

* removed duplicate code

* fix remaining warnings

* fixed scope

---------

Co-authored-by: Clang Tidy <clang-tidy@users.noreply.github.com>

837 of 1874 new or added lines in 151 files covered. (44.66%)

46 existing lines in 25 files now uncovered.

44448 of 73458 relevant lines covered (60.51%)

525066.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.13
/pol-core/pol/network/clientio.cpp
1
/** @file
2
 *
3
 * @par History
4
 * - 2009/12/04 Turley:    Crypto cleanup - Tomi
5
 */
6

7

8
#include <errno.h>
9
#include <iterator>
10
#include <mutex>
11
#include <stddef.h>
12
#include <string>
13

14
#include "../../clib/fdump.h"
15
#include "../../clib/logfacility.h"
16
#include "../../clib/network/sockets.h"
17
#include "../../clib/passert.h"
18
#include "../../clib/refptr.h"
19
#include "../../clib/spinlock.h"
20
#include "../accounts/account.h"
21
#include "../crypt/cryptbase.h"
22
#include "../ctable.h"
23
#include "../globals/network.h"
24
#include "../globals/state.h"
25
#include "../packetscrobj.h"
26
#include "../polsem.h"
27
#include "../polsig.h"
28
#include "client.h"
29
#include "clienttransmit.h"
30
#include "packethelper.h"
31
#include "packethooks.h"
32
#include "packets.h"
33
#include <fmt/chrono.h>
34

35

36
namespace Pol::Network
37
{
38
PacketLog Client::start_log()
×
39
{
40
  std::string filename = "log/";
×
41
  filename += acct->name();
×
42
  filename += ".log";
×
43

44
  return session()->start_log( filename );
×
45
}
×
46

47
PacketLog Client::stop_log()
4✔
48
{
49
  return session()->stop_log();
4✔
50
}
51

52

53
PacketLog ThreadedClient::start_log( std::string filename )
×
54
{
55
  Clib::SpinLockGuard guard( _fpLog_lock );
×
56
  if ( !fpLog.empty() )
×
57
  {
58
    return PacketLog::Unchanged;  // already logging
×
59
  }
60

61
  fpLog = OPEN_FLEXLOG( filename, true );
×
62
  if ( !fpLog.empty() )
×
63
  {
64
    return PacketLog::Success;
×
65
  }
66

NEW
67
  return PacketLog::Error;
×
UNCOV
68
}
×
69

70
PacketLog ThreadedClient::stop_log()
4✔
71
{
72
  Clib::SpinLockGuard guard( _fpLog_lock );
4✔
73
  if ( !fpLog.empty() )
4✔
74
  {
75
    auto time_tm = Clib::localtime( time( nullptr ) );
×
76
    FLEXLOGLN( fpLog, "Log closed at {:%c}", time_tm );
×
77
    CLOSE_FLEXLOG( fpLog );
×
78
    fpLog.clear();
×
79
    return PacketLog::Success;
×
80
  }
81

82
  return PacketLog::Unchanged;
4✔
83
}
4✔
84

85

86
std::string ThreadedClient::ipaddrAsString() const
11✔
87
{
88
  return AddressToString( &this->ipaddr );
11✔
89
}
90

91
std::string ThreadedClient::ipaddrProxyAsString() const
×
92
{
93
  return AddressToString( &this->ipaddr_proxy );
×
94
}
95

96
void ThreadedClient::recv_remaining( int total_expected )
334✔
97
{
98
  int count;
99
  int max_expected = total_expected - bytes_received;
334✔
100

101
  {
102
    std::lock_guard<std::mutex> lock( _socketMutex );
334✔
103
    count = cryptengine->Receive( &buffer[bytes_received], max_expected, csocket );
334✔
104
  }
334✔
105

106
  if ( count > 0 )
334✔
107
  {
108
    passert( count <= max_expected );
330✔
109

110
    bytes_received += count;
330✔
111
    counters.bytes_received += count;
330✔
112
    Core::networkManager.polstats.bytes_received += count;
330✔
113
  }
114
  else if ( count == 0 )  // graceful close
4✔
115
  {
116
    disconnect = true;
4✔
117
  }
118
  else
119
  {
120
    int errn = socket_errno;
×
121
    if ( errn != SOCKET_ERRNO( EWOULDBLOCK ) )
×
122
      disconnect = true;
×
123
  }
124
}
334✔
125

126
void ThreadedClient::recv_remaining_nocrypt( int total_expected )
6✔
127
{
128
  int count;
129

130
  {
131
    std::lock_guard<std::mutex> lock( _socketMutex );
6✔
132
    count = recv( csocket, (char*)&buffer[bytes_received], total_expected - bytes_received, 0 );
6✔
133
  }
6✔
134
  if ( count > 0 )
6✔
135
  {
136
    bytes_received += count;
6✔
137
    counters.bytes_received += count;
6✔
138
    Core::networkManager.polstats.bytes_received += count;
6✔
139
  }
140
  else if ( count == 0 )  // graceful close
×
141
  {
142
    disconnect = true;
×
143
  }
144
  else
145
  {
146
    int errn = socket_errno;
×
147
    if ( errn != SOCKET_ERRNO( EWOULDBLOCK ) )
×
148
      disconnect = true;
×
149
  }
150
}
6✔
151

152
/* NOTE: If this changes, code in client.cpp must change - pause() and restart() use
153
   pre-encrypted values of 33 00 and 33 01.
154
   */
155
void ThreadedClient::transmit_encrypted( const void* data, int len )
19,624✔
156
{
157
  THREAD_CHECKPOINT( active_client, 100 );
19,624✔
158
  const unsigned char* cdata = (const unsigned char*)data;
19,624✔
159
  unsigned char* pch;
160
  int i;
161
  int bidx;  // Offset in output byte
162
  EncryptedPktBuffer* outbuffer =
163
      PktHelper::RequestPacket<EncryptedPktBuffer>( ENCRYPTEDPKTBUFFER );
19,624✔
164
  pch = reinterpret_cast<unsigned char*>( outbuffer->getBuffer() );
19,624✔
165
  bidx = 0;
19,624✔
166
  THREAD_CHECKPOINT( active_client, 101 );
19,624✔
167
  for ( i = 0; i < len; i++ )
352,681✔
168
  {
169
    THREAD_CHECKPOINT( active_client, 102 );
333,057✔
170
    unsigned char ch = cdata[i];
333,057✔
171
    int nbits = Core::keydesc[ch].nbits;
333,057✔
172
    unsigned short inval = Core::keydesc[ch].bits_reversed;
333,057✔
173

174
    THREAD_CHECKPOINT( active_client, 103 );
333,057✔
175

176
    while ( nbits-- )
1,974,445✔
177
    {
178
      THREAD_CHECKPOINT( active_client, 104 );
1,641,388✔
179
      *pch <<= 1;
1,641,388✔
180
      if ( inval & 1 )
1,641,388✔
181
        *pch |= 1;
712,300✔
182
      bidx++;
1,641,388✔
183
      if ( bidx == 8 )
1,641,388✔
184
      {
185
        THREAD_CHECKPOINT( active_client, 105 );
195,504✔
186
        pch++;
195,504✔
187
        bidx = 0;
195,504✔
188
      }
189
      THREAD_CHECKPOINT( active_client, 106 );
1,641,388✔
190

191
      inval >>= 1;
1,641,388✔
192
    }
193
    THREAD_CHECKPOINT( active_client, 107 );
333,057✔
194
  }
195
  THREAD_CHECKPOINT( active_client, 108 );
19,624✔
196

197
  {
198
    int nbits = Core::keydesc[0x100].nbits;
19,624✔
199
    unsigned short inval = Core::keydesc[0x100].bits_reversed;
19,624✔
200

201
    THREAD_CHECKPOINT( active_client, 109 );
19,624✔
202

203
    while ( nbits-- )
98,120✔
204
    {
205
      THREAD_CHECKPOINT( active_client, 110 );
78,496✔
206
      *pch <<= 1;
78,496✔
207
      if ( inval & 1 )
78,496✔
208
        *pch |= 1;
58,872✔
209
      bidx++;
78,496✔
210
      THREAD_CHECKPOINT( active_client, 111 );
78,496✔
211
      if ( bidx == 8 )
78,496✔
212
      {
213
        pch++;
12,362✔
214
        bidx = 0;
12,362✔
215
      }
216
      THREAD_CHECKPOINT( active_client, 112 );
78,496✔
217

218
      inval >>= 1;
78,496✔
219
    }
220
  }
221
  THREAD_CHECKPOINT( active_client, 113 );
19,624✔
222

223
  if ( bidx == 0 )
19,624✔
224
  {
225
    pch--;
2,873✔
226
  }
227
  else
228
  {
229
    *pch <<= ( 8 - bidx );
16,751✔
230
  }
231
  THREAD_CHECKPOINT( active_client, 114 );
19,624✔
232

233
  passert_always( pch - reinterpret_cast<unsigned char*>( outbuffer->buffer ) + 1 <=
19,624✔
234
                  int( sizeof outbuffer->buffer ) );
235
  THREAD_CHECKPOINT( active_client, 115 );
19,624✔
236
  xmit( &outbuffer->buffer, static_cast<unsigned short>(
19,624✔
237
                                pch - reinterpret_cast<unsigned char*>( outbuffer->buffer ) + 1 ) );
19,624✔
238
  PktHelper::ReAddPacket( outbuffer );
19,624✔
239
  THREAD_CHECKPOINT( active_client, 116 );
19,624✔
240
}
19,624✔
241

242
void Client::transmit( const void* data, int len )
19,628✔
243
{
244
  ref_ptr<Core::BPacket> p;
19,628✔
245
  bool handled = false;
19,628✔
246
  // see if the outgoing packet has a SendFunction installed. If so call it. It may or may not
247
  // want us to continue sending the packet. If it does, handled will be false, and data, len, and p
248
  // will be altered. data has the new packet data to send, len the new length, and p, a ref counted
249
  // pointer to the packet object.
250
  //
251
  // If there is no outgoing packet script, handled will be false, and the passed params will be
252
  // unchanged.
253
  {
254
    PacketHookData* phd = nullptr;
19,628✔
255
    handled = GetAndCheckPacketHooked( this, data, phd );
19,628✔
256
    if ( handled )
19,628✔
257
    {
258
      Core::PolLock lock;
×
259
      CallOutgoingPacketExportedFunction( this, data, len, p, phd, handled );
×
260
    }
×
261
  }
262

263
  if ( handled )
19,628✔
264
    return;
×
265

266
  unsigned char msgtype = *(const char*)data;
19,628✔
267

268
  {
269
    Clib::SpinLockGuard guard( _fpLog_lock );
19,628✔
270
    if ( !fpLog.empty() )
19,628✔
271
    {
272
      std::string tmp = fmt::format( "Server -> Client: {:#x}, {} bytes\n", msgtype, len );
×
273
      Clib::fdump( std::back_inserter( tmp ), data, len );
×
274
      FLEXLOGLN( fpLog, tmp );
×
275
    }
×
276
  }
19,628✔
277

278
  std::lock_guard<std::mutex> guard( _socketMutex );
19,628✔
279
  if ( disconnect )
19,628✔
280
  {
281
    POLLOG_INFOLN( "Warning: Trying to send to a disconnected client! " );
×
282
    std::string tmp = fmt::format( "Server -> Client: {:#x}, {} bytes\n", msgtype, len );
×
283
    Clib::fdump( std::back_inserter( tmp ), data, len );
×
284
    POLLOG_INFOLN( tmp );
×
285
    return;
×
286
  }
×
287

288
  if ( last_xmit_buffer )
19,628✔
289
  {
290
    Core::networkManager.queuedmode_iostats.sent[msgtype].count++;
×
291
    Core::networkManager.queuedmode_iostats.sent[msgtype].bytes += len;
×
292
  }
293
  Core::networkManager.iostats.sent[msgtype].count++;
19,628✔
294
  Core::networkManager.iostats.sent[msgtype].bytes += len;
19,628✔
295

296
  if ( encrypt_server_stream )
19,628✔
297
  {
298
    pause();
19,624✔
299
    transmit_encrypted( data, len );
19,624✔
300
  }
301
  else
302
  {
303
    xmit( data, static_cast<unsigned short>( len ) );
4✔
304
    // _xmit( client->csocket, data, len );
305
  }
306
}
19,628✔
307

308
void transmit( Client* client, const void* data, int len )
4✔
309
{
310
  Core::networkManager.clientTransmit->AddToQueue( client, data, len );
4✔
311
}
4✔
312

313
void Client::Disconnect()
×
314
{
315
  if ( this->isConnected() )
×
316
  {
317
    this->preDisconnect = true;
×
318
    Core::networkManager.clientTransmit->QueueDisconnection( this );
×
319
  }
320
}
×
321
}  // namespace Pol::Network
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc