This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var script = document.createElement("script"); | |
script.src = "//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"; | |
script.onload = script.onreadystatechange = function(){ console.log("done;"); }; | |
document.body.appendChild(script); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright 2012 Eduardo Rolim | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int i_epoll; | |
i_epoll = epoll_create1 (0); | |
if (i_epoll == -1) { | |
CHUCKNORRIS("epoll_create1"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct epoll_event i_evento; | |
int status; | |
i_evento.data.fd = sock; | |
i_evento.events = EPOLLIN | EPOLLET; | |
epoll_ctl (i_epoll, EPOLL_CTL_ADD, sock, &i_evento); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int cria_evento(int epoll, int sock) { | |
int status; | |
struct epoll_event i_evento; | |
// Criando um evento em "epoll" para o descritor "sock" | |
i_evento.data.fd = sock; | |
i_evento.events = EPOLLIN | EPOLLET; | |
status = epoll_ctl (epoll, EPOLL_CTL_ADD, sock, &i_evento); | |
if (status == -1) { | |
CHUCKNORRIS("cria epoll_ctl"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define QTDEVENTOS 64 | |
struct epoll_event *i_eventos; | |
i_eventos = calloc(QTDEVENTOS, sizeof(struct epoll_event)); | |
while (1) { | |
int n, i; | |
n = epoll_wait(i_epoll, i_eventos, QTDEVENTOS, -1); | |
for (i = 0; i < n; i++) { | |
//percorre cada evento fazendo alguma coisa | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define TAMBUFFER 512 | |
#define PORTA 9899 | |
#define QTDEVENTOS 64 | |
#define CHUCKNORRIS(x) perror(x); exit(EXIT_FAILURE); | |
#define DEBUG(x) fprintf(stdout, "%d: %s", __LINE__, x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int cria_servidor(int porta) { | |
struct sockaddr_in endereco; | |
int sock, status; | |
// Criando o socket servidor | |
sock = socket(PF_INET, SOCK_STREAM, 0); | |
if (sock < 0) { | |
CHUCKNORRIS("Erro de criação do socket."); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static int nonblock_socket(int sock) { | |
int flags, status; | |
flags = fcntl(sock, F_GETFL, 0); | |
if (flags == -1) { | |
DEBUG("fcntl get"); | |
return -1; | |
} | |
flags |= O_NONBLOCK; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(int argc, char *argv[]) { | |
int status; | |
// Seta manipuladores para os sinais de término da aplicação. | |
signal(SIGINT, sigcallback); // Ctrl+C | |
signal(SIGTERM, sigcallback); // Comando "kill" | |
// Criando o servidor na porta designada | |
servidor = cria_servidor(PORTA); | |