示例#1
0
 void Slave::MessageReceived(ChannelHandlerContext& ctx, MessageEvent<RedisMessage>& e)
 {
     if (e.GetMessage()->IsReply())
     {
         HandleRedisReply(ctx.GetChannel(), e.GetMessage()->reply);
     }
     else if (e.GetMessage()->IsCommand())
     {
         HandleRedisCommand(ctx.GetChannel(), e.GetMessage()->command);
     }
     else
     {
         HandleRedisDumpChunk(ctx.GetChannel(), e.GetMessage()->chunk);
     }
 }
示例#2
0
bool ChannelService::EventSunk(ChannelPipeline* pipeline, MessageEvent<DatagramPacket>& e)
{
    DatagramPacket* packet = e.GetMessage();
    Channel* ch = e.GetChannel();
    DatagramChannel* sc = (DatagramChannel*) ch;
    return sc->Send(packet->GetInetAddress(), &(packet->GetBuffer())) > 0;
}
示例#3
0
文件: master.cpp 项目: yinqiwen/comms
 void Master::MessageReceived(ChannelHandlerContext& ctx, MessageEvent<RedisCommandFrame>& e)
 {
     RedisCommandFrame* cmd = e.GetMessage();
     DEBUG_LOG("Master recv cmd from slave:%s", cmd->ToString().c_str());
     if (!strcasecmp(cmd->GetCommand().c_str(), "replconf"))
     {
         if (cmd->GetArguments().size() == 2 && !strcasecmp(cmd->GetArguments()[0].c_str(), "ack"))
         {
             int64 offset;
             if (string_toint64(cmd->GetArguments()[1], offset))
             {
                 SlaveConnTable::iterator found = m_slaves.find(ctx.GetChannel()->GetID());
                 if (found != m_slaves.end())
                 {
                     SlaveConn* slave = found->second;
                     if (NULL != slave)
                     {
                         slave->acktime = time(NULL);
                         slave->ack_offset = offset;
                     }
                 }
             }
         }
     }
 }
示例#4
0
		void MessageReceived(ChannelHandlerContext& ctx,
		        MessageEvent<Buffer>& e)
		{
			total_recv += (e.GetMessage()->ReadableBytes());
			Buffer send(7);
			send.Write("Hello\r\n", 7);
			ctx.GetChannel()->Write(send);
			//total_received++;
			//DEBUG_LOG("Received response with total:%d", total_received);
			//            if (total_received == total_would_send)
			//            {
			//                ctx.GetChannel()->Close();
			//                ctx.GetChannel()->GetService().Stop();
			//            }
			//            else if (total_received % 2000 == 0)
			//            {
			//                //INFO_LOG("Received %d response\n", total_received);
			//                Buffer buf(16);
			//                buf.Write("Hello,world\r\n", 13);
			//                for (uint32 i = 0; i < 2000; i++)
			//                {
			//                    if (!ctx.GetChannel()->Write(buf))
			//                    {
			//                        ERROR_LOG("Failed to write buf to channel with total send:%d", total_send);
			//                        continue;
			//                    }
			//                    total_send++;
			//                    buf.SetReadIndex(0);
			//                }
			//                //INFO_LOG("Write %d request\n", total_send);
			//            }
		}
示例#5
0
bool ChannelService::EventSunk(ChannelPipeline* pipeline, MessageEvent<Buffer>& e)
{
    Buffer* buffer = e.GetMessage();
    RETURN_FALSE_IF_NULL(buffer);
    uint32 len = buffer->ReadableBytes();
    int32 ret = e.GetChannel()->WriteNow(buffer);
    return ret >= 0 ? (len == (uint32) ret) : false;
}
示例#6
0
bool RedisCommandEncoder::WriteRequested(ChannelHandlerContext& ctx, MessageEvent<RedisCommandFrame>& e)
{
    RedisCommandFrame* msg = e.GetMessage();
    m_buffer.Clear();
    if (Encode(m_buffer, *msg))
    {
        return ctx.GetChannel()->Write(m_buffer);
    }
    return false;
}
示例#7
0
bool RedisCommandEncoder::WriteRequested(ChannelHandlerContext& ctx, MessageEvent<RedisCommandFrame>& e)
{
    RedisCommandFrame* msg = e.GetMessage();
    if (Encode(ctx.GetChannel()->GetOutputBuffer(), *msg))
    {
        ctx.GetChannel()->EnableWriting();
        return true;
    }
    return false;
}
示例#8
0
				void MessageReceived(ChannelHandlerContext& ctx,
						MessageEvent<Buffer>& e)
				{
					Buffer* input = e.GetMessage();

					if (m_cumulation.Readable())
					{
						m_cumulation.DiscardReadedBytes();
						m_cumulation.Write(input, input->ReadableBytes());
						CallDecode(ctx, e.GetChannel(), m_cumulation);
					} else
					{
						CallDecode(ctx, e.GetChannel(), *input);
						if (input->Readable())
						{
							m_cumulation.Write(input, input->ReadableBytes());
						}
					}
				}
示例#9
0
void FastRedisCommandDecoder::MessageReceived(ChannelHandlerContext& ctx, MessageEvent<Buffer>& e)
{
    Buffer& buffer = *(e.GetMessage());
    bool have_empty = false;
    while (buffer.GetRawReadBuffer()[0] == '\r' || buffer.GetRawReadBuffer()[0] == '\n')
    {
        buffer.AdvanceReadIndex(1);
        have_empty = true;
    }
    if(!m_ignore_empty && have_empty)
    {
        m_cmd.Clear();
        fire_message_received<RedisCommandFrame>(ctx, &m_cmd, NULL);
    }
    Channel* ch = ctx.GetChannel();
    std::string err;
    size_t mark_read_index = buffer.GetReadIndex();
    while (buffer.Readable() && !ch->IsReadBlocked())
    {
        /* Determine request type when unknown. */
        if (!m_reqtype)
        {
            if (buffer.GetRawReadBuffer()[0] == '*')
            {
                m_reqtype = REDIS_REQ_MULTIBULK;
            }
            else
            {
                m_reqtype = REDIS_REQ_INLINE;
            }
        }

        if (m_reqtype == REDIS_REQ_INLINE)
        {
            m_cmd.Clear();
            if (RedisCommandDecoder::ProcessInlineBuffer(buffer, m_cmd) == 1)
            {
                fire_message_received<RedisCommandFrame>(ctx, &m_cmd, NULL);
            }
            else
            {
                break;
            }
        }
        else if (m_reqtype == REDIS_REQ_MULTIBULK)
        {
            if (ProcessMultibulkBuffer(buffer, err) == 1)
            {
                size_t raw_data_size = buffer.GetReadIndex() - mark_read_index;
                m_cmd.m_raw_msg.WrapReadableContent(buffer.GetRawReadBuffer() - raw_data_size, raw_data_size);
                fire_message_received<RedisCommandFrame>(ctx, &m_cmd, NULL);
            }
            else
            {
                break;
            }
        }
        else
        {
            err = "Unknown request type";

        }
        if (!err.empty())
        {
            APIException ex(err);
            fire_exception_caught(ctx.GetChannel(), ex);
            ERROR_LOG("Exception:%s occured.", err.c_str());
        }
    }
}
示例#10
0
 void CoroChannel::MessageReceived(ChannelHandlerContext& ctx, MessageEvent<Buffer>& e)
 {
     Buffer* buf = e.GetMessage();
     SetReadedContent(buf);
     WakeupCoro();
 }
示例#11
0
    void CoroRedisClient::MessageReceived(ChannelHandlerContext& ctx, MessageEvent<RedisReply>& e)
    {
        SetReply(e.GetMessage());

    }
void ServiceIPCEventHandler::MessageReceived(ChannelHandlerContext& ctx,
        MessageEvent<IPCEvent>& e)
{
#define CHECK_SEVICE_HANDLER() do{  \
		if (NULL == m_handler)  \
			{                   \
			    if( GetServiceProcess()->GetServiceType() == DISPATCHER_SERVICE_PROCESS_TYPE) return; \
				WARN_LOG("ServiceHandler is NULL for handling IPC event."); \
				return;                                                     \
			}                                                               \
         }while(0)


	IPCEvent* ipc_event = e.GetMessage();
	IPCEventType type = ipc_event->GetType();
	switch (type)
	{
		case SOCKET_MSG:
		{
			CHECK_SEVICE_HANDLER();
			SocketMessageEvent* msg = (SocketMessageEvent*) ipc_event;
			uint32 reportSocketChannelID = msg->GetChannelID();
			if (m_has_dispatcher)
			{
				uint32 realID = msg->GetChannelID();
				Channel* src_channel = e.GetChannel();
				VirtualChannelHelper::GetVirtualSocketID(src_channel, realID,
				        reportSocketChannelID);
			}
			if (NULL != msg->GetContent())
			{
				if (NULL != msg->GetSocketInetAddress())
				{
					m_handler->OnSocketMessage(reportSocketChannelID,
					        *(msg->GetContent()),
					        *(msg->GetSocketInetAddress()));
				}
				else
				{
					m_handler->OnSocketMessage(reportSocketChannelID,
					        *(msg->GetContent()));
				}
			}
			break;
		}
		case IPC_MSG:
		{
			CHECK_SEVICE_HANDLER();
			IPCMessageEvent* msg = (IPCMessageEvent*) ipc_event;
			if (NULL != msg->GetContent())
			{
				m_handler->OnIPCMessage(msg->GetSrcType(), msg->GetSrcIndex(),
				        *(msg->GetContent()));
			}
			break;
		}
		case ADMIN:
		{
			CHECK_SEVICE_HANDLER();
			IPCMessageEvent* msg = (IPCMessageEvent*) ipc_event;
			if (NULL != msg->GetContent())
			{
				AdminCommand cmd;
				if (0
				        == AdminCommandHandler::DecodeCommand(cmd,
				                msg->GetContent()))
				{
					std::string result;
					Buffer tmp;
					if (AdminCommandHandler::HandleCommand(cmd, result) >= 0)
					{
						tmp.EnsureWritableBytes(result.size() + 2);
						tmp.Printf("%s\r\n", result.c_str());
					}
					else
					{
						tmp.Printf("Failed to handler command:%s\r\n",
						        cmd.name.c_str());
					}
					VirtualChannelHelper::WriteSocket(cmd.admin_channel_id,
					        tmp);
				}
			}
			break;
		}
		case IPC_CTRL:
		{
			IPCCtrlEvent* event = (IPCCtrlEvent*) ipc_event;
			switch (event->GetIPCCtrlType())
			{
				case SERV_PROC_STARTED:
				{
					CHECK_SEVICE_HANDLER();
					m_handler->OnServiceProcessStarted(event->GetSrcType(),
					        event->GetSrcIndex());
					break;
				}
				case SERV_PROC_STOPED:
				{
					CHECK_SEVICE_HANDLER();
					m_handler->OnServiceProcessStoped(event->GetSrcType(),
					        event->GetSrcIndex());
					break;
				}
				default:
				{
					break;
				}
			}
			break;
		}
		case SOCKET_CTRL:
		{
			CHECK_SEVICE_HANDLER();
			SocketCtrlEvent* event = (SocketCtrlEvent*) ipc_event;
			uint32 reportSocketChannelID = event->GetSocketChannelID();
			if (m_has_dispatcher)
			{
				uint32 realID = event->GetSocketChannelID();
				Channel* src_channel = ctx.GetChannel();
				VirtualChannelHelper::GetVirtualSocketID(src_channel, realID,
				        reportSocketChannelID);
			}
			switch (event->GetSocketCtrlType())
			{
				case SOCKET_OPENED:
				{
					m_handler->OnSocketOpened(reportSocketChannelID);
					break;
				}
				case SOCKET_CONNECTED:
				{
					if (NULL != event->GetAddress())
					{
						m_handler->OnSocketConnected(reportSocketChannelID,
						        *(event->GetAddress()));
					}
					else if (NULL != event->GetUnixAddress())
					{
						m_handler->OnSocketConnected(reportSocketChannelID,
						        *(event->GetUnixAddress()));
					}

					break;
				}
				case SOCKET_CLOSED:
				{
					m_handler->OnSocketClosed(reportSocketChannelID);
					if (m_has_dispatcher)
					{
						VirtualChannelHelper::ClearTable(ctx.GetChannel(),
						        event->GetSocketChannelID());
					}
					break;
				}
				case SOCKET_CONNECT_FAILED:
				{
					if (NULL != event->GetAddress())
					{
						m_handler->OnSocketConnectFailed(reportSocketChannelID,
						        *(event->GetAddress()));
					}
					else if (NULL != event->GetUnixAddress())
					{
						m_handler->OnSocketConnectFailed(reportSocketChannelID,
						        *(event->GetUnixAddress()));
					}
					if (m_has_dispatcher)
					{
						VirtualChannelHelper::ClearTable(ctx.GetChannel(),
						        event->GetSocketChannelID());
					}
					break;
				}
				default:
				{
					ERROR_LOG(
					        "Unknown socket ctrl type %u", event->GetSocketCtrlType());
					break;
				}
			}
			break;
		}
		default:
		{
			ERROR_LOG("Unknown ipc event type %u", ipc_event->GetType());
			break;
		}
	}
}