Skip to content

Commit

Permalink
Merge pull request #60 from toppers/support-winsock-build
Browse files Browse the repository at this point in the history
Windowビルドでエラーになる箇所の対応
  • Loading branch information
tmori authored May 22, 2022
2 parents 056a7aa + 3c4ffaf commit 8c3732f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
47 changes: 47 additions & 0 deletions src/device/peripheral/athrill_syscall_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
#define ATHRILL_SYSCALL_DEVICE
#include "athrill_syscall.h"
#include <stdio.h>
#include <stdint.h>
#include <sys/fcntl.h>
#include <sys/types.h>
#ifdef OS_LINUX
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/select.h>
#else
#include <winsock2.h>
#endif
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -128,7 +133,13 @@ static void athrill_syscall_none(AthrillSyscallArgType *arg)
}
static void athrill_syscall_socket(AthrillSyscallArgType *arg)
{
#ifdef OS_LINUX
int sockfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
#else
int sockfd = socket(AF_INET, SOCK_STREAM , 0);
u_long val=1;
ioctlsocket(sockfd, FIONBIO, &val);
#endif
if (sockfd < 0) {
printf("ERROR:%s(): errno=%d\n", __FUNCTION__, errno);
return;
Expand All @@ -143,7 +154,11 @@ static void athrill_syscall_sense(AthrillSyscallArgType *arg)
struct timeval tv;
int retval;
int val;
#ifdef OS_LINUX
socklen_t len = sizeof(val);
#else
int len = sizeof(val);
#endif

FD_ZERO(&fds);
FD_SET(arg->body.api_sense.sockfd, &fds);
Expand Down Expand Up @@ -228,7 +243,11 @@ static void athrill_syscall_accept(AthrillSyscallArgType *arg)
Std_ReturnType err;
struct sockaddr_in client_addr;
struct sys_sockaddr_in *sockaddrp;
#ifdef OS_LINUX
socklen_t addrlen;
#else
int addrlen;
#endif
sys_uint32 *addrlenp;

err = mpu_get_pointer(0U, arg->body.api_accept.sockaddr, (uint8 **)&sockaddrp);
Expand Down Expand Up @@ -387,7 +406,11 @@ static void athrill_syscall_send(AthrillSyscallArgType *arg)
if (err != 0) {
return;
}
#ifdef OS_LINUX
ret = send(arg->body.api_send.sockfd, bufp, arg->body.api_send.len, MSG_DONTWAIT);
#else
ret = send(arg->body.api_send.sockfd, bufp, arg->body.api_send.len, 0);
#endif
if (ret < 0) {
arg->ret_value = -errno;
}
Expand All @@ -405,7 +428,11 @@ static void athrill_syscall_recv(AthrillSyscallArgType *arg)
if (err != 0) {
return;
}
#ifdef OS_LINUX
ret = recv(arg->body.api_recv.sockfd, bufp, arg->body.api_recv.len, MSG_DONTWAIT);
#else
ret = recv(arg->body.api_recv.sockfd, bufp, arg->body.api_recv.len, 0);
#endif
if (ret < 0) {
arg->ret_value = -errno;
}
Expand Down Expand Up @@ -503,11 +530,19 @@ static int create_directory(const char* dir)
{
int ret = 0;

#ifdef OS_LINUX
if ( (mkdir(dir,0777) == -1) && (errno != EEXIST) ) {
printf("create_directory() mkdir failed path=%s errno=0x%x",
dir, errno);
ret = -1;
}
#else
if ( (_mkdir(dir) == -1) && (errno != EEXIST) ) {
printf("create_directory() mkdir failed path=%s errno=0x%x",
dir, errno);
ret = -1;
}
#endif
return ret;
}

Expand Down Expand Up @@ -761,7 +796,11 @@ static void athrill_syscall_set_virtfs_top(AthrillSyscallArgType *arg)
err = mpu_get_pointer(0U, arg->body.api_set_virtfs_top.top_dir,(uint8**)&top_dir);
ASSERT(err == 0);

#ifdef OS_LINUX
if ( (mkdir(top_dir,0777) == -1) && (errno != EEXIST) ) {
#else
if ( (_mkdir(top_dir) == -1) && (errno != EEXIST) ) {
#endif
printf("SYSCAL]set_virtfs_top mkdir failed path=%s errno=0x%x",
top_dir, errno);
} else {
Expand Down Expand Up @@ -958,7 +997,11 @@ static int create_pipe(const char *path, int is_read )
int ret;
if ( stat(path, &stat_buf) == 0 ) {
// as pipe name exist, remove it first
#ifdef OS_LINUX
int tmp_fd = open(path, O_RDONLY|O_NONBLOCK);
#else
int tmp_fd = open(path, O_RDONLY);
#endif
char buf[255];

// clear pipe
Expand All @@ -970,7 +1013,11 @@ static int create_pipe(const char *path, int is_read )
ret = mkfifo(path, 0666);
}
int mode = (is_read ? O_RDONLY : O_RDWR );
#ifdef OS_LINUX
int fd = open(path, mode | O_NONBLOCK);
#else
int fd = open(path, mode );
#endif
//printf("open: path=%s fd=%d errno=%d\n",path,fd,errno);

return fd;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/target/target_os_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
*/
#define TARGET_OS_SOCKET_TYPE int
#else
#include <windows.h>
#include <winsock2.h>
#include <windows.h>
/*
* Winsock
*/
Expand Down
4 changes: 4 additions & 0 deletions src/lib/tcp/tcp_client.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include "tcp/tcp_client.h"
#include <sys/types.h>
#ifdef OS_LINUX
#include <sys/socket.h>
#include <netinet/in.h>
#else
#include <winsock2.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <string.h>
Expand Down
12 changes: 12 additions & 0 deletions src/lib/tcp/tcp_connection.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "tcp_connection.h"
#include <sys/types.h>
#ifdef OS_LINUX
#include <sys/socket.h>
#else
#include <winsock2.h>
#endif
#include <errno.h>
#include <stdio.h>

Expand All @@ -27,7 +31,11 @@ Std_ReturnType tcp_connection_send_nblk(TcpConnectionType *connection, const cha
ssize_t snd_size;

*res = 0;
#ifdef OS_LINUX
snd_size = send(connection->socket.fd, data, size, MSG_DONTWAIT);
#else
snd_size = send(connection->socket.fd, data, size, 0);
#endif
if (snd_size < 0) {
if (errno != EAGAIN) {
printf("ERROR: tcp_connection_send() errno=%d\n", errno);
Expand All @@ -47,7 +55,11 @@ Std_ReturnType tcp_connection_receive_nblk(TcpConnectionType *connection, char *
{
ssize_t rcv_size;
*res = 0;
#ifdef OS_LINUX
rcv_size = recv(connection->socket.fd, data, size, MSG_DONTWAIT);
#else
rcv_size = recv(connection->socket.fd, data, size, 0);
#endif
if (rcv_size < 0) {
if (errno != EAGAIN) {
printf("ERROR: tcp_connection_receive() errno=%d\n", errno);
Expand Down
9 changes: 8 additions & 1 deletion src/lib/tcp/tcp_server.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include "tcp/tcp_server.h"
#include <sys/types.h>
#ifdef OS_LINUX
#include <sys/socket.h>
#include <netinet/in.h>
#else
#include <winsock2.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -39,8 +43,11 @@ Std_ReturnType tcp_server_create(const TcpServerConfigType *config, TcpServerTyp
Std_ReturnType tcp_server_accept(const TcpServerType *server, TcpConnectionType *connection)
{
struct sockaddr_in addr;
#ifdef OS_LINUX
socklen_t len = sizeof(struct sockaddr_in);

#else
int len = sizeof(struct sockaddr_in);
#endif
//printf("tcp_server_accept: fd=%d\n", server->socket.fd);
connection->socket.fd = accept(server->socket.fd, (struct sockaddr *)&addr, &len);
if (connection->socket.fd < 0) {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/tcp/tcp_socket.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#include "tcp_socket.h"
#include <unistd.h>
#include <sys/types.h>
#ifdef OS_LINUX
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#else
#include <winsock2.h>
#endif
#include <stdio.h>
#include <errno.h>
#include <string.h>
Expand Down

0 comments on commit 8c3732f

Please sign in to comment.