Skip to content

Instantly share code, notes, and snippets.

View vndmtrx's full-sized avatar
💅
Fazendo vários nadas

Eduardo N.S.R. vndmtrx

💅
Fazendo vários nadas
View GitHub Profile
@vndmtrx
vndmtrx / jquery_inject_google.js
Created August 5, 2012 15:57
Injetanto jQuery do Google
(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);
})();
@vndmtrx
vndmtrx / epoll_sock.c
Created August 17, 2012 18:01
Socket não bloqueante com notificação de eventos usando epoll()
/* 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,
@vndmtrx
vndmtrx / epol_create.c
Created August 21, 2012 20:31
Criando um epoll
int i_epoll;
i_epoll = epoll_create1 (0);
if (i_epoll == -1) {
CHUCKNORRIS("epoll_create1");
}
@vndmtrx
vndmtrx / cria_evento_epoll.c
Created August 21, 2012 20:31
Criando evento epoll
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);
@vndmtrx
vndmtrx / func_cria_evento.c
Created August 21, 2012 20:32
Função cria evento
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");
@vndmtrx
vndmtrx / epoll_maquina_eventos.c
Created August 21, 2012 20:32
Maquina de eventos com epoll
#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
}
}
@vndmtrx
vndmtrx / epoll_variaveis.c
Created August 21, 2012 20:33
Variáveis de ambiente
#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)
@vndmtrx
vndmtrx / epoll_cria_servidor.c
Created August 21, 2012 20:34
Função cria_servidor
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.");
}
@vndmtrx
vndmtrx / epoll_nonblock_socket.c
Created August 21, 2012 20:34
Função nonblock_socket
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;
@vndmtrx
vndmtrx / epoll_main.c
Created August 21, 2012 20:35
Aplicação Principal
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);