Listener.d:在不干净的断开连接时崩溃
对于我正在制作的小型聊天服务器,我决定使用 D;找到我自己 通过listener.d 中的一个非常简洁的例子来开始我决定 差不多就拿这个例子来说吧!然而,我陷入了一个我无法真正解决的错误 把我的手指绕起来。很可能是我自己的错,我正在做某事 错了,但考虑到我几乎从示例中获取了代码,我更 倾向于相信这个例子已经被破坏了。
我将解释发生的情况:
- 列出项目
- 我启动我的服务器(没有错,它正在按预期运行并侦听)
- 我远程登录到它。我的服务器接受连接。
- 我使用 telnet 发送一些信息。服务器处理信息 再次正确,没问题。
- 我通过使用 ^] 退出 telnet,然后写 quit。断开连接 相当不优雅。
- 服务器正确识别这不是干净的断开连接并执行 删除套接字的代码。
- 然后我得到了范围违规。
这是主要过程和循环: https://github.com/JGBrands/BlaatServer/blob/master/source /bserver.d
这是服务器类,删除套接字的代码位于 函数中完成底层 void destroySocket(int index);
https://github.com/JGBrands/BlaatServer/blob/master/source /server.d
实际上让我复制粘贴它。 :-)
void destroySocket(int index) {
this.reads[index].close(); /* release resources. */
/* Remove the socket now. We don't want this around! It'll crash us! */
if (index != this.reads.length -1)
this.reads[index] = this.reads[this.reads.length -1];
this.reads = this.reads[0 .. this.reads.length -1];
writeln("Total connections: " ~ to!string(this.reads.length));
}
代码主要是从listener.d示例中接管的,就像我说的, 我得到的错误是这样的:
core.exception.RangeError@server(61): Range violation
----------------
----------------
我相信该函数正在删除不应该删除的内容,对于那些 有兴趣,这是 server.d 中的第 61 行:
if (this.sset.isSet(this.reads[i])) {
希望你们能帮助我对此有更多的理解,我是否在这里遗漏了一些非常明显的东西?
For a small chat server thingie I'm making, I decided to use D; finding myself
with a very neat example from listener.d to get a kick start I decided to
pretty much take the example over! However, I'm stuck on a bug I can't truly
wrap my finger around. Most likely it's my own fault and I'm doing something
wrong, but considering I took the code pretty much from the example I am more
inclined to believe the example is broken.
I'll explain what happens:
- List item
- I start up my server (nothing wrong, it's running as it should and listening)
- I telnet to it. My server accepts the connection.
- I use telnet to send some information. Server handles the information
properly, again, no issue. - I quit telnet by using ^] and then writing quit. Breaking the connection
rather ungracefully. - The server properly recognises this isn't a clean disconnect and executes
the code to remove the socket. - I then get a range violation.
This is the main process and it's loop:
https://github.com/JGBrands/BlaatServer/blob/master/source/bserver.d
This is the server class, the code where it deletes the socket is at the
complete bottom in the function void destroySocket(int index);
https://github.com/JGBrands/BlaatServer/blob/master/source/server.d
Actually let me copy paste that. :-)
void destroySocket(int index) {
this.reads[index].close(); /* release resources. */
/* Remove the socket now. We don't want this around! It'll crash us! */
if (index != this.reads.length -1)
this.reads[index] = this.reads[this.reads.length -1];
this.reads = this.reads[0 .. this.reads.length -1];
writeln("Total connections: " ~ to!string(this.reads.length));
}
The code is primarily taken over from the listener.d example like I said, the
error I get is this:
core.exception.RangeError@server(61): Range violation
----------------
----------------
I'm lead to believe the function is deleting something it shouldn't, for those
interested, this is line 61 in server.d:
if (this.sset.isSet(this.reads[i])) {
Hopefully you guys can help me make more sense about this, am I missing something really obvious here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如棘轮所指出的:
这并不能验证索引是否在范围内。它仅验证索引不是最后一个元素。只要索引已被验证在范围内就可以了。
As ratchet points out:
This does not verify that index is within range. It only validates that index is not the last element. This is fine as long as index has already been verified to be within range.