vijayyande / spserver

Automatically exported from code.google.com/p/spserver
Other
0 stars 0 forks source link

如何在SP_IocpServer中随时发送一个数据到客户端,或者关闭一个连接 #11

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
如题,非SP_IocpDispatcher模式

Original issue reported on code.google.com by xdi...@gmail.com on 11 Jan 2009 at 12:33

GoogleCodeExporter commented 9 years ago
SP_IocpServer 没有实现类似的功能,只有在 Dispatcher 上有实现。
如果需要类似的功能,建议使用 Dispatcher ,从 Server 切换为 
Dispatcher ,应该非常简单。

Original comment by stephen....@gmail.com on 11 Jan 2009 at 12:46

GoogleCodeExporter commented 9 years ago
改了下下源码,实现成了我喜欢的框架,很简单,终于又把网络��
�构跟程序分开了.

SP_IocpServer   *   iocpServer;

void CALLBACK NotifyProc(SP_Session * session, int code, void *arg)
{
    char *clientIp = session->getRequest()->getClientIP();
    switch (code)
    {
    case NC_CLIENT_CONNECT:
        break;
    case NC_CLIENT_DISCONNECT:
        break;
    case NC_CLIENT_TIMEOUT:
        break;
    case NC_RECEIVE_COMPLETE:
        break;
    }
}

int main(int argc, char* argv[])
{

    spwin32_initsocket();

    iocpServer = new SP_IocpServer("", 81, new SP_IocpHandlerFactory( new 
SP_OnlineSidList(), NotifyProc, (void *)0x567));

    iocpServer->setMaxConnections( 3333333 );
    iocpServer->setMaxThreads(3);
    iocpServer->setTimeout(33);
    iocpServer->runForever();
    printf("%d\n", iocpServer->isRunning());
    return 0;
}

多谢有这么好的开源模型, 另建议下个版本集成下回调模式

Original comment by xdi...@gmail.com on 17 Jan 2009 at 4:26

GoogleCodeExporter commented 9 years ago
发送断开就用以下代码实现了,脱离了框架,不知道可否有未考
虑完善之处,请作者指出
int sendPacket(SP_Session * session, void * buf, int len)
{
    SP_Response * response = new SP_Response( session->getSid() );

    PACKETHDR   hdr;
    hdr.signature = PACKET_SIGNATURE;
    hdr.len = len;

    SP_Buffer * spBuffer = response->getReply()->getMsg();
    spBuffer->append(&hdr, sizeof(hdr));
    spBuffer->append(buf, len);
    response->getReply()->setCompletionKey( ++mMsgSeq );

    ((SP_IocpSession_t*)session->getArg())->mEventArg->getResponseQueue()->push
(response);

    return len;
}

void disconnect(SP_Session * session)
{
    session->setStatus( SP_Session::eWouldExit );
    session->setRunning( 0 );   
}

Original comment by xdi...@gmail.com on 17 Jan 2009 at 4:28

GoogleCodeExporter commented 9 years ago
又看了看代码,disconnect应该这样写
void disconnect(SP_Session * session)
{
    SP_Sid_t sid = session->getSid();
    SP_Response * response = new SP_Response( sid );
    response->getToCloseList()->add( sid );
    ((SP_IocpSession_t*)session->getArg())->mEventArg->getResponseQueue()->push
(response); 
}

Original comment by xdi...@gmail.com on 17 Jan 2009 at 10:00

GoogleCodeExporter commented 9 years ago
为什么一定要这样改呢?Dispatcher 有什么不好吗?Dispatcher 
一样是把网络和业务分开的。

这样直接操作 ResponseQueue 有可能会存在多线程竞态的问题。

Original comment by liusi...@gmail.com on 21 Jan 2009 at 2:06

GoogleCodeExporter commented 9 years ago

Original comment by stephen....@gmail.com on 15 Mar 2009 at 7:24