- 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/
我能做到: sudo nano .bash_profile 但是当我这样做的时候: sudo echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bas
去年我花了整整两天时间试图让 python-mysql 工作在我干净安装的 Lion 上,最后绝望地放弃了。升级到山狮后,我想我会再试一次,但没有成功。可能是我对 Lion 的所有尝试都搞砸了我的设置
我有一个使用 py2app 转换为捆绑应用程序的 python 脚本。手动调用时,它运行良好。但是,在 Mountain Lion 下,当我尝试使用 launchd 调用它时,系统日志中出现以下错误:
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
我最近在我的 Mac 上安装了 Mountain Lion 操作系统,只是出于必要。我试图将我在 Xcode 5 中创建的应用程序连接到我的 iPhone,但它不起作用。我收到错误消息“身份‘iPho
题目地址:https://leetcode.com/problems/valid-mountain-array/description/ 题目描述 Given an array A of inte
题目地址:https://leetcode.com/problems/longest-mountain-in-array/description/ 题目描述 Let's call any (con
很多人都知道 Mac OS X 命令行工具 say,它可以从终端在 Mac 上执行文本到语音的转换: sh $ 说 Hello World ! 是否有可用于 Mountain Lion 听写应用程序的
我已经尝试了几个小时让 java 3d 在我的 macbook 上运行,但我不知道它出了什么问题......这是我所做的: - 我下载了 java3d 1.5 并替换了/System/Library/
我曾经在 Lion 中运行以下代码以打开终端,更新一个项目,然后打开一个新选项卡,然后再次运行以更新第二个、第三个和第四个。由于 Mountain Lion 升级,这不再有效。一半时间它似乎在我的主目
我尝试为 Mac OS 仪表板编写自己的小部件 - 一种显示丢失图像的相框,但比我之前发现或使用的其他小部件更漂亮。 为了获得一些资源/示例,我首先下载了...第二...第三个此类小部件,但没有任何效
基本上,我希望整个 OS X 桌面(或者理想情况下,除了菜单栏之外的所有内容)向上滑动并在底部显示我自己的 View ,就像 Mountain Lion 中的通知中心的工作方式一样(只是从底部开始)从
我正在构建一个适用于 10.8+ 的应用程序,其中必须修补文件。由于此功能,我创建了一个“PrivilegedHelperTool”并安装为 KeepLive = YES RunAtLoad = YE
Mountain Lion 提供了一个内置共享按钮,可显示适合该应用的共享服务菜单: 如何将其插入我的应用程序中? 最佳答案 要在 Mountain Lion 上添加分享按钮: 1) 添加一个名为 s
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
今天,我学习了一些关于让一切都在 Mountain Lion 上运行的教程。 我已经安装了Web Sharing并且启用了,我也启用了php(mac自带的,不知道更新了) 所以我没有安装 MySQL
我想在 Mountain Lion 上安装 Qt。为此,我去了http://qt-project.org/downloads并下载了最新的 Qt 库安装程序 (4.8.3)。然后我按照说明操作,在安装
更新到 mountain lion 后,我的 postgres 就可以工作了。它仍在运行,但我的应用程序无法再连接到它。 $ ps aux | grep postgres postgres
Traceback (most recent call last): File "./Scripts/version.py", line 6, in from Foundation im
我一直在尝试在我的系统(OSX 10.8,全新安装)上设置 PostgreSQL,但我在使用 psql、createdb 时遇到了麻烦,等。我尝试了各种解决方案,但似乎都没有用。 安装成功,我继续使用
我是一名优秀的程序员,十分优秀!