From 109ffe9a777658936a38d0c146579a67c60a0d17 Mon Sep 17 00:00:00 2001 From: xuxiuxi <xuxiuxi@454eff88-639b-444f-9e54-f578c98de674> Date: 星期四, 11 五月 2017 17:48:48 +0800 Subject: [PATCH] --- RtspFace/ev_server.cpp | 59 ++++++++++++++++++++++++++++++++++++++--------------------- 1 files changed, 38 insertions(+), 21 deletions(-) diff --git a/RtspFace/ev_server.cpp b/RtspFace/ev_server.cpp index 4baa6b2..2f423a2 100644 --- a/RtspFace/ev_server.cpp +++ b/RtspFace/ev_server.cpp @@ -42,6 +42,10 @@ { } }; +#ifndef USER_DEFINE_EVCLIENT_PROC +evclient_proc_t evclient_proc = nullptr; +#endif + // Set a socket to non-blocking mode. static int setnonblock(int fd) { @@ -65,11 +69,11 @@ if (what & EVBUFFER_EOF) { //Client disconnected, remove the read event and the free the client structure. - LOG_INFO << "Client disconnected."; + LOG_INFO << "Client disconnected." << std::endl; } else { - LOG_WARN << "Client socket error, disconnecting."; + LOG_WARN << "Client socket error, disconnecting." << std::endl; } bufferevent_free(client->buf_ev); close(client->fd); @@ -85,12 +89,12 @@ // Write back the read buffer. It is important to note that // bufferevent_write_buffer will drain the incoming data so it // is effectively gone after we call it. - //LOG_DEBUG << (char*)bufev->input; + //LOG_DEBUG << (char*)bufev->input << std::endl; //bufferevent_write_buffer(bufev, bufev->input); //char buff[100] = {'\0'}; //size_t readSize = bufferevent_read(bufev, buff, sizeof(buff)); - //LOG_DEBUG << "readSize=" << readSize << "\t" << buff; + //LOG_DEBUG << "readSize=" << readSize << "\t" << buff << std::endl; EVPHeader* evpHeader = (EVPHeader*)client->recvbuff; @@ -103,7 +107,7 @@ client->read_times = 1; if (readSize != sizeof(headerBuff)) { - LOG_WARN << "client send incomplete header"; + LOG_WARN << "client send incomplete header" << std::endl; buffered_on_error(bufev, 0, arg); return; } @@ -114,7 +118,7 @@ if (evpHeader->cmd <= EVPCommand::EVPC__FIRST || evpHeader->cmd >= EVPCommand::EVPC__LAST || evpHeader->size < sizeof(EVPHeader) || evpHeader->size > CLIENT_BUFFER_MAX) { - LOG_WARN << "client send invalid header"; + LOG_WARN << "client send invalid header" << std::endl; buffered_on_error(bufev, 0, arg); return; } @@ -157,16 +161,15 @@ bool closeClient = true; if (client->proc != nullptr) { - EVClientStub cs; + EVClientStub cs(client->recvbuff, client->recvbuff_end); cs.id = client->fd; - cs.recvBuff = client->recvbuff; - cs.recvBuffSize = client->recvbuff_end; closeClient = !(client->proc(cs)); if (cs.sendBuff != nullptr && cs.sendBuffSize > 0) { - - //#todo bufferevent_write + size_t writeSize = bufferevent_write(bufev, cs.sendBuff, cs.sendBuffSize); + if (writeSize != cs.sendBuffSize) + LOG_WARN << "server send truncate " << (cs.sendBuffSize - writeSize) << " bytes" << std::endl; if (cs.deleteSendBuff) delete[] cs.sendBuff; @@ -175,7 +178,7 @@ if (closeClient) { - LOG_DEBUG << "server initiative close"; + LOG_DEBUG << "server initiative close" << std::endl; buffered_on_error(bufev, 0, arg); } @@ -187,7 +190,7 @@ // check read times if (client->read_times > CLIENT_READ_TIMES_MAX) { - LOG_WARN << "client read times to max"; + LOG_WARN << "client read times to max" << std::endl; buffered_on_error(bufev, 0, arg); } } @@ -206,19 +209,19 @@ int client_fd = accept(fd, (struct sockaddr *)&client_addr, &client_len); if (client_fd < 0) { - LOG_WARN << "accept failed"; + LOG_WARN << "accept failed" << std::endl; return; } // Set the client socket to non-blocking mode. if (setnonblock(client_fd) < 0) - LOG_WARN << "failed to set client socket non-blocking"; + LOG_WARN << "failed to set client socket non-blocking" << std::endl; // We've accepted a new client, create a client object. struct EVClient* client = new EVClient; if (client == NULL) { - LOG_ERROR << "malloc failed"; + LOG_ERROR << "malloc failed" << std::endl; } client->fd = client_fd; client->proc = evclient_proc; @@ -229,7 +232,7 @@ // We have to enable it before our callbacks will be called. bufferevent_enable(client->buf_ev, EV_READ); - LOG_INFO << "Accepted connection from " << inet_ntoa(client_addr.sin_addr); + LOG_INFO << "Accepted connection from " << inet_ntoa(client_addr.sin_addr) << std::endl; } int server_main(int argc, char **argv) @@ -243,7 +246,7 @@ int listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (listen_fd < 0) { - LOG_ERROR << "create socket failed"; + LOG_ERROR << "create socket failed" << std::endl; return EXIT_FAILURE; } @@ -254,13 +257,13 @@ listen_addr.sin_port = htons(SERVER_PORT); if (bind(listen_fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) { - LOG_ERROR << "bind failed"; + LOG_ERROR << "bind failed" << std::endl; return EXIT_FAILURE; } if (listen(listen_fd, 5) < 0) { - LOG_ERROR << "listen failed"; + LOG_ERROR << "listen failed" << std::endl; return EXIT_FAILURE; } @@ -269,7 +272,7 @@ setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr_on, sizeof(reuseaddr_on)); if (setnonblock(listen_fd) < 0) { - LOG_ERROR << "failed to set server socket to non-blocking"; + LOG_ERROR << "failed to set server socket to non-blocking" << std::endl; return EXIT_FAILURE; } @@ -283,3 +286,17 @@ return EXIT_SUCCESS; } + +void ev_send_status_packet(EVClientStub& client, EVPStatus::EVPS status) +{ + client.sendBuffSize = sizeof(EVPHeader)+sizeof(EVP_Status); + client.sendBuff = new uint8_t[client.sendBuffSize]; + client.deleteSendBuff = true; + + EVPHeader* evpHeader = new (client.sendBuff) EVPHeader; + evpHeader->cmd = EVPCommand::EVPC_STATUS; + evpHeader->size = client.sendBuffSize; + + EVP_Status* evpStatus = new (client.sendBuff + sizeof(EVPHeader)) EVP_Status; + evpStatus->status = status; +} -- Gitblit v1.8.0