+void SocketEngine::Statistics::UpdateWriteCounters(int len_out)
+{
+ CheckFlush();
+
+ WriteEvents++;
+ if (len_out > 0)
+ outdata += len_out;
+ else if (len_out < 0)
+ ErrorEvents++;
+}
+
+void SocketEngine::Statistics::CheckFlush() const
+{
+ // Reset the in/out byte counters if it has been more than a second
+ time_t now = ServerInstance->Time();
+ if (lastempty != now)
+ {
+ lastempty = now;
+ indata = outdata = 0;
+ }
+}
+
+void SocketEngine::Statistics::GetBandwidth(float& kbitpersec_in, float& kbitpersec_out, float& kbitpersec_total) const
+{
+ CheckFlush();
+ float in_kbit = indata * 8;
+ float out_kbit = outdata * 8;
+ kbitpersec_total = ((in_kbit + out_kbit) / 1024);
+ kbitpersec_in = in_kbit / 1024;
+ kbitpersec_out = out_kbit / 1024;
+}
+
+std::string SocketEngine::LastError()
+{
+#ifndef _WIN32
+ return strerror(errno);
+#else
+ char szErrorString[500];
+ DWORD dwErrorCode = WSAGetLastError();
+ if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szErrorString, _countof(szErrorString), NULL) == 0)
+ sprintf_s(szErrorString, _countof(szErrorString), "Error code: %u", dwErrorCode);
+
+ std::string::size_type p;
+ std::string ret = szErrorString;
+ while ((p = ret.find_last_of("\r\n")) != std::string::npos)
+ ret.erase(p, 1);
+
+ return ret;
+#endif
+}
+
+std::string SocketEngine::GetError(int errnum)
+{
+#ifndef _WIN32
+ return strerror(errnum);
+#else
+ WSASetLastError(errnum);
+ return LastError();
+#endif
+}