read event never triggered, using libevent - epoll

I just write an echo server using libevent, but it seems that the read event is never triggered. The code is:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/tcp.h>
#include <event.h>
#include <event2/event.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
static short ListenPort = 19999;
static long ListenAddr = INADDR_ANY;//任意地址,值就是0
static int MaxConnections = 1024;
static int ServerSocket;
static struct event ServerEvent;
int SetNonblock(int fd)
{
int flags;
if ((flags = fcntl(fd, F_GETFL)) == -1) {
return -1;
}
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
return -1;
}
return 0;
}
void ServerRead(int fd, short ev, void *arg)
{
//1)when telnet on 1999 and send a string,this never prints,help!
printf("client readble\n");
fflush(stdout);
struct client *client = (struct client *)arg;
u_char buf[8196];
int len, wlen;
len = read(fd, buf, sizeof(buf));
if (len == 0) {
printf("disconnected\n");
close(fd);
event_del(&ServerEvent);
free(client);
return;
} else if (len < 0) {
printf("socket fail %s\n", strerror(errno));
close(fd);
event_del(&ServerEvent);
free(client);
return;
}
wlen = write(fd, buf, len);//1)client str never echo back
if (wlen < len) {
printf("not all data write back to client\n");
}
}
void ServerWrite(int fd, short ev, void *arg)
{
//2)to be simple,let writer do nothing
/* if(!arg)
{
printf("ServerWrite err!arg null\n");
return;
}
int len=strlen(arg);
if(len <= 0)
{
printf("ServerWrite err!len:%d\n",len);
return;
}
int wlen = write(fd, arg, len);
if (wlen<len) {
printf("not all data write back to client!wlen:%d len:%d \n",wlen,len);
}
*/
return;
}
void ServerAccept(int fd, short ev, void *arg)
{
int cfd;
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
int yes = 1;
cfd = accept(fd, (struct sockaddr *)&addr, &addrlen);
if (cfd == -1) {
//3)this prints ok
printf("accept(): can not accept client connection");
return;
}
if (SetNonblock(cfd) == -1) {
close(cfd);
return;
}
if (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) {
printf("setsockopt(): TCP_NODELAY %s\n", strerror(errno));
close(cfd);
return;
}
event_set(&ServerEvent, cfd, EV_READ | EV_PERSIST, ServerRead, NULL);
event_set(&ServerEvent, cfd, EV_WRITE| EV_PERSIST, ServerWrite,NULL);
event_add(&ServerEvent, NULL);
printf("Accepted connection from %s \n", (char *)inet_ntoa(addr.sin_addr));
}
int NewSocket(void)
{
struct sockaddr_in sa;
ServerSocket = socket(AF_INET, SOCK_STREAM, 0);
if (ServerSocket == -1) {
printf("socket(): can not create server socket\n");
return -1;
}
if (SetNonblock(ServerSocket) == -1) {
return -1;
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(ListenPort);
sa.sin_addr.s_addr = htonl(ListenAddr);
if (bind(ServerSocket, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
close(ServerSocket);
printf("bind(): can not bind server socket");
return -1;
}
if (listen(ServerSocket, MaxConnections) == -1) {
printf("listen(): can not listen server socket");
close(ServerSocket);
return -1;
}
event_set(&ServerEvent, ServerSocket, EV_READ | EV_PERSIST, ServerAccept, NULL);
if (event_add(&ServerEvent, 0) == -1) {
printf("event_add(): can not add accept event into libevent");
close(ServerSocket);
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
int retval;
event_init();
retval = NewSocket();
if (retval == -1) {
exit(-1);
}
event_dispatch();
return 0;
}
The server is tested using Telnet, but the client receives nothing.
The question details are posted as comments in the code above, at 1)、2)、3).
Can someone help me find out why the read event is never triggered?

basically you should not set the accepted socket as EV_WRITE until you actually want to write to the socket. You are telling libevent "let me know when I can write to this socket", which is pretty much always. So ServerWrite is being called in a tight loop. In practice the only time you need EV_WRITE is if you are writing a buffer but all of the bytes are not written. You then need to save the unwritten portion of the buffer and use EV_WRITE to signal when the socket can be written to again.

Related

ENet : Failed to connect to local server from iOS real device BUT do work when using desktop client

As i continue to check this great lib with iOS device which is iphone 5 iOS 8.3
I have strange ( or not ) connection error
When i try to connect the server which is running from my xCode . and Iphone app is running from xcode also .
The line :
if (enet_host_service(client, &event, 1000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT)
Dosn't pass and jumps to :
enet_peer_reset(peer);
BUT when i run this code both from my desktop as simple client/server app , every thing is working just fine .
here is the code which i take from some internet site .
notice i set the HOST to be my Mac : echo $HOSTNAME.
Maybe this is the problem and the iPhone can't connect my Mac .. i dont know
how can i check this ?
Thanks
Here is the code :
Client desktop :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "enet/enet.h"
#define BUFFERSIZE 1024
#define PORT 9991
ENetAddress address;
ENetHost *server;
ENetEvent event;
ENetPacket *packet;
char buffer[BUFFERSIZE];
int main(int argc, char ** argv) {
int i;
if (enet_initialize() != 0) {
printf("Could not initialize enet.");
return 0;
}
address.host = ENET_HOST_ANY;
address.port = PORT;
server = enet_host_create(&address, 100, 2, 0, 0);
if (server == NULL) {
printf("Could not start server.\n");
return 0;
}
else
{
printf("Server Started host: %d on port:%d.\n",address.host, address.port);
}
while (1) {
while (enet_host_service(server, &event, 1000) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_CONNECT:
break;
case ENET_EVENT_TYPE_RECEIVE:
if (event.peer->data == NULL) {
event.peer->data =
malloc(strlen((char*) event.packet->data)+1);
strcpy((char*) event.peer->data, (char*)
event.packet->data);
sprintf(buffer, "%s has connected\n",
(char*) event.packet->data);
packet = enet_packet_create(buffer,
strlen(buffer)+1, 0);
enet_host_broadcast(server, 1, packet);
enet_host_flush(server);
} else {
for (i=0; i<server->peerCount; i++) {
if (&server->peers[i] != event.peer) {
sprintf(buffer, "%s: %s",
(char*) event.peer->data, (char*)
event.packet->data);
packet = enet_packet_create(buffer,
strlen(buffer)+1, 0);
enet_peer_send(&server->peers[i], 0,
packet);
enet_host_flush(server);
} else {
}
}
}
break;
case ENET_EVENT_TYPE_DISCONNECT:
sprintf(buffer, "%s has disconnected.", (char*)
event.peer->data);
packet = enet_packet_create(buffer, strlen(buffer)+1, 0);
enet_host_broadcast(server, 1, packet);
free(event.peer->data);
event.peer->data = NULL;
break;
default:
printf("Tick tock.\n");
break;
}
}
}
enet_host_destroy(server);
enet_deinitialize();
return 0;
}
Client Iphone device ( part of cpp game engine ) compiles fine .
char buffer[BUFFERSIZE];
ENetHost *client;
ENetAddress address;
ENetEvent event;
ENetPeer *peer;
ENetPacket *packet;
int connected=0;
if (enet_initialize() != 0) {
log("Could not initialize enet.\n");
return false;
}
client = enet_host_create(NULL, 1, 2, 5760/8, 1440/8);
if (client == NULL) {
log("Could not create client.\n");
return false;
}
enet_address_set_host(&address, HOST);
address.port = PORT;
peer = enet_host_connect(client, &address, 2, 0);
if (peer == NULL) {
log("Could not connect to server\n");
return false;
}
if (enet_host_service(client, &event, 1000) > 0 &&
event.type == ENET_EVENT_TYPE_CONNECT) {
log("Connection to %s succeeded.\n", HOST);
connected++;
strncpy(buffer, "Meiry Test", BUFFERSIZE);
packet = enet_packet_create(buffer, strlen(buffer)+1,
ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(peer, 0, packet);
} else {
enet_peer_reset(peer);
log("Could not connect to %s.\n", HOST);
return false;
}
Server
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "enet/enet.h"
#define BUFFERSIZE 1024
#define PORT 9991
ENetAddress address;
ENetHost *server;
ENetEvent event;
ENetPacket *packet;
char buffer[BUFFERSIZE];
int main(int argc, char ** argv) {
int i;
if (enet_initialize() != 0) {
printf("Could not initialize enet.");
return 0;
}
address.host = ENET_HOST_ANY;
address.port = PORT;
server = enet_host_create(&address, 100, 2, 0, 0);
if (server == NULL) {
printf("Could not start server.\n");
return 0;
}
else
{
printf("Server Started host: %d on port:%d.\n",address.host, address.port);
}
while (1) {
while (enet_host_service(server, &event, 1000) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_CONNECT:
break;
case ENET_EVENT_TYPE_RECEIVE:
if (event.peer->data == NULL) {
event.peer->data =
malloc(strlen((char*) event.packet->data)+1);
strcpy((char*) event.peer->data, (char*)
event.packet->data);
sprintf(buffer, "%s has connected\n",
(char*) event.packet->data);
packet = enet_packet_create(buffer,
strlen(buffer)+1, 0);
enet_host_broadcast(server, 1, packet);
enet_host_flush(server);
} else {
for (i=0; i<server->peerCount; i++) {
if (&server->peers[i] != event.peer) {
sprintf(buffer, "%s: %s",
(char*) event.peer->data, (char*)
event.packet->data);
packet = enet_packet_create(buffer,
strlen(buffer)+1, 0);
enet_peer_send(&server->peers[i], 0,
packet);
enet_host_flush(server);
} else {
}
}
}
break;
case ENET_EVENT_TYPE_DISCONNECT:
sprintf(buffer, "%s has disconnected.", (char*)
event.peer->data);
packet = enet_packet_create(buffer, strlen(buffer)+1, 0);
enet_host_broadcast(server, 1, packet);
free(event.peer->data);
event.peer->data = NULL;
break;
default:
printf("Tick tock.\n");
break;
}
}
}
enet_host_destroy(server);
enet_deinitialize();
return 0;
}

Apps are reviewed on an IPv6 network. Please ensure that your app supports IPv6 networks, as IPv6 compatibility is required

I have received the following rejection from Apple for my app the last couple of days. My app communicates with UDP and the remote server is always IPv4. I have used BSD sockets. Please guide me how can I solve this problem.
I have tried to create a NAT64 hotspot using an IPv4 network but I was unable to send any packets to the server. Moreover, we don't have IPv6 available at my place now.
From Apple:
2.2 Details
We discovered one or more bugs in your app when reviewed on an iPad running iOS 9.3.2 and iPhone running iOS 9.3.2 on both Wi-Fi and cellular networks.
Specifically, during review we were unable to bypass the Initializing page. We encountered an error while waiting for the app to load. We've attached a screenshot for your reference.
Next Steps
Please run your app on a device to identify the issue(s), then revise and resubmit your app for review.
Apps are reviewed on an IPv6 network. Please ensure that your app supports IPv6 networks, as IPv6 compatibility is required.
For additional information about supporting IPv6 Networks, please refer to Supporting iPv6 DNS64/NAT64 Networks.
Source Code Bellow:
UdpSocketManager.h >>
#ifndef UDP_SOCKET_MANAGER_H__
#define UDP_SOCKET_MANAGER_H__
#import "TInetAddr.h"
class UdpSocketManager
{
public:
UdpSocketManager();
~UdpSocketManager();
void getLocalAddress();
void initializeSocket();
void start();
void stop();
void sendSignal(int p_type, TInetAddr *p_destAddress, unsigned char *p_data, int p_length);
void receiveSignal();
int localPort;
int signalingSocket;
int signalSocketRecvLength;
int socketFamily;
int isIPV4Available;
int isIPV6Available;
char wifiIP[INET_ADDRSTRLEN];
char cellularIP[INET_ADDRSTRLEN];
char wifiIP_v6[INET6_ADDRSTRLEN];
char cellularIP_v6[INET6_ADDRSTRLEN];
long returnLength;
struct sockaddr_in remoteAddrForRecv;
struct sockaddr_in srcAddrV4;
struct sockaddr_in6 srcAddrV6;
struct sockaddr_in sendAddr4;
struct sockaddr_in6 sendAddr6;
bool running;
pthread_t thread;
};
#endif
UdpSocketManager.m >>
#include <string.h>
#include <pthread.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "UdpSocketManager.h"
#import <Foundation/Foundation.h>
unsigned int ipAddressToUnsignedInt(char *ipAddress)
{
unsigned int ipAddressLongValue = 0L;
int byteSegment = 0;
for(int i = 0; i < strlen(ipAddress); i++)
{
char ch = ipAddress[i];
if(ch == '.')
{
ipAddressLongValue <<= 8;
ipAddressLongValue |= byteSegment;
byteSegment = 0;
}
else
{
byteSegment = byteSegment * 10 + (ch - 48);
}
}
ipAddressLongValue <<= 8;
ipAddressLongValue |= byteSegment;
return ipAddressLongValue;
}
int custom_random(int max=65535)
{
int randomValue;
randomValue = arc4random_uniform(65535)%max;
return randomValue;
}
int custom_random(int min, int max)
{
int randomValue;
randomValue = arc4random_uniform(max);
if(randomValue<min)
randomValue=(min+custom_random(max-min));
return randomValue;
}
void* runUdpSocketManager(void *objRef)
{
UdpSocketManager *THIS = (UdpSocketManager *) objRef;
THIS->running=true;
while (THIS->running)
{
THIS->receiveSignal();
}
pthread_exit(NULL);
return 0;
}
UdpSocketManager::UdpSocketManager()
{
socketFamily=AF_INET;
signalingSocket=-1;
running=false;
initializeSocket();
}
UdpSocketManager::~UdpSocketManager()
{
}
void UdpSocketManager::getLocalAddress()
{
//Read local address
getLocalAddress();
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success=0;
isIPV4Available=FALSE;
isIPV6Available=FALSE;
success = getifaddrs(&interfaces);
if (success == 0)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family==AF_INET)
{
if([[NSString stringWithUTF8String:temp_addr->ifa_name] hasPrefix:#"en"])
{
isIPV4Available=TRUE;
strcpy(wifiIP, inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
printf("IP Address: %s\n",inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
}
else if([[NSString stringWithUTF8String:temp_addr->ifa_name] hasPrefix:#"pdp_ip0"])
{
isIPV4Available=TRUE;
strcpy(cellularIP, inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
printf("IP Address: %s\n",inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
}
}
else if(temp_addr->ifa_addr->sa_family==AF_INET6)
{
if([[NSString stringWithUTF8String:temp_addr->ifa_name] hasPrefix:#"en"])
{
isIPV6Available=TRUE;
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)temp_addr->ifa_addr)->sin6_addr), (char*)wifiIP_v6, INET6_ADDRSTRLEN);
printf("Interface: %s IPV6: %s\n",temp_addr->ifa_name,wifiIP_v6);
}
else if([[NSString stringWithUTF8String:temp_addr->ifa_name] hasPrefix:#"pdp_ip0"])
{
isIPV6Available=TRUE;
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)temp_addr->ifa_addr)->sin6_addr), (char*)cellularIP_v6, INET6_ADDRSTRLEN);
printf("Interface: %s IPV6: %s\n",temp_addr->ifa_name,cellularIP_v6);
}
}
temp_addr = temp_addr->ifa_next;
}
}
freeifaddrs(interfaces);
}
void UdpSocketManager::initializeSocket()
{
if(signalingSocket!=-1)
close(signalingSocket);
if (isIPV4Available)
{
if((signalingSocket=socket(AF_INET, SOCK_DGRAM, 0))==-1)
{
NSLog(#"Unable to create signaling socket of AF_INET");
}
else
{
socketFamily=AF_INET;
NSLog(#"Socket created successfully. [AF_INET]");
}
}
else if(!isIPV4Available && isIPV6Available)
{
if((signalingSocket=socket(AF_INET6, SOCK_DGRAM, 0))==-1)
{
NSLog(#"Unable to create signaling socket of AF_INET6");
}
else
{
socketFamily=AF_INET6;
NSLog(#"Socket created successfully. [AF_INET6]");
}
}
else
{
if((signalingSocket=socket(AF_INET, SOCK_DGRAM, 0))==-1)
{
NSLog(#"Unable to create signaling socket of AF_INET");
}
else
{
socketFamily=AF_INET;
NSLog(#"Socket created successfully. [AF_INET]");
}
}
int count=0;
while(true)
{
count++;
if(socketFamily==AF_INET)
{
srcAddrV4.sin_len = sizeof(srcAddrV4);
srcAddrV4.sin_family = socketFamily;
srcAddrV4.sin_addr.s_addr = INADDR_ANY;
srcAddrV4.sin_port = htons(localPort);
if (bind(signalingSocket, (struct sockaddr *) &srcAddrV4, sizeof(srcAddrV4)) < 0)
{
NSLog(#"[AF_INET] ERROR occured creating signaling port at attempt (%d) Port: %d", count, localPort);
localPort=(int)custom_random(1024, 65535);
}
else
{
int on=1;
setsockopt(signalingSocket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
setsockopt(signalingSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
NSLog(#"[AF_INET] SignalingSocket Created Successfully at attempt (%d) Port: %d\n", count, localPort);
break;
}
}
else
{
srcAddrV6.sin6_len = sizeof(srcAddrV6);
srcAddrV6.sin6_family = socketFamily;
srcAddrV6.sin6_addr = in6addr_any;
srcAddrV6.sin6_port = htons(localPort);
if (bind(signalingSocket, (struct sockaddr *) &srcAddrV6, sizeof(srcAddrV6)) < 0)
{
NSLog(#"[AF_INET] ERROR occured creating signaling port at attempt (%d) Port: %d", count, localPort);
localPort=(int)custom_random(1024, 65535);
}
else
{
int on=1;
setsockopt(signalingSocket, SOL_SOCKET, SO_NOSIGPIPE, (void *)&on, sizeof(on));
setsockopt(signalingSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
NSLog(#"[AF_INET6] SignalingSocket Created Successfully at attempt (%d) Port: %d\n", count, localPort);
break;
}
}
}
}
void UdpSocketManager::start()
{
pthread_create(&thread, NULL, runUdpSocketManager, (void *) this);
}
void UdpSocketManager::stop()
{
running=false;
}
void UdpSocketManager::receiveSignal()
{
int port;
char ipAddress[16];
socklen_t fromlen;
unsigned char udpSignalRecvBuffer[1600];
fromlen = sizeof(remoteAddrForRecv);
signalSocketRecvLength = (int)recvfrom(signalingSocket, (char *)udpSignalRecvBuffer,1600,0,(struct sockaddr *)&remoteAddrForRecv,&fromlen);
if(signalSocketRecvLength>0)
{
strcpy(ipAddress, inet_ntoa(remoteAddrForRecv.sin_addr));
port = ntohs(remoteAddrForRecv.sin_port);
NSLog(#"RECEIVED %d bytes from %s:%d", signalSocketRecvLength, ipAddress, port);
}
else
{
usleep(10000);// 10 ms
}
}
void UdpSocketManager::sendSignal(int p_type, TInetAddr *p_destAddress, unsigned char *p_data, int p_length)
{
if(socketFamily==AF_INET6)
{
// Convert IPv4 address to IPv4-mapped-into-IPv6 address.
sendAddr6.sin6_family = AF_INET6;
sendAddr6.sin6_port = p_destAddress->m_port;
sendAddr6.sin6_addr.__u6_addr.__u6_addr32[0] = 0;
sendAddr6.sin6_addr.__u6_addr.__u6_addr32[1] = 0;
sendAddr6.sin6_addr.__u6_addr.__u6_addr32[2] = htonl(0xffff);
sendAddr6.sin6_addr.__u6_addr.__u6_addr32[3] = ntohl(ipAddressToUnsignedInt(p_destAddress->m_address));
sendAddr6.sin6_addr.__u6_addr.__u6_addr16[4] = 0;
sendAddr6.sin6_addr.__u6_addr.__u6_addr16[5] = 0xffff;
char ipV6Address[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &sendAddr6.sin6_addr, ipV6Address, INET6_ADDRSTRLEN);
NSLog(#"ipV6Address: %s\n", ipV6Address);
sendAddr6.sin6_flowinfo = 0;
sendAddr6.sin6_scope_id = 0;
}
else
{
sendAddr4.sin_family = AF_INET;
sendAddr4.sin_port = htons(p_destAddress->m_port);
if(inet_aton((char *) p_destAddress->m_address, &sendAddr4.sin_addr)==0)
{
NSLog(#"signal message - inet_aton() failed, %s", p_destAddress->m_address);
}
}
if(socketFamily==AF_INET)
returnLength=sendto(signalingSocket, p_data, p_length, 0, (struct sockaddr *)&sendAddr4, sizeof(sendAddr4));
else
returnLength=sendto(signalingSocket, p_data, p_length, 0, (struct sockaddr *)&sendAddr6, sizeof(sendAddr6));
NSLog(#"SENT %ld bytes to %s:%d\n", returnLength,p_destAddress->m_address,p_destAddress->m_port);
}
I don't think your "Convert IPv4 address to IPv4-mapped-into-IPv6 address" is correct. Rather than trying to construct an address yourself, you should call getaddrinfo() with a string of the thing you want to connect to (which could be either a hostname or an IPv4 address literal), and it will return to you a list of sockaddrs; you should use the first one from there to pass to sendto. It will give you the appropriate IP address family to use, and if you give an IPv4 address and it is an IPv6-only network (without you having to test anything), it will automatically give you the correct IPv6 address to use for that NAT64 router (without you needing to figure this out yourself). (If you wanted to manually construct an IPv6 address from IPv4 using NAT64/DNS64 without using getaddrinfo(), you would have to follow the complicated procedure in RFC 7050.)
Also, all the things you are doing in getLocalAddress() is unnecessary and potentially leads to more problems. You don't need isIPV4Available or isIPV6Available -- you shouldn't care at that point. Just create and bind both an IPv4 and an IPv6 socket in the beginning (not needing to care which one works), and each time you need to send, you get the right sockaddr to use using getaddrinfo() as above, and then send to the socket whose address family corresponds to the sockaddr you are using. And when you want to receive, you call recvfrom on both sockets.
I see several problems with the code you have shown:
UdpSocketManager does not call getLocalAddress() before calling initializeSocket(), so the binding socket is always IPv4, never IPv6. You are also not initializing localPort before calling initializeSocket(), either.
getLocalAddress() (if ever called) calls itself, so you have an endless recursion loop.
The use of custom_random() in a loop to pick a random binding port is just plain wrong, unnecessary, and potentially unending. To pick a random port when your configured port fails, just call bind() one time with the port set to 0. The OS knows which ports are available and will pick an available random port for you. Your entire initializeSocket() is overly complicated for what it attempts to do. It can be greatly simplified.
When enabling SO_REUSEADDR, it must be enabled before calling bind(), not after. Also look at SO_REUSEPORT.
Your remoteAddrForRecv variable is declared as sockaddr_in, which is only large enough to hold an IPv4 address and will cause recvfrom() to fail if called on an IPv6 socket. Use sockaddr_storage instead. In fact. All of your sockaddr_in and sockaddr_in6 variables should be replaced with sockaddr_storage.
inet_ntoa() is likewise also IPv4-only. Use inet_ntop() instead to handle both IPv4 and IPv6 addresses.
Try something more like this instead:
UdpSocketManager.h
#ifndef UDP_SOCKET_MANAGER_H__
#define UDP_SOCKET_MANAGER_H__
#import "TInetAddr.h"
class UdpSocketManager
{
public:
UdpSocketManager();
~UdpSocketManager();
void getLocalAddresses();
void initializeSocket();
void start();
void stop();
void sendSignal(int p_type, TInetAddr *p_destAddress, unsigned char *p_data, int p_length);
void receiveSignal();
int localPort;
int signalingSocket;
int socketFamily;
int isIPV4Available;
int isIPV6Available;
char wifiIP_v4[INET_ADDRSTRLEN];
char cellularIP_v4[INET_ADDRSTRLEN];
char wifiIP_v6[INET6_ADDRSTRLEN];
char cellularIP_v6[INET6_ADDRSTRLEN];
bool running;
pthread_t thread;
};
#endif
UdpSocketManager.m
#include <string.h>
#include <pthread.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "UdpSocketManager.h"
#import <Foundation/Foundation.h>
void* runUdpSocketManager(void *objRef)
{
UdpSocketManager *THIS = (UdpSocketManager *) objRef;
while (THIS->running)
{
THIS->receiveSignal();
}
pthread_exit(NULL);
return 0;
}
UdpSocketManager::UdpSocketManager()
{
socketFamily = AF_INET;
signalingSocket = -1;
localPort = 0; // or whatever port you actual want
srcAddrLen = 0;
sendAddrLen = 0;
running = false;
initializeSocket();
}
UdpSocketManager::~UdpSocketManager()
{
if(signalingSocket != -1)
{
close(signalingSocket);
signalingSocket = -1;
}
}
void UdpSocketManager::getLocalAddresses()
{
//Read local addresses
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
isIPV4Available = FALSE;
isIPV6Available = FALSE;
memset(&wifiIP_v4, 0, sizeof(wifiIP_v4));
memset(&cellularIP_v4, 0, sizeof(cellularIP_v4));
memset(&wifiIP_v6, 0, sizeof(wifiIP_v6));
memset(&cellularIP_v6, 0, sizeof(cellularIP_v6));
if(getifaddrs(&interfaces) == 0)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL)
{
if ([[NSString stringWithUTF8String:temp_addr->ifa_name] hasPrefix:#"en"])
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
isIPV4Available = TRUE;
inet_ntop(AF_INET, &(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr), wifiIP_v4, sizeof(wifiIP_v4));
printf("Interface: %s IPv4: %s\n", temp_addr->ifa_name, wifiIP_v4);
}
else if(temp_addr->ifa_addr->sa_family == AF_INET6)
{
isIPV6Available = TRUE;
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)temp_addr->ifa_addr)->sin6_addr), wifiIP_v6, sizeof(wifiIP_v6));
printf("Interface: %s IPv6: %s\n", temp_addr->ifa_name, wifiIP_v6);
}
}
else if([[NSString stringWithUTF8String:temp_addr->ifa_name] hasPrefix:#"pdp_ip0"])
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
isIPV4Available = TRUE;
inet_ntop(AF_INET, &(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr), cellularIP_v4, sizeof(cellularIP_v4));
printf("Interface: %s IPv6: %s\n", temp_addr->ifa_name, cellularIP_v4);
}
else if(temp_addr->ifa_addr->sa_family == AF_INET6)
{
isIPV6Available = TRUE;
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)temp_addr->ifa_addr)->sin6_addr), cellularIP_v6, sizeof(cellularIP_v6));
printf("Interface: %s IPv6: %s\n", temp_addr->ifa_name, cellularIP_v6);
}
}
temp_addr = temp_addr->ifa_next;
}
freeifaddrs(interfaces);
}
}
void UdpSocketManager::initializeSocket()
{
struct sockaddr_storage srcAddr;
struct sockaddr_in6 *srcAddr6;
struct sockaddr_in *srcAddr4;
socklen_t srcAddrLen;
if(signalingSocket != -1)
{
close(signalingSocket);
signalingSocket = -1;
}
getLocalAddresses();
if(isIPV6Available)
{
signalingSocket = socket(AF_INET6, SOCK_DGRAM, 0);
if(signalingSocket == -1)
{
NSLog(#"Unable to create IPv6 signaling socket");
return;
}
socketFamily = AF_INET6;
NSLog(#"IPv6 Socket created successfully");
}
else
{
signalingSocket = socket(AF_INET, SOCK_DGRAM, 0);
if(signalingSocket == -1)
{
NSLog(#"Unable to create IPv4 signaling socket");
return;
}
socketFamily = AF_INET;
NSLog(#"IPv4 Socket created successfully");
}
int on = 1;
setsockopt(signalingSocket, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on));
setsockopt(signalingSocket, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if(socketFamily == AF_INET6)
{
on = 0;
setsockopt(signalingSocket, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
srcAddr6 = (struct sockaddr_in6 *) &srcAddr;
srcAddrLen = sizeof(sockaddr_in6);
srcAddr6->sin6_len = srcAddrLen;
srcAddr6->sin6_family = socketFamily;
srcAddr6->sin6_addr = in6addr_any;
srcAddr6->sin6_port = htons(localPort);
}
else
{
srcAddr4 = (struct sockaddr_in *) &srcAddr;
srcAddrLen = sizeof(sockaddr_in);
srcAddr4->sin_len = srcAddrLen;
srcAddr4->sin_family = socketFamily;
srcAddr4->sin_addr.s_addr = INADDR_ANY;
srcAddr4->sin_port = htons(localPort);
}
if (bind(signalingSocket, (struct sockaddr *) &srcAddr, srcAddrLen) < 0)
{
if (localPort == 0)
{
NSLog(#"ERROR occured binding random signaling port");
close(signalingSocket);
signalingSocket = -1;
return;
}
NSLog(#"ERROR occured binding signaling port: %d", localPort);
if(socketFamily == AF_INET6)
srcAddr6->sin6_port = 0;
else
srcAddr4->sin_port = 0;
if (bind(signalingSocket, (struct sockaddr *) &srcAddr, srcAddrLen) < 0)
{
NSLog(#"ERROR occured binding random signaling port");
close(signalingSocket);
signalingSocket = -1;
return;
}
getsockname(signalingSocket, (struct sockaddr *) &srcAddr, &srcAddrLen);
if(socketFamily == AF_INET6)
localPort = ntohs(srcAddr6->sin6_port);
else
localPort = ntohs(srcAddr4->sin_port);
}
NSLog(#"SignalingSocket bound successfully on Port: %d\n", localPort);
}
void UdpSocketManager::start()
{
if(signalingSocket != -1)
{
running = true;
pthread_create(&thread, NULL, runUdpSocketManager, this);
}
}
void UdpSocketManager::stop()
{
running = false;
}
void UdpSocketManager::receiveSignal()
{
int port;
char ipAddress[INET6_ADDRSTRLEN];
unsigned char udpSignalRecvBuffer[1600];
ssize_t signalRecvLength;
struct sockaddr_storage remoteAddr;
socklen_t fromlen;
fromlen = sizeof(remoteAddr);
signalRecvLength = recvfrom(signalingSocket, udpSignalRecvBuffer, sizeof(udpSignalRecvBuffer), 0, (struct sockaddr *) &remoteAddr, &fromlen);
if(signalRecvLength >= 0)
{
if(remoteAddrForRecv.ss_family == AF_INET6)
{
struct sockaddr_in6 *remoteAddr6 = (struct sockaddr_in6 *) &remoteAddr;
inet_ntop(AF_INET6, &(remoteAddr6->sin6_addr), ipAddress, sizeof(ipAddress));
port = ntohs(remoteAddr6->sin6_port);
}
else
{
struct sockaddr_in *remoteAddr4 = (struct sockaddr_in4 *) &remoteAddr;
inet_ntop(AF_INET, &(remoteAddr4->sin_addr), ipAddress, sizeof(ipAddress));
port = ntohs(remoteAddr4->sin_port);
}
NSLog(#"RECEIVED %d bytes from %s:%d", signalRecvLength, ipAddress, port);
}
else
{
usleep(10000);// 10 ms
}
}
void UdpSocketManager::sendSignal(int p_type, TInetAddr *p_destAddress, unsigned char *p_data, int p_length)
{
struct sockaddr_storage sendAddr;
socklen_t sendAddrLen;
ssize_t returnLength;
char ipAddress[INET6_ADDRSTRLEN];
if(socketFamily == AF_INET6)
{
struct sockaddr_in6 *sendAddr6 = (struct sockaddr_in6 *) &sendAddr;
sendAddrLen = sizeof(sockaddr_in6);
sendAddr6->sin6_family = AF_INET6;
sendAddr6->sin6_port = htons(p_destAddress->m_port);
// Convert IPv4 address to IPv4-mapped IPv6 address.
sendAddr6->sin6_addr.__u6_addr.__u6_addr32[0] = 0;
sendAddr6->sin6_addr.__u6_addr.__u6_addr32[1] = 0;
sendAddr6->sin6_addr.__u6_addr.__u6_addr16[4] = 0;
sendAddr6->sin6_addr.__u6_addr.__u6_addr16[5] = 0xffff;
sendAddr6->sin6_addr.__u6_addr.__u6_addr32[3] = inet_addr(p_destAddress->m_address);
sendAddr6->sin6_flowinfo = 0;
sendAddr6->sin6_scope_id = 0;
inet_ntop(AF_INET6, &(sendAddr6->sin6_addr), ipAddress, sizeof(ipAddress));
}
else
{
struct sockaddr_in *sendAddr4 = (struct sockaddr_in *) &sendAddr;
sendAddrLen = sizeof(sockaddr_in);
sendAddr4->sin_family = AF_INET;
sendAddr4->sin_port = htons(p_destAddress->m_port);
if(inet_aton((char *) p_destAddress->m_address, &(sendAddr4->sin_addr)) == 0)
{
NSLog(#"signal message - inet_aton() failed, %s", p_destAddress->m_address);
return;
}
inet_ntop(AF_INET, &(sendAddr4->sin_addr), ipAddress, sizeof(ipAddress));
}
returnLength = sendto(signalingSocket, p_data, p_length, 0, (struct sockaddr *) &sendAddr, sendAddrLen);
if(returnLength >= 0)
NSLog(#"SENT %ld bytes to %s:%d\n", returnLength, ipAddress, p_destAddress->m_port);
}
Solution for AFNetworking for reachability in IPv6 and IPv4
change the following code in class AFNetworkReachabilityManager
+ (instancetype)sharedManager {
static AFNetworkReachabilityManager *_sharedManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
struct sockaddr_in6 address;
bzero(&address, sizeof(address));
address.sin6_len = sizeof(address);
address.sin6_family = AF_INET6;
_sharedManager = [self managerForAddress:&address];
});
return _sharedManager;
}

glib-2.0: g_spawn_command_line_sync() - unknown stdout length

function g_spawn_command_line_sync() has argument "gchar **standard_output":
https://developer.gnome.org/glib/stable/glib-Spawning-Processes.html#g-spawn-command-line-sync
I need read binary data from standard_output, but I not known length of standard_output.
Function g_spawn_command_line_sync():
http://fossies.org/dox/glib-2.38.2/gspawn-win32_8c_source.html#l01452
execute:
GString *outstr = NULL;
*standard_output = g_string_free (outstr, FALSE);
Struct GString include "gsize len", but g_spawn_command_line_sync() accessible only "gchar **".
I have next solution. I write size of stdout to stderr, which not using.
Example code:
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
int main()
{
gint exit_status = 0;
gchar *p_stdout = NULL;
gchar *p_stderr = NULL;
GError *p_error = NULL;
gboolean result;
result = g_spawn_command_line_sync("./make_image.py", &p_stdout, &p_stderr, &exit_status, &p_error);
if (!result) {
if (p_error != NULL) {
printf(p_error->message);
}
else {
printf("ERROR: Command not run\n");
}
}
else if (exit_status != 0) {
printf(p_stderr);
}
else {
int size = atoi(p_stderr);
gchar *p_c = p_stdout;
for (int i = 0; i < size; ++i) {
fputc(*p_c++, stdout);
}
//printf(p_stdout);
}
if (p_stdout) {
g_free(p_stdout);
}
if (p_stderr) {
g_free(p_stderr);
}
if (p_error) {
g_error_free(p_error);
}
return 0;
}
Use g_spawn_async_with_pipes. Reading binary data from file descriptors is easy. If you really need to detect when the child exits, add a callback using g_child_watch_add or g_child_watch_add_full, but you can probably get away with just reading the descriptor until it returns an error.

How to reply a D-Bus message

I got the D-Bus server.c and client.c code, and made some modification.
I want the result that when type for example "hi" from client.c
server will print "receive message hi", and reply "reply_content!!!!!!" to client.c
But it seems that now client.c cannot get the reply message.
Anyone have the idea?
Thanks in advance.
"server.c"
/* server.c */
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
static DBusHandlerResult
filter_func(DBusConnection *connection, DBusMessage *message, void *usr_data)
{
DBusMessage *reply;
dbus_bool_t handled = false;
char *word = NULL;
DBusError dberr;
dbus_error_init(&dberr);
dbus_message_get_args(message, &dberr, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID);
printf("receive message: %s\n", word);
handled = true;
reply = dbus_message_new_method_return(message);
char * reply_content = "reply_content!!!!!!";
dbus_message_append_args(reply, DBUS_TYPE_STRING, &reply_content, DBUS_TYPE_INVALID);
dbus_connection_send(connection, reply, NULL);
dbus_connection_flush(connection);
dbus_message_unref(reply);
return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
}
int main(int argc, char *argv[])
{
DBusError dberr;
DBusConnection *dbconn;
dbus_error_init(&dberr);
dbconn = dbus_bus_get(DBUS_BUS_SESSION, &dberr);
if (!dbus_connection_add_filter(dbconn, filter_func, NULL, NULL)) {
return -1;
}
dbus_bus_add_match(dbconn, "type='signal',interface='client.signal.Type'", &dberr);
while(dbus_connection_read_write_dispatch(dbconn, -1)) {
/* loop */
}
return 0;
}
Here is client.c
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
static DBusHandlerResult
filter_func(DBusConnection *connection, DBusMessage *message, void *usr_data)
{
dbus_bool_t handled = false;
char *word = NULL;
DBusError dberr;
dbus_error_init(&dberr);
dbus_message_get_args(message, &dberr, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID);
printf("receive message %s\n", word);
handled = true;
return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
}
int db_send(DBusConnection *dbconn)
{
DBusMessage *dbmsg;
char *word = (char *)malloc(sizeof(char));
int i;
dbmsg = dbus_message_new_signal("/client/signal/Object", "client.signal.Type", "Test");
scanf("%s", word);
if (!dbus_message_append_args(dbmsg, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID)) {
return -1;
}
if (!dbus_connection_send(dbconn, dbmsg, NULL)) {
return -1;
}
dbus_connection_flush(dbconn);
printf("send message %s\n", word);
dbus_message_unref(dbmsg);
return 0;
}
int main(int argc, char *argv[])
{
DBusError dberr;
DBusConnection *dbconn;
dbus_error_init(&dberr);
dbconn = dbus_bus_get(DBUS_BUS_SESSION, &dberr);
if (!dbus_connection_add_filter(dbconn, filter_func, NULL, NULL)) {
return -1;
}
db_send(dbconn);
while(dbus_connection_read_write_dispatch(dbconn, -1)) {
db_send(dbconn);
}
dbus_connection_unref(dbconn);
return 0;
}
You're communicating only with signals at the moment, which don't have a return value.
In client.c, use dbus_message_new_method_call instead of dbus_message_new_signal, and in server.c you probably have to change type='signal' to type='method_call'.

A question of libevent example code: how is invoked?

I'm learning libev however the code is so hard to understand, so I choose to learn libevent first whose code is relatively clearer. But I encounter a problem when try the example (http://www.wangafu.net/~nickm/libevent-book/01_intro.html).
How is the code event_add(state->write_event, NULL) in do_read() make do_write() function invoked?
/* For sockaddr_in */
#include <netinet/in.h>
/* For socket functions */
#include <sys/socket.h>
/* For fcntl */
#include <fcntl.h>
#include <event2/event.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define MAX_LINE 16384
void do_read(evutil_socket_t fd, short events, void *arg);
void do_write(evutil_socket_t fd, short events, void *arg);
char
rot13_char(char c)
{
return c;
/* We don't want to use isalpha here; setting the locale would change
* which characters are considered alphabetical. */
if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
return c + 13;
else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
return c - 13;
else
return c;
}
struct fd_state {
char buffer[MAX_LINE];
size_t buffer_used;
size_t n_written;
size_t write_upto;
struct event *read_event;
struct event *write_event;
};
struct fd_state *
alloc_fd_state(struct event_base *base, evutil_socket_t fd)
{
struct fd_state *state = malloc(sizeof(struct fd_state));
if (!state)
return NULL;
state->read_event = event_new(base, fd, EV_READ|EV_PERSIST, do_read, state);
if (!state->read_event) {
free(state);
return NULL;
}
state->write_event =
event_new(base, fd, EV_WRITE|EV_PERSIST, do_write, state);
if (!state->write_event) {
event_free(state->read_event);
free(state);
return NULL;
}
state->buffer_used = state->n_written = state->write_upto = 0;
assert(state->write_event);
return state;
}
void
free_fd_state(struct fd_state *state)
{
event_free(state->read_event);
event_free(state->write_event);
free(state);
}
void
do_read(evutil_socket_t fd, short events, void *arg)
{
struct fd_state *state = arg;
char buf[1024];
int i;
ssize_t result;
while (1) {
assert(state->write_event);
result = recv(fd, buf, sizeof(buf), 0);
if (result <= 0)
break;
for (i=0; i < result; ++i) {
if (state->buffer_used < sizeof(state->buffer))
state->buffer[state->buffer_used++] = rot13_char(buf[i]);
if (buf[i] == '\n') {
assert(state->write_event);
**event_add(state->write_event, NULL);**
state->write_upto = state->buffer_used;
}
}
}
if (result == 0) {
free_fd_state(state);
} else if (result < 0) {
if (errno == EAGAIN) // XXXX use evutil macro
return;
perror("recv");
free_fd_state(state);
}
}
void
**do_write(evutil_socket_t fd, short events, void *arg)**
{
struct fd_state *state = arg;
while (state->n_written < state->write_upto) {
ssize_t result = send(fd, state->buffer + state->n_written,
state->write_upto - state->n_written, 0);
if (result < 0) {
if (errno == EAGAIN) // XXX use evutil macro
return;
free_fd_state(state);
return;
}
assert(result != 0);
state->n_written += result;
}
if (state->n_written == state->buffer_used)
state->n_written = state->write_upto = state->buffer_used = 1;
event_del(state->write_event);
}
void
do_accept(evutil_socket_t listener, short event, void *arg)
{
struct event_base *base = arg;
struct sockaddr_storage ss;
socklen_t slen = sizeof(ss);
int fd = accept(listener, (struct sockaddr*)&ss, &slen);
if (fd < 0) { // XXXX eagain??
perror("accept");
} else if (fd > FD_SETSIZE) {
close(fd); // XXX replace all closes with EVUTIL_CLOSESOCKET */
} else {
struct fd_state *state;
evutil_make_socket_nonblocking(fd);
state = alloc_fd_state(base, fd);
assert(state); /*XXX err*/
assert(state->write_event);
event_add(state->read_event, NULL);
}
}
void
run(void)
{
evutil_socket_t listener;
struct sockaddr_in sin;
struct event_base *base;
struct event *listener_event;
base = event_base_new();
if (!base)
return; /*XXXerr*/
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
sin.sin_port = htons(40713);
listener = socket(AF_INET, SOCK_STREAM, 0);
evutil_make_socket_nonblocking(listener);
#ifndef WIN32
{
int one = 1;
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
}
#endif
if (bind(listener, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
perror("bind");
return;
}
if (listen(listener, 16)<0) {
perror("listen");
return;
}
listener_event = event_new(base, listener, EV_READ|EV_PERSIST, do_accept, (void*)base);
/*XXX check it */
event_add(listener_event, NULL);
event_base_dispatch(base);
}
int
main(int c, char **v)
{
setvbuf(stdout, NULL, _IONBF, 0);
run();
return 0;
}
I'm not sure if I'm answering the same question you asked - I understand it as:
How does calling event_add(state->write_event, NULL) in do_read() lead to do_write() being invoked?
The key to figuring this out is understanding what the do_read() function is actually doing. do_read() is a callback function associated with a socket which has data to be read: this is set up with allocate_fd_state():
struct fd_state *
alloc_fd_state(struct event_base *base, evutil_socket_t fd)
{
/*
* Allocate a new fd_state structure, which will hold our read and write events
* /
struct fd_state *state = malloc(sizeof(struct fd_state));
[...]
/*
* Initialize a read event on the given file descriptor: associate the event with
* the given base, and set up the do_read callback to be invoked whenever
* data is available to be read on the file descriptor.
* /
state->read_event = event_new(base, fd, EV_READ|EV_PERSIST, do_read, state);
[...]
/*
* Set up another event on the same file descriptor and base, which invoked the
* do_write callback anytime the file descriptor is ready to be written to.
*/
state->write_event =
event_new(base, fd, EV_WRITE|EV_PERSIST, do_write, state);
[...]
return state;
}
At this point, though, neither of these events have been event_add()'ed to the event_base base. The instructions for what to do are all written out, but no one is looking at them. So how does anything get read? state->read_event is event_add()'ed to the base after an incoming connection is made. Look at do_accept():
void
do_accept(evutil_socket_t listener, short event, void *arg)
{
[ ... accept a new connection and give it a file descriptor fd ... ]
/*
* If the file descriptor is invalid, close it.
*/
if (fd < 0) { // XXXX eagain??
perror("accept");
} else if (fd > FD_SETSIZE) {
close(fd); // XXX replace all closes with EVUTIL_CLOSESOCKET */
/*
* Otherwise, if the connection was successfully accepted...
*/
} else {
[ ... allocate a new fd_state structure, and make the file descriptor non-blocking ...]
/*
* Here's where the magic happens. The read_event created back in alloc_fd_state()
* is finally added to the base associated with it.
*/
event_add(state->read_event, NULL);
}
}
So right after accepting a new connection, the program tells libevent to wait until there's data available on the connection, and then run the do_read() callback. At this point, it's still impossible for do_write() to be called. It needs to be event_add()'ed. This happens in do_read():
void
do_read(evutil_socket_t fd, short events, void *arg)
{
/* Create a temporary buffer to receive some data */
char buf[1024];
while (1) {
[ ... Receive the data, copying it into buf ... ]
[ ... if there is no more data to receive, or there was an error, exit this loop... ]
[ ... else, result = number of bytes received ... ]
for (i=0; i < result; ++i) {
[ ... if there's room in the buffer, copy in the rot13() encoded
version of the received data ... ]
/*
* Boom, headshot. If we've reached the end of the incoming data
* (assumed to be a newline), then ...
*/
if (buf[i] == '\n') {
[...]
/*
* Have libevent start monitoring the write_event, which calls do_write
* as soon as the file descriptor is ready to be written to.
*/
event_add(state->write_event, NULL);
[...]
}
}
}
[...]
}
So, after reading in some data from a file descriptor, the program starts waiting until
the file descriptor is ready to be written to, and then invokes do_write(). Program
flow looks like this:
[ set up an event_base and start waiting for events ]
[ if someone tries to connect ]
[ accept the connection ]
[ ... wait until there is data to read on the connection ... ]
[ read in data from the connection until there is no more left ]
[ ....wait until the connection is ready to be written to ... ]
[ write out our rot13() encoded response ]
I hope that a) that was the correct interpretation of your question, and b) this was a helpful answer.

Resources