webassembly中的UDP套接字 [英] UDP socket at webassembly

查看:65
本文介绍了webassembly中的UDP套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我用 C 和 C++ 编写的桌面应用程序移植到 webassembly 平台,并且正在调查是否可行.该应用程序所做的一项重要工作是通过发送和接收 UDP 消息进行通信.我已经实现了最小的 UDP 客户端,它只创建 UDP 套接字并将数据包发送到服务器(它是本地构建的,并在同一台机器上作为单独的可执行文件运行).socket、bind 和 sendto API 没有返回错误,一切正常,但服务器端没有收到任何消息,并且wireshark 在该端口上没有显示任何活动.

I'm trying to port my desktop app written in C and C++ to webassembly platform and am investigating if it is possible at all. One of important things the app does is communicate by sending and receiving UDP messages. I have implemented minimal UDP client which just creates UDP socket and sends packets to server (which is build natively and is running as separate executable at the same machine). socket, bind and sendto APIs return no error and everything looks working but no messages are receiving on server side and wireshark shows no activity on that port.

UDP 套接字是仅在 webassembly libc 端口上存根,还是在某些 Web 标准连接(例如 WebRTC)之上实现?

Is UDP socket just stubbed at webassembly libc port, or it is implemented on top of some web standard connection (e.g. WebRTC)?

客户端代码如下.我检查了本机构建是否正常工作.

The client code is below. I checked that native build is working properly.

#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#define BUFLEN 512
#define NPACK 100
#define PORT 9930

void diep(char *s)
{
  perror(s);
  exit(1);
}

#define SRV_IP "127.0.0.1"


int main(void)
{
  struct sockaddr_in si_other;
  int s, i, slen=sizeof(si_other);
  char buf[BUFLEN];

  if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
    diep("socket");

  memset((char *) &si_other, 0, sizeof(si_other));
  si_other.sin_family = AF_INET;
  si_other.sin_port = htons(PORT);
  if (inet_aton(SRV_IP, &si_other.sin_addr)==0) {
    fprintf(stderr, "inet_aton() failed\n");
    exit(1);
  }

  for (i=0; i<NPACK; i++) {
    printf("Sending packet %d\n", i);
    sprintf(buf, "This is packet %d\n", i);
    if (sendto(s, buf, BUFLEN, 0, (struct sockaddr*)&si_other, slen)==-1)
      diep("sendto()");
  }

  close(s);
  return 0;
}

我遵循了 http://webassembly.org/getting-started/developers-guide 的说明/ 来构建和运行它.

I followed instructions from http://webassembly.org/getting-started/developers-guide/ to build and run it.

预先感谢您提供任何帮助或线索!

Thanks in advance for any help or clues!

推荐答案

我发现了 UDP 套接字是如何在 webassembly 中实现的.实际上,它们是由 websockets 模拟的.如果客户端和服务器都是网络组件,它可能会工作,但我的服务器是本地构建的.由于wasm不支持动态链接,所有的代码(包括libc实现)都捆绑在一个JS文件中,我们是否可以找到UDP sendto实现:

I found how UDP sockets are implemented at webassembly. Actually, they are emulated by websockets. It probably would work if both client and server were webassemblies, but my server is built natively. As wasm doesn't support dynamic linking, all the code (including libc implementation) is bundled to one JS file, were we can find UDP sendto implementation:

// if we're emulating a connection-less dgram socket and don't have
      // a cached connection, queue the buffer to send upon connect and
      // lie, saying the data was sent now.
      if (sock.type === 2) {
        if (!dest || dest.socket.readyState !== dest.socket.OPEN) {
          // if we're not connected, open a new connection
          if (!dest || dest.socket.readyState === dest.socket.CLOSING || dest.socket.readyState === dest.socket.CLOSED) {
            dest = SOCKFS.websocket_sock_ops.createPeer(sock, addr, port);
          }
          dest.dgram_send_queue.push(data);
          return length;
        }
      }

这篇关于webassembly中的UDP套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆