win32: sockets abstraction layer with cancellable blocking IO.
This commit is contained in:
parent
df46811d2d
commit
935cd1bdff
2
Makefile
2
Makefile
|
@ -78,7 +78,7 @@ $(BINARY): main.o fet.o rf2500.o dis.o uif.o olimex.o ihex.o elf32.o stab.o \
|
|||
fet_db.o usbutil.o titext.o srec.o device.o coff.o opdb.o output.o \
|
||||
cmddb.o stdcmd.o prog.o flash_bsl.o list.o simio.o simio_tracer.o \
|
||||
simio_timer.o simio_wdt.o simio_hwmult.o simio_gpio.o aliasdb.o \
|
||||
gdb_proto.o gdbc.o sport.o
|
||||
gdb_proto.o gdbc.o sport.o sockets.o
|
||||
$(CC) $(MSPDEBUG_LDFLAGS) -o $@ $^ $(MSPDEBUG_LIBS)
|
||||
|
||||
.c.o:
|
||||
|
|
6
gdb.c
6
gdb.c
|
@ -450,7 +450,7 @@ static int gdb_server(int port)
|
|||
int i;
|
||||
|
||||
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (sock < 0) {
|
||||
if (SOCKET_ISERR(sock)) {
|
||||
pr_error("gdb: can't create socket");
|
||||
return -1;
|
||||
}
|
||||
|
@ -479,8 +479,8 @@ static int gdb_server(int port)
|
|||
printc("Bound to port %d. Now waiting for connection...\n", port);
|
||||
|
||||
len = sizeof(addr);
|
||||
client = accept(sock, (struct sockaddr *)&addr, &len);
|
||||
if (client < 0) {
|
||||
client = sockets_accept(sock, (struct sockaddr *)&addr, &len);
|
||||
if (SOCKET_ISERR(client)) {
|
||||
pr_error("gdb: failed to accept connection");
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
|
|
27
gdb_proto.c
27
gdb_proto.c
|
@ -53,26 +53,8 @@ void gdb_printf(struct gdb_data *data, const char *fmt, ...)
|
|||
|
||||
static int gdb_read(struct gdb_data *data, int timeout_ms)
|
||||
{
|
||||
fd_set r;
|
||||
int len;
|
||||
struct timeval to = {
|
||||
.tv_sec = timeout_ms / 1000,
|
||||
.tv_usec = timeout_ms % 1000
|
||||
};
|
||||
|
||||
FD_ZERO(&r);
|
||||
FD_SET(data->sock, &r);
|
||||
|
||||
if (select(data->sock + 1, &r, NULL, NULL,
|
||||
timeout_ms < 0 ? NULL : &to) < 0) {
|
||||
pr_error("gdb: select");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!FD_ISSET(data->sock, &r))
|
||||
return 0;
|
||||
|
||||
len = recv(data->sock, data->xbuf, sizeof(data->xbuf), 0);
|
||||
int len = sockets_recv(data->sock, data->xbuf, sizeof(data->xbuf), 0,
|
||||
timeout_ms);
|
||||
|
||||
if (len < 0) {
|
||||
data->error = errno;
|
||||
|
@ -114,7 +96,7 @@ int gdb_getc(struct gdb_data *data)
|
|||
|
||||
static int gdb_flush(struct gdb_data *data)
|
||||
{
|
||||
if (send(data->sock, data->outbuf, data->outlen, 0) < 0) {
|
||||
if (sockets_send(data->sock, data->outbuf, data->outlen, 0) < 0) {
|
||||
data->error = errno;
|
||||
pr_error("gdb: flush");
|
||||
return -1;
|
||||
|
@ -134,7 +116,8 @@ int gdb_flush_ack(struct gdb_data *data)
|
|||
data->outbuf[data->outlen] = 0;
|
||||
|
||||
do {
|
||||
if (send(data->sock, data->outbuf, data->outlen, 0) < 0) {
|
||||
if (sockets_send(data->sock, data->outbuf,
|
||||
data->outlen, 0) < 0) {
|
||||
data->error = errno;
|
||||
pr_error("gdb: flush_ack");
|
||||
return -1;
|
||||
|
|
7
gdbc.c
7
gdbc.c
|
@ -275,7 +275,7 @@ static int gdbc_ctl(device_t dev_base, device_ctl_t op)
|
|||
|
||||
case DEVICE_CTL_HALT:
|
||||
if (dev->is_running) {
|
||||
if (send(dev->gdb.sock, "\003", 1, 0) < 1) {
|
||||
if (sockets_send(dev->gdb.sock, "\003", 1, 0) < 1) {
|
||||
pr_error("gdbc: write");
|
||||
return -1;
|
||||
}
|
||||
|
@ -389,7 +389,7 @@ static int connect_to(const char *spec)
|
|||
}
|
||||
|
||||
sock = socket(PF_INET, SOCK_STREAM, 0);
|
||||
if (!sock) {
|
||||
if (SOCKET_ISERR(sock)) {
|
||||
printc_err("socket: %s\n", last_error());
|
||||
return -1;
|
||||
}
|
||||
|
@ -400,7 +400,8 @@ static int connect_to(const char *spec)
|
|||
printc_dbg("Connecting to %s:%d...\n",
|
||||
inet_ntoa(addr.sin_addr), port);
|
||||
|
||||
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
if (sockets_connect(sock, (struct sockaddr *)&addr,
|
||||
sizeof(addr)) < 0) {
|
||||
printc_err("connect: %s\n", last_error());
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
|
|
2
main.c
2
main.c
|
@ -326,6 +326,7 @@ int main(int argc, char **argv)
|
|||
int ret = 0;
|
||||
|
||||
opdb_reset();
|
||||
ctrlc_init();
|
||||
|
||||
args.devarg.vcc_mv = 3000;
|
||||
args.devarg.requested_serial = NULL;
|
||||
|
@ -355,7 +356,6 @@ int main(int argc, char **argv)
|
|||
}
|
||||
}
|
||||
} else {
|
||||
ctrlc_init();
|
||||
reader_loop();
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,163 @@
|
|||
/* MSPDebug - debugging tool for MSP430 MCUs
|
||||
* Copyright (C) 2009, 2010 Daniel Beer
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "sockets.h"
|
||||
#include "util.h"
|
||||
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
static DWORD error_save = 0;
|
||||
|
||||
static void sockets_begin(SOCKET s, DWORD event)
|
||||
{
|
||||
u_long mode = 1;
|
||||
|
||||
ioctlsocket(s, FIONBIO, &mode);
|
||||
ctrlc_reset();
|
||||
WSAEventSelect(s, ctrlc_win32_event(), event);
|
||||
}
|
||||
|
||||
static int sockets_wait(DWORD timeout)
|
||||
{
|
||||
DWORD r;
|
||||
|
||||
error_save = WSAGetLastError();
|
||||
if (ctrlc_check())
|
||||
error_save = ERROR_OPERATION_ABORTED;
|
||||
if (error_save != WSAEWOULDBLOCK)
|
||||
return -1;
|
||||
|
||||
r = WaitForSingleObject(ctrlc_win32_event(), timeout);
|
||||
|
||||
if (r == WAIT_TIMEOUT) {
|
||||
error_save = WAIT_TIMEOUT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void sockets_end(SOCKET s)
|
||||
{
|
||||
u_long mode = 0;
|
||||
|
||||
ioctlsocket(s, FIONBIO, &mode);
|
||||
WSAEventSelect(s, ctrlc_win32_event(), 0);
|
||||
WSASetLastError(error_save);
|
||||
}
|
||||
|
||||
SOCKET sockets_accept(SOCKET s, struct sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
SOCKET client = INVALID_SOCKET;
|
||||
|
||||
sockets_begin(s, FD_ACCEPT);
|
||||
|
||||
do {
|
||||
client = WSAAccept(s, addr, addrlen, NULL, 0);
|
||||
} while (SOCKET_ISERR(client) && !sockets_wait(INFINITE));
|
||||
|
||||
sockets_end(s);
|
||||
return client;
|
||||
}
|
||||
|
||||
int sockets_connect(SOCKET s, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
int ret = -1;
|
||||
|
||||
sockets_begin(s, FD_CONNECT);
|
||||
|
||||
do {
|
||||
ret = connect(s, addr, addrlen);
|
||||
} while (ret < 0 && !sockets_wait(INFINITE));
|
||||
|
||||
sockets_end(s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t sockets_send(SOCKET s, const void *buf, size_t len, int flags)
|
||||
{
|
||||
int ret = -1;
|
||||
|
||||
sockets_begin(s, FD_WRITE);
|
||||
|
||||
do {
|
||||
ret = send(s, buf, len, flags);
|
||||
} while (ret < 0 && !sockets_wait(INFINITE));
|
||||
|
||||
sockets_end(s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t sockets_recv(SOCKET s, void *buf, size_t len, int flags,
|
||||
int timeout_ms)
|
||||
{
|
||||
int ret = -1;
|
||||
DWORD to_arg = (timeout_ms >= 0) ? timeout_ms : INFINITE;
|
||||
|
||||
sockets_begin(s, FD_READ);
|
||||
|
||||
do {
|
||||
ret = recv(s, buf, len, flags);
|
||||
} while (ret < 0 && !sockets_wait(to_arg));
|
||||
|
||||
sockets_end(s);
|
||||
return ret;
|
||||
}
|
||||
#else /* WIN32 */
|
||||
SOCKET sockets_accept(SOCKET s, struct sockaddr *addr, socklen_t *addrlen)
|
||||
{
|
||||
return accept(s, addr, addrlen);
|
||||
}
|
||||
|
||||
int sockets_connect(SOCKET s, const struct sockaddr *addr, socklen_t addrlen)
|
||||
{
|
||||
return connect(s, addr, addrlen);
|
||||
}
|
||||
|
||||
ssize_t sockets_send(SOCKET s, const void *buf, size_t len, int flags)
|
||||
{
|
||||
return send(s, buf, len, flags);
|
||||
}
|
||||
|
||||
ssize_t sockets_recv(SOCKET s, void *buf, size_t buf_len, int flags,
|
||||
int timeout_ms)
|
||||
{
|
||||
fd_set r;
|
||||
struct timeval to = {
|
||||
.tv_sec = timeout_ms / 1000,
|
||||
.tv_usec = timeout_ms % 1000
|
||||
};
|
||||
|
||||
FD_ZERO(&r);
|
||||
FD_SET(s, &r);
|
||||
|
||||
if (select(s + 1, &r, NULL, NULL,
|
||||
timeout_ms < 0 ? NULL : &to) < 0)
|
||||
return -1;
|
||||
|
||||
if (!FD_ISSET(s, &r)) {
|
||||
errno = ETIMEDOUT;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return recv(s, buf, buf_len, flags);
|
||||
}
|
||||
#endif
|
17
sockets.h
17
sockets.h
|
@ -20,10 +20,12 @@
|
|||
#define SOCKETS_H_
|
||||
|
||||
#ifdef WIN32
|
||||
#include <winsock.h>
|
||||
#include <winsock2.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef int socklen_t;
|
||||
|
||||
#define SOCKET_ISERR(x) ((x) == INVALID_SOCKET)
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
|
@ -33,6 +35,19 @@ typedef int socklen_t;
|
|||
#include <fcntl.h>
|
||||
|
||||
#define closesocket close
|
||||
|
||||
typedef int SOCKET;
|
||||
|
||||
#define SOCKET_ISERR(x) ((x) < 0)
|
||||
#endif
|
||||
|
||||
/* These are versions of the blocking IO calls which can be interrupted
|
||||
* by the user pressing Ctrl+C.
|
||||
*/
|
||||
SOCKET sockets_accept(SOCKET s, struct sockaddr *addr, socklen_t *addrlen);
|
||||
int sockets_connect(SOCKET s, const struct sockaddr *addr, socklen_t addrlen);
|
||||
ssize_t sockets_send(SOCKET s, const void *buf, size_t len, int flags);
|
||||
ssize_t sockets_recv(SOCKET s, void *buf, size_t len, int flags,
|
||||
int timeout_ms);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue