Giter VIP home page Giter VIP logo

freddie's Introduction

Freddie: Elixir Socket Framework

See the getting started guide and the online documentation.

Overview

Freddie is the Socket Framework for the Elixir. It is written to work with optimized non-bloking socket IO, enabling clients to communicate very quickly. Use Google's protobuf as a unit of packets. Thus, it is easy to create an implementation on most platforms and languages. One protocol file per protocol is matched (Nested structures are also allowed). All messages are optionally encrypted based on AES256. For example, you can use encryption for security-critical messages, and in most cases, you can use it without encryption. All code in freddie is written to follow OTP and works fault-tolerance in the event of a failure. Currently, it can only be used with TCP restrictions, but it will support the RUDP protocol.

Freddie's default structure

Freddie

Freddie's internal structure can be separated in several ways. Each element works only for its own role.

  • Router

    • Categorize the packets sent by the client and then dispatch them to each handler.
    • Unpacking and decrypting packets are provided internally.
    • Handlers for special cases such as connection and disconnection are also available.
  • Session

    • This is the endpoint where the connection to each client is abstracted.
    • Freddie offers several protocols, but for one client, it works through Session.
  • Context

    • The information in the Session and the user-available map are internal structures.
    • Corresponds with one context per session. Therefore, Session can be identified through Context.
    • Context is synchronized through Session, so synchronization is guaranteed for writing. However, when you read it in another process, you can temporarily see the context of the past.
  • Scheme

    • Module defining packets used by the application.
  • Protocols

    • Match the structure of each protocol and packet.
    • This is where the protocol number is defined number.

Installation

$ mix local.hex
$ mix archive.install hex freddie_scaffold [version]

Create Freddie Application

Create a project with the following commands:

$ mix freddie.scaffold echo_server

Configurate Application

Configure a confg/config.exs file

# configure freddie
config :freddie,
  # Type app name
  app_mod: :echo_server,

  # Type listen port
  port: 5050,
  # Type size of acceptor pool
  acceptor_pool_size: 100,

  # Type redis host address
  redis_host: "localhost",
  # Type redis port
  redis_port: 6379,
  # Type size of redis pool
  redis_pool_size: 10,

  # Type packet scheme root mod
  scheme_root_mod: EchoServer.Scheme,
  # Type protocol type root mod
  packet_type_mod: EchoServer.Protocols.Types,
  # Type packet handler mod (derive from Freddie.Router)
  packet_handler_mod: EchoServer.Handler

Add Protocol Schemes

You can add a schema for the protocol under the path 'lib/echo_server/scheme'

lib/echo_sever/scheme/cs_echo.proto

syntax = "proto3";

message CS_Echo {
    string msg = 1;
}

lib/echo_server/scheme/sc_echo.proto

syntax = "proto3";

message SC_Echo {
    string msg = 1;
}

Define Protocol

And then you define spec of protocols like this

lib/echo_server/protocols.ex

defmodule EchoServer.Protocols do
  use EnumType

  # define protocol type and protocol number (please type full name of schemes)
  defenum Types do
    # echo
    value(EchoServer.Scheme.CS_Echo, 1)
    value(EchoServer.Scheme.SC_Echo, 2)
  end
end

Add Protocol Handler

Finally, you define protocol-specific processes in handler.

lib/echo_server/handler.ex

defmodule EchoServer.Handler do
  use Freddie.Router

  require Logger

  alias EchoServer.Handler

  # define (Client-Server) type protocol handler
  defhandler EchoServer.Scheme.CS_Echo do
    {meta, msg, context}
    |> Handler.Echo.handle()
  end

  # define connnection event handler
  connect do
    Logger.info("Client #{inspect(context)} is connected!")
  end

  # define disconnection event handler
  disconnect do
    Logger.info("Client #{inspect(context)} is disconnected!")
  end
end

lib/echo_server/handler/echo.ex

defmodule EchoServer.Handler.Echo do
  alias EchoServer.Scheme

  def handle({_meta, msg, context}) do
    # make response message
    response_packet =
      [{:msg, msg.msg}]
      |> Keyword.new()
      |> Scheme.SC_Echo.new()

    # send a meesage to session using context
    Freddie.Session.send(context, response_packet)
  end
end

Start Application

Start the server with the following command

start server with iex:

$ iex --erl "+spp true +K true +Q 65536 +IOp 2 +scl false" -S mix freddie.server start

or

start server without iex:

$ elixir --erl "+spp true +K true +Q 65536 +IOp 2 +scl false" -S mix freddie.server start

Todo

  1. Provides Reliable UDP communication(Guarantee the latest order or Guaranteed both sequence and retransmission)
  2. Optimize network code
  3. Divide transmission into reliable and unreliable
  4. Add scaffold mix tool
  5. Add route mix tool & web page for dev

Examples

example projects

freddie's People

Contributors

kernelgarden avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.