• Stars
    star
    92
  • Rank 350,011 (Top 8 %)
  • Language
    Elixir
  • Created over 10 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

You need more reagents to conjure this server.

reagent - socket acceptor pool

reagent is a socket acceptor pool for Elixir that leverages the socket library and its protocols to provide an easy way to implement servers.

Getting started

To define a reagent you first have to define a module using the reagent behaviour. This will define some basic functions you can extend and other helpers on the module and will make it startable as a reagent.

defmodule Test do
  use Reagent.Behaviour
end

When you want to start a server running the defined reagent, you have to call Reagent.start. It takes as first parameter the module implementing the behaviour and as second parameter a listener descriptor.

Listener descriptors contain the definition of the listener, including port, whether they're secure or not, other socket options and starting environment.

Reagent behaviour

A reagent to do anything useful has to either implement handle/1 or start/1.

handle/1 is called by the default start/1 and it gets called as a replacement for the acceptor process. It gets called with a Reagent.Connection record.

This is usually useful to implement simple protocols when you don't need a full blown gen_server or similar to handle a connection.

If you want more complex connection handling you can define start/1, it gets called with a Reagent.Connection record as well and must return { :ok, pid } or { :error, reason }. The returned process will be made owner of the socket and be used as reference for the connection itself.

You can also define accept/1 which gets called with the Reagent.Listener and allows you more fine grained socket acception.

Simple example

defmodule Echo do
  use Reagent

  def handle(conn) do
    case conn |> Socket.Stream.recv! do
      nil ->
        :closed

      data ->
        conn |> Socket.Stream.send! data

        handle(conn)
    end
  end
end

This is a simple implementation of an echo server.

To start it on port 8080 just run Reagent.start Echo, port: 8080.

Complex example

defmodule Echo do
  use Reagent

  def start(connection) do
    GenServer.start __MODULE__, connection, []
  end

  use GenServer

  def init(connection) do
    { :ok, connection }
  end

  # this message is sent when the socket has been completely accepted and the
  # process has been made owner of the socket, you don't need to wait for it
  # when implementing handle because it's internally handled
  def handle_info({ Reagent, :ack }, connection) do
    connection |> Socket.active!

    { :noreply, connection }
  end

  def handle_info({ :tcp, _, data }, connection) do
    connection |> Socket.Stream.send! data

    { :noreply, connection }
  end

  def handle_info({ :tcp_closed, _ }, connection) do
    { :stop, :normal, connection }
  end
end

This is the implementation of a full-blown gen_server based echo server (which is obviously overkill).

As with the simple example you just start it with Reagent.start Echo, port: 8080.

More Repositories

1

amnesia

Mnesia wrapper for Elixir.
Elixir
682
star
2

elixir-socket

Socket wrapping for Elixir.
Elixir
679
star
3

ruby-tesseract-ocr

A Ruby wrapper library to the tesseract-ocr API.
Ruby
627
star
4

ruby-thread

Various extensions to the base thread library.
Ruby
525
star
5

rust-ffmpeg

Safe FFmpeg wrapper.
Rust
443
star
6

rust-tun

TUN device creation and handling.
Rust
215
star
7

elixir-datastructures

Datastructures for Elixir.
Elixir
212
star
8

rust-ffmpeg-sys

moved to meh/rust-ffmpeg
Rust
147
star
9

cancer

It's terminal.
Rust
129
star
10

urna

REST in peace.
Elixir
95
star
11

smart-referer

MOVED to GitLab: https://gitlab.com/smart-referer/smart-referer/
JavaScript
95
star
12

dux

DUX MEA LUX
Rust
89
star
13

exquisite

LINQ-like match_spec generation for Elixir.
Elixir
78
star
14

rust-packet

Network packet handling for Rust.
Rust
76
star
15

cauldron

I wonder what kind of Elixir is boiling in there.
Elixir
74
star
16

jazz

Yet another library to handle JSON in Elixir.
Elixir
61
star
17

rorschach

And I'll look down and whisper "GNO."
Rust
59
star
18

screenruster

X11 screen saver and locker.
Rust
53
star
19

miserve

Servo based minimal browser.
PostScript
48
star
20

blender

Blend in the crowd by faking to be the most common Firefox browser version, operating system and other stuff.
Ruby
42
star
21

rust-uinput

Linux uinput wrapper.
Rust
39
star
22

lissio

E vai col lissio!
Ruby
39
star
23

rust-terminfo

Terminal information for Rust.
Rust
38
star
24

cesso

CSV handling library for Elixir.
Elixir
26
star
25

rust-xcb-util

Rust bindings and wrappers for XCB utility functions.
Rust
26
star
26

ruby-mbox

A Ruby mbox parser.
Ruby
23
star
27

steamy

Steam controller handling.
Rust
21
star
28

ruby-threadpool

A simple no-wasted-resources thread pool implementation.
Ruby
21
star
29

ruby-x11

X11 bindings for Ruby
Ruby
20
star
30

wrong

The most wrong build system and package manager for C and C++.
Makefile
19
star
31

elixir-benchmark

Elixir benchmark utilities.
Elixir
18
star
32

mad

Managers & Developers
18
star
33

rust-mailbox

MBOX reader.
Rust
17
star
34

rust-hid

HID access library.
Rust
15
star
35

moc

Mirror of the moc project, with some of my own changes
C
15
star
36

rust-shmem

Shared memory for Rust.
Rust
14
star
37

ruby-clj

Like json, but with clojure sexps.
Ruby
14
star
38

NetCommander

NetCommander - An easy to use arp spoofing tool.
Python
14
star
39

miniLOL

Not just a CMS.
JavaScript
12
star
40

derp

Elixir error logger.
Elixir
12
star
41

continuum

Spaaaaceeeeeeeeeeeee.
Elixir
12
star
42

nucular

A reactor in D.
D
12
star
43

ruby-torchat

Torchat implementation in Ruby, event-driven EventMachine based library.
Ruby
12
star
44

rust-sixel

Sixel encoder/decoder for Rust.
Rust
11
star
45

ruby-ffi-inline

Inline C/C++ in Ruby easily and cleanly.
Ruby
11
star
46

rust-lzma

LZMA handling library.
Rust
10
star
47

fag

Forums Are Gay.
Ruby
10
star
48

clj.js

parse/dump Clojure like you'd do with JSON
JavaScript
10
star
49

vimmeh

Vim Script
9
star
50

rust-bdf

BDF format handling.
Rust
9
star
51

bitlbee-omegle

Omegle plugin for BitlBee.
C
9
star
52

lulzKey

lulzKey. Let's close those effin Windows so penguins won't enter.
C++
8
star
53

neovimmeh

Fennel
8
star
54

elixir-operators

Ever wanted operators which are as slow as can be? Now you can!
Elixir
8
star
55

arpoon

Man the harpoons, kill the spoofer!
Ruby
8
star
56

exts

Elixir Terms Storage, ets wrapper.
Elixir
8
star
57

failirc

Fail IRC library in Ruby.
Ruby
7
star
58

lulzhttpd

A C++ web server.
C++
7
star
59

screenruster-saver

Helper library to create screen savers.
Rust
7
star
60

rust-uinput-sys

Linux uinput definitions.
Rust
7
star
61

elixir-managed_process

Garbage collected processes.
Elixir
7
star
62

npapi-mumble-link

Mumble Link wrapped as an NPAPI plugin.
C
7
star
63

expo

XPosed for Kotlin.
Kotlin
7
star
64

opentyrian

opentyrian mirror with some of my own changes
C
7
star
65

rust-openal

OpenAL wrapper
Rust
6
star
66

rust-picto

Image handling library for Rust.
Rust
6
star
67

ruby-fuse

Bindings and wrapper for FUSE with FFI.
Ruby
6
star
68

shumei

Shumei brings an actor system and other concurrency plumbing primitives to the web.
TypeScript
6
star
69

random

Random codes.
Assembly
6
star
70

elixir-finalizer

Define finalizers using the cool NIF hack.
Elixir
6
star
71

httprot

Prot prot prot.
Elixir
6
star
72

screenruster-saver-laughing_man

I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
Rust
6
star
73

ruby-call-me

Various calling things, overload, pattern matching, memoization and such.
Ruby
6
star
74

orgasm

ORGanized ASseMbly: a Ruby (dis)?assembler library (NOT a (dis)?assembler FOR Ruby, a (dis)?assembler IN Ruby)
Ruby
6
star
75

llvm-lol

LOLCODE LLVM implementation.
C++
5
star
76

ruby-glyr

Ruby wrapper for glyr.
Ruby
5
star
77

decimex

erlang-decimal wrapper for Elixir
Elixir
5
star
78

nohomo

Homoglyphs begone!
Vim Script
5
star
79

clj-sockets

POSIX sockets for Clojure.
Clojure
5
star
80

rust-hwaddr

MAC address handling.
Rust
5
star
81

colorb

Colorify your strings
Ruby
5
star
82

rust-xkb

Rusty wrapper to libxkbcommon.
Rust
5
star
83

boolean-expression

A simple library to evaluate boolean expressions.
Ruby
5
star
84

bitlbee-torchat

Bitlbee plugin for using torchat.
C
5
star
85

amirite

Include and forget testing framework for C++11
C++
5
star
86

dexts

Disk Elixir Terms Storage, dest wrapper.
Elixir
5
star
87

nokku

Knock knock knock, hitori bocchi.
Rust
4
star
88

rust-xkbcommon-sys

Bindings to libxkbcommon.
Rust
4
star
89

screenruster-saver-hacks

ScreenRuster support for XScreenSaver hacks.
Rust
4
star
90

libcss

A library to parse and work with CSS
C
4
star
91

ela

Embedded Linear Algebra
C++
4
star
92

faildns

Fail DNS library in Ruby
Ruby
4
star
93

rust-curses-sys

Rust
3
star
94

ruby-clit

Simple library to make nice themeable CLI output.
Ruby
3
star
95

rft

Shitty Rust FFT implementation.
Rust
3
star
96

fagadget

New cross-toolkit and cross-platform desktop-gadget prototype.
C++
3
star
97

zwm

Shell
3
star
98

cryptsetup

Mirror of cryptsetup with some of my own changes
C
3
star
99

LOLastfm

LOL it's a scrobbler.
Ruby
3
star
100

parsepples

A parslet-like parser for C++
C++
3
star