ChannelHandler* ChannelPipeline::Remove(ChannelHandlerContext* ctx) { RETURN_NULL_IF_NULL(ctx); ChannelHandler* handler = ctx->GetHandler(); if (m_head == m_tail) { m_head = NULL; m_tail = NULL; m_name2ctx.clear(); DELETE(ctx); return handler; } else if (ctx == m_head) { return RemoveFirst(); } else if (ctx == m_tail) { return RemoveLast(); } else { //callBeforeRemove(ctx); ChannelHandlerContext* prev = ctx->GetPrev(); ChannelHandlerContext* next = ctx->GetNext(); prev->SetNext(next); next->SetPrev(prev); m_name2ctx.erase(ctx->GetName()); DELETE(ctx); //callAfterRemove(ctx); return handler; } }
void MessageReceived(ChannelHandlerContext& ctx, MessageEvent< Buffer>& e) { 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); } }
void DispatcerIPCEventHandler::ChannelClosed(ChannelHandlerContext& ctx, ChannelStateEvent& e) { DEBUG_LOG("Dispatcher(%s_%u) close a client.",g_service_proc->GetServiceName().c_str(), g_service_proc->GetProcessIndex()); ChannelList::iterator it = m_client_channel_list.begin(); while (it != m_client_channel_list.end()) { if (ctx.GetChannel() == *it) { m_client_channel_list.erase(it); break; } it++; } IPCChannelTable::iterator tit = m_ipc_channel_table.begin(); while (tit != m_ipc_channel_table.end()) { if (ctx.GetChannel() == tit->second) { ProcessAddress addr = tit->first; m_ipc_channel_table.erase(tit); DispatcherProcess* proc = (DispatcherProcess*) g_service_proc; if (proc->GetIPCType() == IPC_FIFO) { proc->CreateServiceProcessIPCFIFOChannel(addr); } break; } tit++; } }
void ChannelPipeline::GetAll(vector<ChannelHandler*> handlerlist) { ChannelHandlerContext* ctx = m_head; while(NULL != ctx) { handlerlist.push_back(ctx->GetHandler()); ctx = ctx->GetNext(); } }
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; }
ChannelHandlerContext* ChannelPipeline::GetActualDownstreamContext( ChannelHandlerContext* ctx) { RETURN_NULL_IF_NULL(ctx); ChannelHandlerContext* realCtx = ctx; while (!realCtx->CanHandleDownstream()) { realCtx = realCtx->GetPrev(); RETURN_NULL_IF_NULL(realCtx); } return realCtx; }
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); } }
void ServiceIPCEventHandler::ChannelClosed(ChannelHandlerContext& ctx, ChannelStateEvent& e) { Channel* ch = ctx.GetChannel(); Address* addr = const_cast<Address*>(ch->GetRemoteAddress()); Address* localaddr = const_cast<Address*>(ch->GetLocalAddress()); if (NULL != addr && InstanceOf<SocketUnixAddress>(addr).OK) { SocketUnixAddress* un = (SocketUnixAddress*) addr; SocketUnixAddress* un1 = (SocketUnixAddress*) localaddr; DEBUG_LOG( "Local %s Closed %s", un1->GetPath().c_str(), un->GetPath().c_str()); if (m_service->GetManagerProcess()->IsManagerIPCUnixSocketServer( un->GetPath())) { m_service->SetIPCUnixSocket(NULL); } else if (m_service->GetManagerProcess()->IsManagerCtrlUnixSocketServer( un->GetPath())) { m_service->SetCtrlChannel(NULL); } else { GetServiceProcess()->AddClosedDispatcherIPCChannel(ch); VirtualChannelHelper::ClearTable(ch); } } }
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); // } }
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; } } } } } }
void Master::ChannelWritable(ChannelHandlerContext& ctx, ChannelStateEvent& e) { uint32 conn_id = ctx.GetChannel()->GetID(); SlaveConnTable::iterator found = m_slave_table.find(conn_id); if (found != m_slave_table.end()) { SlaveConnection* slave = found->second; if (slave->state == SLAVE_STATE_SYNING_CACHE_DATA) { if ((uint64) slave->sync_offset >= m_backlog.GetReplEndOffset()) { slave->state = SLAVE_STATE_SYNCED; ChannelOptions options; options.auto_disable_writing = true; slave->conn->Configure(options); } else { if (!m_backlog.IsValidOffset(slave->sync_offset)) { slave->conn->Close(); } else { size_t len = m_backlog.WriteChannel(slave->conn, slave->sync_offset, MAX_SEND_CACHE_SIZE); slave->sync_offset += len; slave->conn->EnableWriting(); } } } } }
inline bool HandleStreamEvent(ChannelHandlerContext& ctx, ChannelStateEvent& e) { //ChannelStateEvent& stateEvent = (ChannelStateEvent&) e; switch (e.GetState()) { case CLOSED: { CloseRequested(ctx, e); break; } case BOUND: { BindRequested(ctx, e); break; } case CONNECTED: { ConnectRequested(ctx, e); break; } default: { ctx.SendDownstream(e); break; } } return true; }
void DispatcerIPCEventHandler::ChannelConnected(ChannelHandlerContext& ctx, ChannelStateEvent& e) { DEBUG_LOG("Dispatcher(%s_%u) accept a client.", g_service_proc->GetServiceName().c_str(), g_service_proc->GetProcessIndex()); Channel* ch = ctx.GetChannel(); Address* remote = const_cast<Address*> (ch->GetRemoteAddress()); if (NULL != remote) { if (InstanceOf<SocketUnixAddress> (remote).OK) { SocketUnixAddress* un = (SocketUnixAddress*) remote; const std::string& path = un->GetPath(); ProcessAddress proc_addr; g_service_proc->GetManagerProcess()->ParseDispatcherClientUnixSocketAddress(path, proc_addr); DEBUG_LOG("Parsed process address %u_%u", proc_addr.proc_type, proc_addr.proc_idx); IPCChannelTable::iterator tit = m_ipc_channel_table.find(proc_addr); if (tit == m_ipc_channel_table.end()) { AddServiceIPCChannel(proc_addr, ch); } } } }
void HttpClientHandler::ChannelConnected(ChannelHandlerContext& ctx, ChannelStateEvent& e) { m_connected = true; m_client = ctx.GetChannel(); g_ready_channels.insert(this); try_send_request(); }
void Slave::ChannelConnected(ChannelHandlerContext& ctx, ChannelStateEvent& e) { DEBUG_LOG("Master conn connected."); Buffer info; info.Printf("info Server\r\n"); ctx.GetChannel()->Write(info); m_slave_state = SLAVE_STATE_WAITING_INFO_REPLY; m_ping_recved_time = time(NULL); m_master_link_down_time = 0; }
void Master::ChannelClosed(ChannelHandlerContext& ctx, ChannelStateEvent& e) { uint32 conn_id = ctx.GetChannel()->GetID(); SlaveConnTable::iterator found = m_slaves.find(conn_id); if (found != m_slaves.end()) { WARN_LOG("Slave %s closed.", found->second->GetAddress().c_str()); found->second = NULL; } }
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; }
ChannelHandlerContext* ChannelPipeline::PriAddFirst(const string& name, ChannelHandler* handler) { if (m_name2ctx.empty()) { return Init(name, handler); } else { CHECK_DUPLICATE_NAME(name); ChannelHandlerContext* oldHead = m_head; ChannelHandlerContext* newHead = NULL; NEW(newHead, ChannelHandlerContext(*this,NULL, oldHead, name, handler)); RETURN_NULL_IF_NULL(newHead); oldHead->SetPrev(newHead); m_head = newHead; m_name2ctx[name] = newHead; return newHead; } }
void Master::ChannelClosed(ChannelHandlerContext& ctx, ChannelStateEvent& e) { uint32 conn_id = ctx.GetChannel()->GetID(); SlaveConnTable::iterator found = m_slave_table.find(conn_id); if (found != m_slave_table.end()) { DELETE(found->second); m_slave_table.erase(found); } LockGuard<ThreadMutex> guard(m_port_table_mutex); m_slave_port_table.erase(conn_id); }
ChannelHandlerContext* ChannelPipeline::PriAddAfter(const string& baseName, const string& name, ChannelHandler* handler) { ChannelHandlerContext* ctx = GetContext(baseName); RETURN_NULL_IF_NULL(ctx); if (ctx == m_tail) { return PriAddLast(name, handler); } else { CHECK_DUPLICATE_NAME(name); ChannelHandlerContext* newCtx = NULL; NEW(newCtx, ChannelHandlerContext(*this, ctx, ctx->GetNext(), name, handler)); RETURN_NULL_IF_NULL(newCtx); ctx->GetNext()->SetPrev(newCtx); ctx->SetNext(newCtx); m_name2ctx[name] = newCtx; return newCtx; } }
ChannelHandlerContext* ChannelPipeline::PriAddLast(const string& name, ChannelHandler* handler) { if (m_name2ctx.empty()) { return Init(name, handler); //return true; } else { CHECK_DUPLICATE_NAME(name); ChannelHandlerContext* oldTail = m_tail; ChannelHandlerContext* newTail = NULL; NEW(newTail, ChannelHandlerContext(*this, oldTail, NULL, name, handler)); RETURN_NULL_IF_NULL(newTail); oldTail->SetNext(newTail); m_tail = newTail; m_name2ctx[name] = newTail; return newTail; } }
void Cleanup(ChannelHandlerContext& ctx, ChannelStateEvent& e) { if (!m_cumulation.Readable()) { ctx.SendUpstream(e); return; } // Make sure all frames are read before notifying a closed channel. CallDecode(ctx, ctx.GetChannel(), m_cumulation); // Call decodeLast() finally. Please note that decodeLast() is // called even if there's nothing more to read from the buffer to // notify a user that the connection was closed explicitly. FrameDecodeResult<T> partialFrame = DecodeLast(ctx, ctx.GetChannel(), m_cumulation); if (partialFrame.msg != NULL) { fire_message_received(ctx, partialFrame.msg, partialFrame.destructor); } ctx.SendUpstream(e); }
void HttpClientHandler::MessageReceived(ChannelHandlerContext& ctx, MessageEvent<HTTPMessage>& e) { g_recved_res++; g_ratio_recved_res++; m_request_sent = false; if (!g_options.keepAlive) { ctx.GetChannel()->Close(); g_all_channels.erase(this); } else { g_ready_channels.insert(this); } try_send_request(); }
void ChannelConnected(ChannelHandlerContext& ctx, ChannelStateEvent& e) { INFO_LOG("Success to connect remote"); Buffer buf(16); buf.Write("Hello,world\r\n", 13); for (uint32 i = 0; i < 2000; i++) { if(ctx.GetChannel()->Write(buf)) { total_send++; } buf.SetReadIndex(0); } //INFO_LOG("Write %d request", total_send); }
void Master::ChannelWritable(ChannelHandlerContext& ctx, ChannelStateEvent& e) { uint32 conn_id = ctx.GetChannel()->GetID(); SlaveConnTable::iterator found = m_slaves.find(conn_id); if (found != m_slaves.end()) { SlaveConn* slave = found->second; if (NULL != slave && slave->state == SYNC_STATE_SYNCED) { DEBUG_LOG("[Master]Slave sync from %lld to %llu at state:%u", slave->sync_offset, g_repl->WALEndOffset(), slave->state); SyncWAL(slave); } } else { WARN_LOG("[Master]No slave found for:%u", conn_id); } }
void ServiceIPCEventHandler::ChannelConnected(ChannelHandlerContext& ctx, ChannelStateEvent& e) { Channel* ch = ctx.GetChannel(); Address* addr = const_cast<Address*>(ch->GetRemoteAddress()); Address* localaddr = const_cast<Address*>(ch->GetLocalAddress()); if (NULL != addr && InstanceOf<SocketUnixAddress>(addr).OK) { SocketUnixAddress* un = (SocketUnixAddress*) addr; SocketUnixAddress* un1 = (SocketUnixAddress*) localaddr; DEBUG_LOG( "Local %s Connected %s", un1->GetPath().c_str(), un->GetPath().c_str()); if (m_service->GetManagerProcess()->IsManagerCtrlUnixSocketServer( un->GetPath())) { ch->DetachFD(); ch->SetIOEventCallback( VirtualChannelHelper::CtrlChannelIOEventCallback, AE_READABLE, ch); // make_fd_blocking(ch->GetReadFD()); } } }
void ReplayingDecoder::cleanup(ChannelHandlerContext& ctx, const ChannelStateEvent& e) { ChannelBufferPtr cumulation = this->cumulation; this->cumulation.reset(); if (!cumulation) { return; } replayable->terminate(); if (cumulation->readable()) { // Make sure all data was read before notifying a closed channel. callDecode(ctx, e.getChannel(), cumulation, SocketAddress::NULL_ADDRESS); } // Call decodeLast() finally. Please note that decodeLast() is // called even if there's nothing more to read from the buffer to // notify a user that the connection was closed explicitly. ChannelMessage partiallyDecoded = decodeLast(ctx, e.getChannel(), replayable, state); if (!partiallyDecoded.empty()) { unfoldAndfireMessageReceived(ctx, partiallyDecoded, SocketAddress::NULL_ADDRESS); } ctx.sendUpstream(e); }
ChannelHandler* ChannelPipeline::RemoveFirst() { ChannelHandlerContext* oldHead = m_head; RETURN_NULL_IF_NULL(oldHead); //callBeforeRemove(oldHead); ChannelHandler* handler = oldHead->GetHandler(); if (NULL == oldHead->GetNext()) { m_head = NULL; m_tail = NULL; m_name2ctx.clear(); } else { oldHead->GetNext()->SetPrev(NULL); m_head = oldHead->GetNext(); m_name2ctx.erase(oldHead->GetName()); } DELETE(oldHead); //callAfterRemove(oldHead); return handler; }
ChannelHandler* ChannelPipeline::RemoveLast() { ChannelHandlerContext* oldTail = m_tail; RETURN_NULL_IF_NULL(m_tail); //callBeforeRemove(oldTail); ChannelHandler* handler = oldTail->GetHandler(); if (NULL == oldTail->GetPrev()) { m_head = NULL; m_tail = NULL; m_name2ctx.clear(); DELETE(oldTail); } else { oldTail->GetPrev()->SetNext(NULL); m_tail = oldTail->GetPrev(); m_name2ctx.erase(oldTail->GetName()); } //callBeforeRemove(oldTail); DELETE(oldTail); return handler; }
ChannelHandler* ChannelPipeline::Get(const string& name) { ChannelHandlerContext* ctx = GetContext(name); RETURN_NULL_IF_NULL(ctx); return ctx->GetHandler(); }