假设外部组件包含内部组件,并且我们希望将内部组件中的事件传播到外部组件。如果不使用商店,有两种方法可以做到这一点:
Inner.svelte:使用 Svelte 的调度程序来调度原始事件的重新打包版本:
<input type="text" on:input={callDispatcher} />
const dispatcher = createEventDispatcher();
function callDispatcher(e) {
dispatcher("mymsg", {
foo: e.target.value
});
}
Run Code Online (Sandbox Code Playgroud)
Outer.svelte:监听 Inner 的调度事件:
<Inner on:mymsg={handler} />
function handler(e) {
alert(e.detail.foo);
}
Run Code Online (Sandbox Code Playgroud)
Inner.svelte:接受 Outer 传入的处理程序:
export let externalHandler;
<input type="text" on:input={externalHandler} />
Run Code Online (Sandbox Code Playgroud)
Outer.svelte:当感兴趣的 Inner 事件发生时,它将调用 Outer 的处理程序:
<Inner externalHandler={handler} />
function handler(e) {
alert(e.target.value);
}
Run Code Online (Sandbox Code Playgroud)
哪一种是更好的做法?方法1的调度程序似乎是一个不必要的中间层,不仅增加了更多的代码,而且还丢失了原始的事件信息。但奇怪的是,Svelte 教程提到了方法 1,而不是方法 2。
使用 ctypesgen,我生成了一个结构体(我们称之为 mystruct),其字段定义如下:
[('somelong', ctypes.c_long),
('somebyte', ctypes.c_ubyte)
('anotherlong', ctypes.c_long),
('somestring', foo.c_char_Array_5),
]
Run Code Online (Sandbox Code Playgroud)
当我尝试将该结构的实例(我们称之为 x)写入文件时: open(r'rawbytes', 'wb').write(mymodule.mystruct(1, 2, 3, '12345')),我注意到写入文件的内容不是字节对齐的。
我应该如何将该结构写入文件以使字节对齐为 1 字节?
我有一个 TCP 服务器和多个客户端几乎同时尝试连接到该服务器。我注意到:
\n\n在客户端,connect即使3次握手尚未完成,也可能返回0。
在服务器端,accept即使完成3次握手也可能不会返回。
为了说明这两点,下面是 Wireshark 跟踪(服务器正在侦听端口 1234):
\n\n1. 以下是客户端返回 0 的情况的 Wireshark 跟踪,connect即使 3 次握手尚未完成(缺少来自客户端的最后一个 SYN):
// calls ::connect ...\n59507 \xe2\x86\x92 1234 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 WS=256 SACK_PERM=1\n1234 \xe2\x86\x92 59507 [SYN, ACK] Seq=0 Ack=1 Win=65535 Len=0 MSS=1460 WS=256 SACK_PERM=1\n// ... and ::connect returned 0, despite the above 2 lines \n// forming an incomplete handshake. Why?\n\n// after the ::connect, client calls ::send to send 8 …Run Code Online (Sandbox Code Playgroud)