zhangmeng
2024-04-22 16935f4aebffdd1b6580b844391a0aa0f4f3012b
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
//
// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
 
#include "core/nng_impl.h"
 
#ifdef NNG_PLATFORM_WINDOWS
 
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
void
nni_plat_abort(void)
{
    abort();
}
 
void
nni_plat_printf(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    (void) vprintf(fmt, ap);
    va_end(ap);
}
 
void
nni_plat_println(const char *message)
{
    (void) fprintf(stderr, "%s\n", message);
}
 
const char *
nni_plat_strerror(int errnum)
{
    if (errnum > NNG_ESYSERR) {
        errnum -= NNG_ESYSERR;
    }
    return (strerror(errnum));
}
 
// Win32 has its own error codes, but these ones it shares with POSIX.
static struct {
    int sys_err;
    int nng_err;
} nni_plat_errnos[] = {
    // clang-format off
    { ENOENT,    NNG_ENOENT },
    { EINTR,    NNG_EINTR },
    { EINVAL,    NNG_EINVAL },
    { ENOMEM,    NNG_ENOMEM },
    { EACCES,    NNG_EPERM },
    { EAGAIN,    NNG_EAGAIN },
    { EBADF,    NNG_ECLOSED },
    { EBUSY,    NNG_EBUSY },
    { ENAMETOOLONG,    NNG_EINVAL },
    { EPERM,    NNG_EPERM },
    { EPIPE,    NNG_ECLOSED },
    { 0,        0 } // must be last
    // clang-format on
};
 
int
nni_plat_errno(int errnum)
{
    int i;
 
    if (errnum == 0) {
        return (0);
    }
    for (i = 0; nni_plat_errnos[i].nng_err != 0; i++) {
        if (errnum == nni_plat_errnos[i].sys_err) {
            return (nni_plat_errnos[i].nng_err);
        }
    }
    // Other system errno.
    return (NNG_ESYSERR + errnum);
}
 
// Windows has infinite numbers of error codes it seems.  We only bother
// with the ones that are relevant to us (we think).  Note that there is
// no overlap between errnos and GetLastError values.  Note also that
// the WinSock errors are basically in the same number space as other
// errors, and WSAGetLastError() is an alias for GetLastError().
static struct {
    int win_err;
    int nng_err;
} nni_win_errnos[] = {
    // clang-format off
    { ERROR_FILE_NOT_FOUND,        NNG_ENOENT         },
    { ERROR_PATH_NOT_FOUND,        NNG_ENOENT         },
    { ERROR_ACCESS_DENIED,        NNG_EPERM         },
    { ERROR_INVALID_HANDLE,        NNG_ECLOSED         },
    { ERROR_NOT_ENOUGH_MEMORY,  NNG_ENOMEM         },
    { ERROR_INVALID_ACCESS,        NNG_EPERM         },
    { ERROR_INVALID_DATA,        NNG_EINVAL         },
    { ERROR_OUTOFMEMORY,        NNG_ENOMEM         },
    { ERROR_HANDLE_EOF,        NNG_ECLOSED         },
    { ERROR_NOT_SUPPORTED,        NNG_ENOTSUP         },
    { ERROR_OUT_OF_STRUCTURES,  NNG_ENOMEM         },
    { ERROR_INVALID_PARAMETER,  NNG_EINVAL         },
    { ERROR_CONNECTION_REFUSED, NNG_ECONNREFUSED },
        { ERROR_DUP_NAME,           NNG_EADDRINUSE   },
    { ERROR_BROKEN_PIPE,        NNG_ECLOSED         },
    { ERROR_BAD_PIPE,        NNG_ECLOSED         },
    { ERROR_NO_DATA,        NNG_ECLOSED         },
    { ERROR_PIPE_NOT_CONNECTED, NNG_ECLOSED         },
    { ERROR_OPERATION_ABORTED,  NNG_ECLOSED         },
    { ERROR_SHARING_VIOLATION,  NNG_EBUSY        },
    { WAIT_TIMEOUT,            NNG_ETIMEDOUT    },
    { WSAEINTR,            NNG_EINTR         },
    { WSAEBADF,            NNG_ECLOSED         },
    { WSAEACCES,            NNG_EPERM         },
    { WSAEWOULDBLOCK,        NNG_EAGAIN         },
    { WSAEINPROGRESS,        NNG_EAGAIN         },
    { WSAENOTSOCK,            NNG_ECLOSED         },
    { WSAEINVAL,            NNG_EINVAL       },
    { WSAEMSGSIZE,            NNG_EMSGSIZE     },
    { WSAENOPROTOOPT,        NNG_ENOTSUP         },
    { WSAEPROTONOSUPPORT,        NNG_ENOTSUP         },
    { WSAEPROTONOSUPPORT,        NNG_ENOTSUP         },
    { WSAESOCKTNOSUPPORT,        NNG_ENOTSUP      },
    { WSAEOPNOTSUPP,        NNG_ENOTSUP      },
    { WSAEPFNOSUPPORT,        NNG_ENOTSUP      },
    { WSAEAFNOSUPPORT,        NNG_ENOTSUP      },
    { WSAEADDRINUSE,        NNG_EADDRINUSE   },
    { WSAEADDRNOTAVAIL,        NNG_EADDRINVAL   },
    { WSAENETDOWN,            NNG_EUNREACHABLE },
    { WSAENETUNREACH,        NNG_EUNREACHABLE },
    { WSAECONNABORTED,        NNG_ETIMEDOUT    },
    { WSAECONNRESET,        NNG_ECLOSED         },
    { WSAENOBUFS,            NNG_ENOMEM         },
    { WSAENOTCONN,            NNG_ECLOSED         },
    { WSAESHUTDOWN,            NNG_ECLOSED         },
    { WSAETIMEDOUT,            NNG_ETIMEDOUT    },
    { WSAECONNREFUSED,        NNG_ECONNREFUSED },
    { WSAEHOSTDOWN,            NNG_EUNREACHABLE },
    { WSAEHOSTUNREACH,        NNG_EUNREACHABLE },
    { WSAVERNOTSUPPORTED,        NNG_ENOTSUP         },
    { WSAEDISCON,            NNG_ECLOSED         },
    { WSAECANCELLED,        NNG_ECANCELED    },
    { WSA_E_CANCELLED,        NNG_ECANCELED    },
    { WSAHOST_NOT_FOUND,        NNG_EADDRINVAL   },
    { WSATRY_AGAIN,            NNG_EAGAIN         },
    { WSANO_DATA,            NNG_EADDRINVAL   },
 
    // Must be Last!!
    {             0,           0 },
    // clang-format on
};
 
// This converts a Windows API error (from GetLastError()) to an
// nng standard error code.
int
nni_win_error(int errnum)
{
    int i;
 
    if (errnum == 0) {
        return (0);
    }
    for (i = 0; nni_win_errnos[i].win_err != 0; i++) {
        if (errnum == nni_win_errnos[i].win_err) {
            return (nni_win_errnos[i].nng_err);
        }
    }
    // Other system errno.
    return (NNG_ESYSERR + errnum);
}
 
#endif // NNG_PLATFORM_WINDOWS