Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

udp数据可能被误读 #216

Open
kongxa opened this issue Mar 1, 2024 · 1 comment
Open

udp数据可能被误读 #216

kongxa opened this issue Mar 1, 2024 · 1 comment

Comments

@kongxa
Copy link

kongxa commented Mar 1, 2024

在UdpServer.cpp中,UdpServer在接收到对端发送的首个数据后,会根据本地地址和对端地址创建新的socket。在创建新socket的时候首先执行bindUdpSock,在bindPeerAddr之前的这一瞬间会不会接收其他对端的数据包?
从我当前的程序看是存在这种情况的。
运行环境:debian11
使用地址:127.0.0.1
测试方式:1个UdpServer,500个udp client同时发送数据

        lock_guard<std::recursive_mutex> lck(*_session_mutex);
        auto it = _session_map->find(id);
        if (it != _session_map->end()) {
            return it->second;
        }
        assert(_socket);
        socket->bindUdpSock(_socket->get_local_port(), _socket->get_local_ip());
        socket->bindPeerAddr((struct sockaddr *) addr_str.data(), addr_str.size());
        auto helper = _session_alloc(server, socket);
        helper->session()->attachServer(*this);
@kongxa kongxa changed the title udp数据可能被读取两次 udp数据可能被误读 Mar 1, 2024
@xia-chu
Copy link
Member

xia-chu commented Mar 2, 2024

可能 所以后面代码做了判断:

socket->setOnRead([weak_self, weak_helper, id](const Buffer::Ptr &buf, struct sockaddr *addr, int addr_len) {
            auto strong_self = weak_self.lock();
            if (!strong_self) {
                return;
            }

            //快速判断是否为本会话的的数据, 通常应该成立
            if (id == makeSockId(addr, addr_len)) {
                if (auto strong_helper = weak_helper.lock()) {
                    emitSessionRecv(strong_helper, buf);
                }
                return;
            }

            //收到非本peer fd的数据,让server去派发此数据到合适的session对象
            strong_self->onRead_l(false, id, buf, addr, addr_len);
        });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants