Skip to content
Snippets Groups Projects
Unverified Commit 33ab3dd2 authored by Bensong Liu's avatar Bensong Liu
Browse files

do some bugfix

parent 33bc11f5
No related branches found
No related tags found
No related merge requests found
...@@ -66,19 +66,21 @@ namespace Protocols { ...@@ -66,19 +66,21 @@ namespace Protocols {
auto onEvent = [&](auto activeFd) { auto onEvent = [&](auto activeFd) {
if (activeFd == ipcPipe) { if (activeFd == ipcPipe) {
// Outbound gave me a message to forward! Send it. // Outbound gave me a message to forward! Send it.
rlog.debug("Inbound event: from outbound msg. ");
auto targetClientId = rlib::sockIO::recv_msg(activeFd); auto targetClientId = rlib::sockIO::recv_msg(activeFd);
auto msg = rlib::sockIO::recv_msg(activeFd); auto msg = rlib::sockIO::recv_msg(activeFd);
auto clientAddr = ClientIdUtils::parseClientId(targetClientId); auto clientAddr = ClientIdUtils::parseClientId(targetClientId);
auto status = sendto(udpSenderSocket, msg.data(), msg.size(), 0, &clientAddr.addr, clientAddr.len); auto status = sendto(listenFd, msg.data(), msg.size(), 0, &clientAddr.addr, clientAddr.len);
dynamic_assert(status != -1, "sendto failed"); dynamic_assert(status != -1, "sendto failed");
} }
else if (activeFd == listenFd) { else if (activeFd == listenFd) {
rlog.debug("Inbound event: from client msg. ");
SockAddr clientAddr; SockAddr clientAddr;
auto msgLength = recvfrom(activeFd, msgBuffer.data(), msgBuffer.size(), 0, &clientAddr.addr, &clientAddr.len); auto msgLength = recvfrom(activeFd, msgBuffer.data(), msgBuffer.size(), 0, &clientAddr.addr, &clientAddr.len);
dynamic_assert(msgLength != -1, "recvfrom failed"); dynamic_assert(msgLength != -1, "recvfrom failed");
forwardMessageToOutbound(msgBuffer.substr(msgLength), ClientIdUtils::makeClientId(clientAddr)); forwardMessageToOutbound(msgBuffer.substr(0, msgLength), ClientIdUtils::makeClientId(clientAddr));
} }
}; };
...@@ -136,6 +138,7 @@ namespace Protocols { ...@@ -136,6 +138,7 @@ namespace Protocols {
std::string msgBuffer(DGRAM_BUFFER_SIZE, '\0'); std::string msgBuffer(DGRAM_BUFFER_SIZE, '\0');
auto onEvent = [&](auto activeFd) { auto onEvent = [&](auto activeFd) {
if (activeFd == ipcPipe) { if (activeFd == ipcPipe) {
rlog.debug("Outbound event: from inbound msg. ");
// Inbound gave me a message to forward! Send it. // Inbound gave me a message to forward! Send it.
auto targetClientId = rlib::sockIO::recv_msg(activeFd); auto targetClientId = rlib::sockIO::recv_msg(activeFd);
auto msg = rlib::sockIO::recv_msg(activeFd); auto msg = rlib::sockIO::recv_msg(activeFd);
...@@ -146,18 +149,21 @@ namespace Protocols { ...@@ -146,18 +149,21 @@ namespace Protocols {
rlib::sockIO::quick_send(iter->second, msg); // udp rlib::sockIO::quick_send(iter->second, msg); // udp
} }
else { else {
rlog.debug("create new conn to server. ");
// This clientId is new. I don't know how to listen many sockets for response, so I just issue `connect` just like TCP does. // This clientId is new. I don't know how to listen many sockets for response, so I just issue `connect` just like TCP does.
auto connFd = rlib::quick_connect(serverAddr, serverPort, true); auto connFd = rlib::quick_connect(serverAddr, serverPort, true);
epoll_add_fd(epollFd, connFd); epoll_add_fd(epollFd, connFd);
connectionMap.add(targetClientId, connFd); connectionMap.add(targetClientId, connFd);
rlib::sockIO::quick_send(connFd, msg); // udp rlib::sockIO::quick_send(connFd, msg); // udp
// send(connFd, msg.data(), msg.size(), MSG_DONTWAIT);
} }
} }
else { else {
rlog.debug("Outbound msg: from server msg. ");
// Message from some connFd. Read and forward it. // Message from some connFd. Read and forward it.
auto status = recv(activeFd, msgBuffer.data(), msgBuffer.size(), 0); auto msgLength = recv(activeFd, msgBuffer.data(), msgBuffer.size(), 0);
dynamic_assert(status != -1, "recv failed"); dynamic_assert(msgLength != -1, "recv failed");
if (status == 0) { if (msgLength == 0) {
// TODO: close the socket, and notify Inbound to destory data structures. // TODO: close the socket, and notify Inbound to destory data structures.
epoll_del_fd(epollFd, activeFd); epoll_del_fd(epollFd, activeFd);
connectionMap.del(activeFd); connectionMap.del(activeFd);
...@@ -166,7 +172,7 @@ namespace Protocols { ...@@ -166,7 +172,7 @@ namespace Protocols {
dynamic_assert(connectionMap.server2client.count(activeFd) > 0, "connectionMap MUST contain server connfd. "); dynamic_assert(connectionMap.server2client.count(activeFd) > 0, "connectionMap MUST contain server connfd. ");
forwardMessageToInbound(msgBuffer.substr(0, status), connectionMap.server2client.at(activeFd)); forwardMessageToInbound(msgBuffer.substr(0, msgLength), connectionMap.server2client.at(activeFd));
} }
}; };
......
...@@ -23,7 +23,7 @@ struct SockAddr { ...@@ -23,7 +23,7 @@ struct SockAddr {
sockaddr_in in4; sockaddr_in in4;
sockaddr_in6 in6; sockaddr_in6 in6;
}; };
socklen_t len; socklen_t len = sizeof(sockaddr_storage);
}; };
struct ClientIdUtils { struct ClientIdUtils {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment