Giter Club home page Giter Club logo

dragonruby-cext-libsocket's Introduction

Libsocket Dragonruby C Extension

READ BEFORE TESTING AND USING

Right now we are in a testing phase. If you want to give feedback and test ffor how this works, do this:

git clone --branch dev https://github.com/bedwardly-down/dragonruby-cext-libsocket.git

If git branch shows master, delete your git repo and try again. I'll only take dev feedback and pull requests until at least the end of this phase.

Requirements

See here

Recommended way to use

  1. Clone repository as a submodule in your Dragonruby mygame directory:

git submodule add https://github.com/bedwardly-down/dragonruby-cext-libsocket.git mygame/libsocket

  1. Build

On Windows:

NOTE: -lws2_32 is a required flag for a specific Windows API library that's required for libsocket to work Also, your MingW may be in a different directory; adjust if needed.

clang -shared \
  --sysroot=C:\mingw-w64\mingw64^
  --target=x86_64-w64-mingw32 -fuse-ld=lld^
  -isystem include -I . -lws2_32^
  -o mygame\native\windows-amd64\libsocket.dll mygame\libsocket\libsocket-bind.c 

On Linux:

clang -shared \
  -isystem include -I . -fPIC \
  -o mygame/native/linux-amd64/libsocket.so mygame/libsocket/libsocket-bind.c

On Mac:

clang -shared \
  -isystem include -I . -fPIC \
  -o mygame/native/macos/libsocket.dylib mygame/libsocket/libsocket-bind.c
  1. Add to the top of your mygame/app/main.rb:
require 'libsocket/config.rb'
require 'libsocket/socket.rb'
$gtk.ffi_misc.gtk_dlopen("libsocket")
include FFI::SOCKET

That should be it.

Configuration

To configure, make a copy of config.rb.sample as config.rb, replace the default variables with what you need and then insert them where you need them in your source as Config::.

Every project will have different requirements.

NOTE: Do not commit code or open pull requests containing any kind of passwords or sensitive data. Once those get merged, it will be almost impossible to remove them from the repo.

Basic example of how to start and end (won't allow for sending and receiving just yet; still testing)

class Game

    attr_accessor :socket, :cycle_complete

    def initialize
        self.socket ||= Socket.new # wouldn't recommend creating this until you need it; the startup speed hasn't been tested nor has memory usage caused by library
        self.socket.c_api.protocol = 1 # to activate a TCP connection; you'll mostly want UDP unless you're doing something chat related or through certain APIs that require it
        self.cycle_complete ||= false # just to end the socket steps
    end

    def tick
        if @cycle_complete == false
            
            # just opening a direct connection that doesn't allow any communication yet to a fake server hosted on your local PC
            @socket.start "127.0.0.1", "8000" 
            
            # open communication between both sockets when you're ready
            @socket.open
            
            # send a message to the connection at 127.0.0.1:8000, what you passed on @socket.start
            @socket.send "Dragons eat children because children are evil"
            
            # take in a message from that server on its 9001 port and do what you want with it (still need to add an endpoint to have the message that was sent accessible to DragonRuby, but that can come with what's required to get that part actually usable)
            @socket.receive "127.0.0.1", "9001" 
            
            # we hate the other server; they can talk to our hand but may want to make up with them later, so leave the option to reopen communication; closed just for now
            @socket.close
            
            # they are mean, smelly and full of bad karma; let's never speak to them again and burn it all down
            @socket.shutdown
            
            # you'll want this put at the end because it's doing things that you can't see but is required for everything to talk back and forth; might hide this completely inside of other functions
            @socket.tick

            @cycle_complete = true
        end

        @socket = nil # would recommend this due to typically manually releasing memory in C when done with an object. This is just a precaution since the typical C mechanisms for that probably willl not work
    end
end

TODO

  • Add iOS, and Android support. Console support needs official development kits and will probably require you to roll your own code.

dragonruby-cext-libsocket's People

Contributors

bedwardly-down avatar kfischer-okarin avatar

Watchers

 avatar  avatar

dragonruby-cext-libsocket's Issues

Streamline Github Actions for all platforms

The current Github Actions works well enough but long term won’t scale or allow for platform specific testing without repeating too much code. Let’s fix that.

  • Create a generic build.sh script that works for git bash on Windows and the default shells for Mac and Linux; the same build command can work on all platforms but Windows will need extra arguments
  • Implement a way to toggle specific platform builds when testing only a single platform
  • Run Windows building and testing through git bash but Powershell will remain the main shell for Windows at this time; it has curl builtin and can unzip Dragonruby without needing to download any extra libraries that might not come with git bash.

Implement sending proper messages

When I say messages, I mean sending strings to and from a server to a client. Right now, that means sending raw strings and then capturing the response.

Build a TCP Client in DragonRuby to field test

Doesn’t need to be fancy at all. Just needs to connect two dragonruby instances over a network and get them to talk back and forth.

The moment that works, there’s already uses for it in games. It won’t allow for major multiplayer functionality; I would love to see some chat systems working though.

Decouple all libsocket functions

Every function needs to work on a tick by tick basis without directly relying on any other function.

Reasons:

  • Speed - every function needs to fire as fast as possible
  • Reliability - if a function fails, less chance of crashing DragonRuby entirely
  • Logging - all major events should be logged when something fails; DragonRuby needs full control of this with the C portion of the code not directly caring about any of it
  • Maintainability - only implement what is necessary in C; handle everything else in DragonRuby.
  • Scalability - as DragonRuby grows, a network library must be able to adapt to each new version to stay relevant. That ties directly into maintenance.

Note: this is a living list and anyone is welcome to contribute.

Reduce direct reliance on system headers and libraries

This means porting as much functionality as possible to SDL and any other framework that DragonRuby uses.

Anything that’s not provided, try these in order of priority:

  • Port directly to DragonRuby
  • Restructure function to use other functions in code
  • Implement missing method or function directly in code
  • Attempt to use something like this: https://github.com/embeddedartistry/libc
  • Extract function to an unused file in case it’s needed in the future
  • Remove entirely if function is completely irrelevant to DragonRuby

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.