- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:这是在 OSX 上运行程序时的 tcpdump 输出:
Matthew-Mitchell:calm-ocean-4924 matt$ sudo tcpdump -X -i lo0 'port 45564'
Password:
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo0, link-type NULL (BSD loopback), capture size 65535 bytes
22:52:41.620969 IP localhost.53685 > localhost.45564: Flags [S], seq 3787197032, win 65535, options [mss 16344,nop,wscale 4,nop,nop,TS val 465807577 ecr 0,sackOK,eol], length 0
0x0000: 4500 0040 ba9e 4000 4006 0000 7f00 0001 E..@..@.@.......
0x0010: 7f00 0001 d1b5 b1fc e1bc 0a68 0000 0000 ...........h....
0x0020: b002 ffff fe34 0000 0204 3fd8 0103 0304 .....4....?.....
0x0030: 0101 080a 1bc3 a8d9 0000 0000 0402 0000 ................
22:52:41.621062 IP localhost.45564 > localhost.53685: Flags [S.], seq 1362049502, ack 3787197033, win 65535, options [mss 16344,nop,wscale 4,nop,nop,TS val 465807577 ecr 465807577,sackOK,eol], length 0
0x0000: 4500 0040 8dac 4000 4006 0000 7f00 0001 E..@..@.@.......
0x0010: 7f00 0001 b1fc d1b5 512f 39de e1bc 0a69 ........Q/9....i
0x0020: b012 ffff fe34 0000 0204 3fd8 0103 0304 .....4....?.....
0x0030: 0101 080a 1bc3 a8d9 1bc3 a8d9 0402 0000 ................
22:52:41.621075 IP localhost.53685 > localhost.45564: Flags [.], ack 1, win 9186, options [nop,nop,TS val 465807577 ecr 465807577], length 0
0x0000: 4500 0034 fdb1 4000 4006 0000 7f00 0001 E..4..@.@.......
0x0010: 7f00 0001 d1b5 b1fc e1bc 0a69 512f 39df ...........iQ/9.
0x0020: 8010 23e2 fe28 0000 0101 080a 1bc3 a8d9 ..#..(..........
0x0030: 1bc3 a8d9 ....
22:52:41.621086 IP localhost.45564 > localhost.53685: Flags [.], ack 1, win 9186, options [nop,nop,TS val 465807577 ecr 465807577], length 0
0x0000: 4500 0034 53bf 4000 4006 0000 7f00 0001 E..4S.@.@.......
0x0010: 7f00 0001 b1fc d1b5 512f 39df e1bc 0a69 ........Q/9....i
0x0020: 8010 23e2 fe28 0000 0101 080a 1bc3 a8d9 ..#..(..........
0x0030: 1bc3 a8d9
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <event2/event.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#ifdef SO_NOSIGPIPE
#define CB_NOSIGPIPE true
#else
#define CB_NOSIGPIPE false
#define SO_NOSIGPIPE 0
#endif
void loopErr(void * vself);
void loopErr(void * vself){
printf("Loop error\n");
exit(EXIT_FAILURE);
}
void timeout(void * vself, void * foo);
void timeout(void * vself, void * foo){
printf("Timeout\n");
exit(EXIT_FAILURE);
}
void didConnect(void * vself, void * foo);
void didConnect(void * vself, void * foo){
printf("Did connect\n");
}
bool CBSocketAccept(int sock, int * connectionSock);
bool CBSocketAccept(int sock, int * connectionSock){
*connectionSock = accept(sock, NULL, NULL);
if (*connectionSock == -1)
return false;
// Make socket non-blocking
evutil_make_socket_nonblocking(*connectionSock);
// Stop SIGPIPE
int i = 1;
if (CB_NOSIGPIPE)
setsockopt(*connectionSock, SOL_SOCKET, SO_NOSIGPIPE, &i, sizeof(i));
return true;
}
void acceptConn(void * vself, int socket);
void acceptConn(void * vself, int socket){
int connSock;
if (! CBSocketAccept(socket, &connSock)){
printf("Unable to accept a connection.\n");
exit(EXIT_FAILURE);
}
printf("Did accept\n");
}
typedef struct{
struct event_base * base;
void (*onError)(void *);
void (*onTimeOut)(void *, void *); /**< Callback for timeouts */
void * communicator;
pthread_t loopThread; /**< The thread for the event loop. */
void (*userCallback)(void *);
void * userArg;
}CBEventLoop;
union CBOnEvent{
void (*i)(void *, int);
void (*ptr)(void *, void *);
};
typedef struct{
CBEventLoop * loop; /**< For getting timeout CBLogError */
struct event * event; /**< libevent event. */
union CBOnEvent onEvent;
void * peer;
}CBEvent;
void * CBStartEventLoop(void * vloop);
void * CBStartEventLoop(void * vloop){
CBEventLoop * loop = vloop;
// Start event loop
printf("Starting network event loop.\n");
if(event_base_dispatch(loop->base) == -1){
// Error
loop->onError(loop->communicator);
return NULL;
}
// Break from loop. Free everything.
event_base_free(loop->base);
free(loop);
return NULL;
}
void event_base_add_virtual(struct event_base *);
bool CBNewEventLoop(CBEventLoop ** loop, void (*onError)(void *), void (*onDidTimeout)(void *, void *), void * communicator);
bool CBNewEventLoop(CBEventLoop ** loop, void (*onError)(void *), void (*onDidTimeout)(void *, void *), void * communicator){
struct event_base * base = event_base_new();
// Create dummy event to maintain loop.
event_base_add_virtual(base);
// Create arguments for the loop
*loop = malloc(sizeof(**loop));
(*loop)->base = base;
(*loop)->onError = onError;
(*loop)->onTimeOut = onDidTimeout;
(*loop)->communicator = communicator;
// Create thread
pthread_create(&(*loop)->loopThread, NULL, CBStartEventLoop, *loop);
return loop;
}
bool CBSocketBind(int * sock, bool IPv6, uint16_t port);
bool CBSocketBind(int * sock, bool IPv6, uint16_t port){
struct addrinfo hints, *res, *ptr;
// Set hints for the computer's addresses.
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = IPv6 ? AF_INET6 : AF_INET;
hints.ai_socktype = SOCK_STREAM;
// Get host for listening
char portStr[6];
sprintf(portStr, "%u", port);
if (getaddrinfo(NULL, portStr, &hints, &res))
return false;
// Attempt to bind to one of the addresses.
for(ptr = res; ptr != NULL; ptr = ptr->ai_next) {
if ((*sock = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol)) == -1)
continue;
// Prevent EADDRINUSE
int opt = 1;
setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if (bind(*sock, ptr->ai_addr, ptr->ai_addrlen) == -1) {
printf("Bind gave the error %s for address on port %u.\n", strerror(errno), port);
evutil_closesocket(*sock);
continue;
}
break; // Success.
}
freeaddrinfo(res);
if (ptr == NULL) // Failure
return false;
// Prevent SIGPIPE
int i = 1;
setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, &i, sizeof(i));
// Make socket non-blocking
evutil_make_socket_nonblocking(*sock);
return true;
}
void CBCanAccept(evutil_socket_t sock, short eventNum, void * arg);
void CBCanAccept(evutil_socket_t sock, short eventNum, void * arg){
CBEvent * event = arg;
event->onEvent.i(event->loop->communicator, sock);
}
bool CBSocketCanAcceptEvent(CBEvent ** event, CBEventLoop * loop, int sock, void (*onCanAccept)(void *, int));
bool CBSocketCanAcceptEvent(CBEvent ** event, CBEventLoop * loop, int sock, void (*onCanAccept)(void *, int)){
*event = malloc(sizeof(**event));
(*event)->loop = loop;
(*event)->onEvent.i = onCanAccept;
(*event)->event = event_new((*event)->loop->base, sock, EV_READ|EV_PERSIST, CBCanAccept, *event);
return true;
}
bool CBSocketAddEvent(CBEvent * event, uint32_t timeout);
bool CBSocketAddEvent(CBEvent * event, uint32_t timeout){
int res;
if (timeout) {
uint32_t secs = timeout / 1000;
struct timeval time = {secs, (timeout - secs*1000) * 1000};
res = event_add(event->event, &time);
}else
res = event_add(event->event, NULL);
return ! res;
}
bool CBNewSocket(int * sock, bool IPv6);
bool CBNewSocket(int * sock, bool IPv6){
// You need to use PF_INET for IPv4 mapped IPv6 addresses despite using the IPv6 format.
*sock = socket(IPv6 ? PF_INET6 : PF_INET, SOCK_STREAM, 0);
if (*sock == -1)
return false;
// Stop SIGPIPE annoying us.
int i = 1;
if (CB_NOSIGPIPE)
setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, &i, sizeof(i));
// Make address reusable
setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
// Make socket non-blocking
evutil_make_socket_nonblocking(*sock);
return true;
}
bool CBSocketConnect(int sock, uint8_t * IP, bool IPv6, uint16_t port);
bool CBSocketConnect(int sock, uint8_t * IP, bool IPv6, uint16_t port){
// Create sockaddr_in6 information for a IPv6 address
int res;
if (IPv6) {
struct sockaddr_in6 address;
memset(&address, 0, sizeof(address)); // Clear structure.
address.sin6_family = AF_INET6;
memcpy(&address.sin6_addr, IP, 16); // Move IP address into place.
address.sin6_port = htons(port); // Port number to network order
res = connect(sock, (struct sockaddr *)&address, sizeof(address));
}else{
struct sockaddr_in address;
memset(&address, 0, sizeof(address)); // Clear structure.
address.sin_family = AF_INET;
memcpy(&address.sin_addr, IP + 12, 4); // Move IP address into place. Last 4 bytes for IPv4.
address.sin_port = htons(port); // Port number to network order
res = connect(sock, (struct sockaddr *)&address, sizeof(address));
}
if (res < 0 && errno == EINPROGRESS)
return true;
return false;
}
void CBDidConnect(evutil_socket_t socketID, short eventNum, void * arg);
void CBDidConnect(evutil_socket_t socketID, short eventNum, void * arg){
CBEvent * event = arg;
if (eventNum & EV_TIMEOUT) {
// Timeout for the connection
event->loop->onTimeOut(event->loop->communicator, event->peer);
}else{
int optval = -1;
socklen_t optlen = sizeof(optval);
getsockopt(socketID, SOL_SOCKET, SO_ERROR, &optval, &optlen);
if (optval){
// Act as timeout
printf("Connection error: %s\n", strerror(optval));
event->loop->onTimeOut(event->loop->communicator, event->peer);
}else
// Connection successful
event->onEvent.ptr(event->loop->communicator, event->peer);
}
}
bool CBSocketDidConnectEvent(CBEvent ** event, CBEventLoop * loop, int sock, void (*onDidConnect)(void *, void *), void * peer);
bool CBSocketDidConnectEvent(CBEvent ** event, CBEventLoop * loop, int sock, void (*onDidConnect)(void *, void *), void * peer){
*event = malloc(sizeof(**event));
(*event)->loop = loop;
(*event)->onEvent.ptr = onDidConnect;
(*event)->peer = peer;
(*event)->event = event_new((*event)->loop->base, sock, EV_TIMEOUT|EV_WRITE, CBDidConnect, *event);
return true;
}
int main(int argc, const char * argv[]){
int listeningSocket, connSocket;
CBEvent *acceptEvent, *connectEvent;
CBEventLoop *listeningEventLoop, *connEventLoop;
if (!CBNewEventLoop(&listeningEventLoop, loopErr, timeout, NULL)
|| !CBNewEventLoop(&connEventLoop, loopErr, timeout, NULL)){
printf("Unable to start event loops.");
return EXIT_FAILURE;
}
if (!CBSocketBind(&listeningSocket, false, 45564)){
printf("Unable to bind a socket for listening");
return EXIT_FAILURE;
}
if (!CBSocketCanAcceptEvent(&acceptEvent, listeningEventLoop, listeningSocket, acceptConn)) {
printf("Unable to create an accept event");
return EXIT_FAILURE;
}
if(!CBSocketAddEvent(acceptEvent, 0)){
printf("Unable to add an accept event\n");
return EXIT_FAILURE;
}
if (listen(listeningSocket, 1) == -1){
printf("Unable to start listening\n");
return EXIT_FAILURE;
}
// Make socket
if (!CBNewSocket(&connSocket, false)){
printf("Socket create fail\n");
return EXIT_FAILURE;
}
// Add event for connection
if (!CBSocketDidConnectEvent(&connectEvent, connEventLoop, connSocket, didConnect, NULL)) {
printf("Couldn't create did connect event\n");
return EXIT_FAILURE;
}
if (!CBSocketAddEvent(connectEvent, 1000)) {
printf("Couldn't add connect event\n");
return EXIT_FAILURE;
}
// Connect
if (!CBSocketConnect(connSocket, (uint8_t []){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 127, 0, 0, 1}, false, 45564)){
printf("Couldn't connect\n");
return EXIT_FAILURE;
}
pthread_exit(NULL);
return 0;
}
Starting network event loop.
Starting network event loop.
最佳答案
这段代码有几个问题。
首先,CBNewEventLoop 总是返回 true,因为它返回 (bool)loop
(这是 main 中堆栈变量之一的地址)。此外,您似乎会通过返回 (bool)(*loop)
检测到唯一的错误。将是 malloc 失败,但如果 malloc 失败,您需要在写入 *loop
之前检查其结果.
其次,更根本的是,您在一个线程中运行一个事件循环并从主线程调用它的方法。事件驱动系统通常不是线程安全的。您应该将此代码构建为一系列回调,以在事件循环的线程上运行。
最后,一次需要多个事件循环是非常罕见的。您应该为该过程设置一个事件循环,并将其用于监听和连接。
关于c - 在 OSX Mountain Lion 上,Libevent 代码未连接或超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19364342/
我尝试理解[c代码 -> 汇编]代码 void node::Check( data & _data1, vector& _data2) { -> push ebp -> mov ebp,esp ->
我需要在当前表单(代码)的上下文中运行文本文件中的代码。其中一项要求是让代码创建新控件并将其添加到当前窗体。 例如,在Form1.cs中: using System.Windows.Forms; ..
我有此 C++ 代码并将其转换为 C# (.net Framework 4) 代码。有没有人给我一些关于 malloc、free 和 sprintf 方法的提示? int monate = ee; d
我的网络服务器代码有问题 #include #include #include #include #include #include #include int
给定以下 html 代码,将列表中的第三个元素(即“美丽”一词)以斜体显示的 CSS 代码是什么?当然,我可以给这个元素一个 id 或一个 class,但 html 代码必须保持不变。谢谢
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我试图制作一个宏来避免重复代码和注释。 我试过这个: #define GrowOnPage(any Page, any Component) Component.Width := Page.Surfa
我正在尝试将我的旧 C++ 代码“翻译”成头条新闻所暗示的 C# 代码。问题是我是 C# 中的新手,并不是所有的东西都像 C++ 中那样。在 C++ 中这些解决方案运行良好,但在 C# 中只是不能。我
在 Windows 10 上工作,R 语言的格式化程序似乎没有在 Visual Studio Code 中完成它的工作。我试过R support for Visual Studio Code和 R-T
我正在处理一些报告(计数),我必须获取不同参数的计数。非常简单但乏味。 一个参数的示例查询: qCountsEmployee = ( "select count(*) from %s wher
最近几天我尝试从 d00m 调试网络错误。我开始用尽想法/线索,我希望其他 SO 用户拥有可能有用的宝贵经验。我希望能够提供所有相关信息,但我个人无法控制服务器环境。 整个事情始于用户注意到我们应用程
我有一个 app.js 文件,其中包含如下 dojo amd 模式代码: require(["dojo/dom", ..], function(dom){ dom.byId('someId').i
我对“-gencode”语句中的“code=sm_X”选项有点困惑。 一个例子:NVCC 编译器选项有什么作用 -gencode arch=compute_13,code=sm_13 嵌入库中? 只有
我为我的表格使用 X-editable 框架。 但是我有一些问题。 $(document).ready(function() { $('.access').editable({
我一直在通过本教程学习 flask/python http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-wo
我想将 Vim 和 EMACS 用于 CNC、G 代码和 M 代码。 Vim 或 EMACS 是否有任何语法或模式来处理这种类型的代码? 最佳答案 一些快速搜索使我找到了 this vim 和 thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve this
这个问题在这里已经有了答案: Enabling markdown highlighting in Vim (5 个回答) 6年前关闭。 当我在 Vim 中编辑包含 Markdown 代码的 READM
我正在 Swift3 iOS 中开发视频应用程序。基本上我必须将视频 Assets 和音频与淡入淡出效果合并为一个并将其保存到 iPhone 画廊。为此,我使用以下方法: private func d
pipeline { agent any stages { stage('Build') { steps { e
我是一名优秀的程序员,十分优秀!