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

CommSession、CommTarget、CommService、CommConnection含义 #200

Closed
zhouchunliang opened this issue Jan 6, 2021 · 4 comments
Closed

Comments

@zhouchunliang
Copy link

Communicator.cc中的struct CommConnEntry中CommSession、CommTarget、CommService与CommConnection的含义?
image

@Barenboim
Copy link
Contributor

Barenboim commented Jan 6, 2021

CommTarget是通讯目标,基本上就是ip+port, 还有两个超时参数。连接池什么的都在target里。
CommSession是一次req->resp的交互,主要要实现message_in(), message_out()等几个虚函数,让核心知道怎么产生消息。
CommService就是服务了,主要是new_session()的实现,因为对server来讲,session是被动产生的。
Communicator::request(CommSession *session, CommTarget *target)这个接口就可以实现一个异步的网络请求了,你可以写个程序试试,Session大概这么派生:

class MyHttpSession : public CommSession
{
private:
    CommMessageOut *message_out() override { return new protocol::HttpRequest; }
    CommMessageIn *message_in() override { return new protocol::HttpResponse; }
    void handle(int state, int error) override
    {
        protocol::HttpResponse *resp = (protocol::HttpResponse *)this->get_message_in();
        // print resp
        ...
        delete this->get_message_out();
        delete this->get_message_in();
        delete this;
    }
};

int main()
{
    Communicator comm;
    comm.init(2, 10);
    CommTarget target;
    target.init(...);
    comm.request(new MyHttpSession, &target);
    ...
}

@sourcema
Copy link

sourcema commented Feb 28, 2023

image
请问下CommConnection只有析构方法,为什么这么定义呢,出于什么考虑呢?

@Barenboim
Copy link
Contributor

Barenboim commented Feb 28, 2023

image 请问下CommConnection只有析构方法,为什么这么定义呢,出于什么考虑呢?

Target或Service里有个new_connection可以产生这个类的派生,用户可以给连接设置上下文。

@sourcema
Copy link

image 请问下CommConnection只有析构方法,为什么这么定义呢,出于什么考虑呢?

Target或Service里有个new_connection可以产生这个类的派生,用户可以给连接设置上下文。

多谢

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

3 participants