summaryrefslogtreecommitdiffhomepage
path: root/NetEx/NetLayer.cpp
blob: 8494f72cf7e1db6786716b57ba333cd7bbfe0353 (plain)
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
/*  Project nGenEx
    Destroyer Studios LLC
    Copyright © 1997-2004. All Rights Reserved.

    SUBSYSTEM:    NetEx.lib
    FILE:         NetLayer.cpp
    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Wrapper for WinSock Library
*/


#include "MemDebug.h"
#include <windows.h>
#include "NetLayer.h"

#include <mmsystem.h>
#include <time.h>

// +-------------------------------------------------------------------+

static DWORD baseTime = timeGetTime();

// +-------------------------------------------------------------------+

NetLayer::NetLayer()
{
   fail = false;
   ZeroMemory(&info, sizeof(info));

   WORD ver = MAKEWORD(2,2);
   int  err = WSAStartup(ver, &info);

   if (err)
      fail = true;
}

NetLayer::~NetLayer()
{
   WSACleanup();
}

bool
NetLayer::OK() const
{
   return !fail;
}

const char*
NetLayer::Description() const
{
   return info.szDescription;
}

int
NetLayer::GetLastError()
{
   return WSAGetLastError();
}

DWORD
NetLayer::GetTime()
{
   DWORD msec = timeGetTime();

   if (msec >= baseTime) {
      return msec - baseTime;
   }

   else {
      DWORD extra = 0xffffffff;
      return msec + extra - baseTime;
   }
}

long
NetLayer::GetUTC()
{
   return (long) time(0);
}

Text
NetLayer::GetHostName()
{
   char hostname[256];
   ZeroMemory(hostname, sizeof(hostname));
   ::gethostname(hostname, sizeof(hostname));

   return hostname;
}