int main() { //load config int ret = ZBus::Instance().LoadConfig("channel_temp.xml"); if (ret != 0) { printf("ret %d\n", ret); print_error(); exit(1); } //init channels ret = ZBus::Instance().InitChannels(20001); if (ret != 0) { printf("init channel failed\n"); print_error(); exit(1); } Channel* pchannel = ZBus::Instance().GetChannel(20001, 10001); if (!pchannel) { printf("get channel failed\n"); print_error(); exit(1); } Reactor reactor; ReactorHandler handler; reactor.Initialize(1); ret = reactor.RegisterChannel(pchannel, &handler); if (ret < 0) { printf("register channel failed\n"); return -1; } int i = 10; while (i) { reactor.EventLoop(1000); char str[12] = "hello world"; pchannel->Send(str, sizeof(str)); i--; } return 0; }
void Execute(IRCServer *server, User *u, const std::vector<std::string> ¶ms) { const std::string &target = params[0]; Channel *c = server->FindChannel(target); if (c == NULL) u->WriteNumeric(403, target + " :No such channel"); else if (params.size() == 1) { u->WriteNumeric(332, c->GetName() + " :" + c->GetTopic()); u->WriteNumeric(333, c->GetName() + " " + u->GetNick() + " " + Sinkhole::stringify(c->topic_time)); } else { user_status *status = c->FindUserStatus(u); if (c->HasMode(CMODE_PROTECTEDTOPIC) && (status == NULL || !status->HasMode(CMODE_OP))) u->WriteNumeric(482, c->GetName() + " :You're not a channel operator"); else { std::string topic = params[1]; if (topic.length() > IRCServer::topiclen) topic = topic.substr(0, IRCServer::topiclen); c->SetTopic(topic); c->Send(u->GetMask(), "TOPIC " + c->GetName() + " :" + topic); } } }
void Execute(IRCServer *server, User *u, const std::vector<std::string> ¶ms) { const std::string &channel = params[0]; const std::string &targnick = params[1]; std::string reason = params.size() > 2 ? params[2] : ""; Channel *c = server->FindChannel(channel); User *target = server->FindUser(targnick); if (c == NULL) u->WriteNumeric(403, channel + " :No such channel"); else if (target == NULL) u->WriteNumeric(401, targnick + " :No such nick/channel"); else if (!target->IsOnChannel(c)) u->WriteNumeric(441, target->GetNick() + " " + c->GetName() + " :They aren't on that channel"); else { user_status *status = c->FindUserStatus(u); if (status == NULL || !status->HasMode(CMODE_OP)) u->WriteNumeric(482, c->GetName() + " :You're not a channel operator"); else { if (reason.length() > IRCServer::kicklen) reason = reason.substr(0, IRCServer::kicklen); c->Send(u->GetMask(), "KICK " + c->GetName() + " " + target->GetNick() + " :" + reason); u->Part(c); c->Part(u); } } }
// FinishInitialization status_t PortConnection::FinishInitialization() { // get the down stream channel Channel* channel = DownStreamChannelAt(0); if (!channel) return B_BAD_VALUE; // send the connect reply ConnectReply reply; reply.error = B_OK; reply.upStreamChannels = fUpStreamChannels; reply.downStreamChannels = fDownStreamChannels; status_t error = channel->Send(&reply, sizeof(ConnectReply)); if (error != B_OK) return error; // receive the channel infos int32 allChannels = fUpStreamChannels + fDownStreamChannels; PortChannel::Info* infos = new(std::nothrow) PortChannel::Info[allChannels - 1]; if (!infos) return B_NO_MEMORY; ArrayDeleter<PortChannel::Info> _(infos); error = channel->Receive(infos, sizeof(PortChannel::Info) * (allChannels - 1)); if (error != B_OK) return error; // create the channels for (int32 i = 1; i < allChannels; i++) { // create a channel PortChannel* otherChannel; error = _CreateChannel(&otherChannel, infos + i - 1, true); if (error != B_OK) return error; // add the channel if (i < fUpStreamChannels) // inverse, since we're on server side error = AddDownStreamChannel(otherChannel); else error = AddUpStreamChannel(otherChannel); if (error != B_OK) { delete otherChannel; return error; } } return B_OK; }
// Init (client side) status_t InsecureConnection::Init(const char* parameters) { PRINT(("InsecureConnection::Init\n")); if (!parameters) return B_BAD_VALUE; status_t error = AbstractConnection::Init(); if (error != B_OK) return error; // parse the parameters to get a server name and a port we shall connect to // parameter format is "<server>[:port] [ <up> [ <down> ] ]" char server[256]; uint16 port = kDefaultInsecureConnectionPort; int upStreamChannels = kDefaultUpStreamChannels; int downStreamChannels = kDefaultDownStreamChannels; if (strchr(parameters, ':')) { int result = sscanf(parameters, "%255[^:]:%hu %d %d", server, &port, &upStreamChannels, &downStreamChannels); if (result < 2) return B_BAD_VALUE; } else { int result = sscanf(parameters, "%255[^:] %d %d", server, &upStreamChannels, &downStreamChannels); if (result < 1) return B_BAD_VALUE; } // resolve server address NetAddress netAddress; error = NetAddressResolver().GetHostAddress(server, &netAddress); if (error != B_OK) return error; in_addr serverAddr = netAddress.GetAddress().sin_addr; // open the initial channel Channel* channel; error = _OpenClientChannel(serverAddr, port, &channel); if (error != B_OK) return error; error = AddUpStreamChannel(channel); if (error != B_OK) { delete channel; return error; } // send the server a connect request ConnectRequest request; request.protocolVersion = B_HOST_TO_BENDIAN_INT32(kProtocolVersion); request.serverAddress = serverAddr.s_addr; request.upStreamChannels = B_HOST_TO_BENDIAN_INT32(upStreamChannels); request.downStreamChannels = B_HOST_TO_BENDIAN_INT32(downStreamChannels); error = channel->Send(&request, sizeof(ConnectRequest)); if (error != B_OK) return error; // get the server reply ConnectReply reply; error = channel->Receive(&reply, sizeof(ConnectReply)); if (error != B_OK) return error; error = B_BENDIAN_TO_HOST_INT32(reply.error); if (error != B_OK) return error; upStreamChannels = B_BENDIAN_TO_HOST_INT32(reply.upStreamChannels); downStreamChannels = B_BENDIAN_TO_HOST_INT32(reply.downStreamChannels); port = B_BENDIAN_TO_HOST_INT16(reply.port); // open the remaining channels int32 allChannels = upStreamChannels + downStreamChannels; for (int32 i = 1; i < allChannels; i++) { PRINT((" creating channel %ld\n", i)); // open the channel error = _OpenClientChannel(serverAddr, port, &channel); if (error != B_OK) RETURN_ERROR(error); // add it if (i < upStreamChannels) error = AddUpStreamChannel(channel); else error = AddDownStreamChannel(channel); if (error != B_OK) { delete channel; return error; } } return B_OK; }
int main() { //load config int ret = ZBus::Instance().LoadConfig("channel_temp.xml"); if (ret != 0) { printf("ret %d\n", ret); print_error(); return -1; } //init channels ret = ZBus::Instance().InitChannels(10001); if (ret != 0) { printf("init channel failed\n"); print_error(); return -1; } Channel* pchannel = ZBus::Instance().GetChannel(20001, 10001); if (!pchannel) { printf("get channel failed\n"); print_error(); return -1; } void* socket = pchannel->GetRawSocket(); if (!socket) { printf("local peer nulll\n"); print_error(); return -1; } zmq_pollitem_t items [] = { { socket, 0, ZMQ_POLLIN, 0 } }; while (1) { Poll(items, 1, 1000); if (items [0].revents & ZMQ_POLLIN) { char data[12] = {0}; int len = 0; ret = pchannel->Recv(data, len); if (len > 0) { printf("recive client %s\n", data); pchannel->Send(data, len); } } } return 0; }