2017 © Pedro Peláez
 

framework sockswork

Alexya's SocksWork components

image

alexya-framework/sockswork

Alexya's SocksWork components

  • Tuesday, June 6, 2017
  • by manulaiko
  • Repository
  • 1 Watchers
  • 0 Stars
  • 7 Installations
  • PHP
  • 0 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

SocksWork

Alexya's SocksWork library, (*1)

Contents

, (*2)

SocksWork

The class \Alexya\SocksWork\SocksWork provides an easy way to connect to a server and send packets., (*3)

The constructor accepts as parameter the host of the server, the port and the timeout of the connection. It also accepts as 4th parameter a boolean that indicates if SocksWork should connect to the server once the constructor has finnished or not, by default it's set to true, however if you set it to false you'll need to call the \Alexya\SocksWork\SocksWork::connect method., (*4)

Example:, (*5)

<?php

$SocksWork = new SocksWork("localhost", 8080, 100); // Connects to localhost:8080 and sets a timeout of 100ms

Once the connection has been established you can send anything with the \Alexya\SocksWork\SocksWork::send command that accepts as parameter the binary data to send or an instance of \Alexya\SocksWork\PacketBuilder., (*6)

If the parameter is binary data the response will be set to the \Alexya\SocksWork\SocksWork::$response property, if it's a \Alexya\SocksWork\PacketBuilder instance the response will be sent directly to it's property., (*7)

The method also accepts a 2nd parameter that is a boolean that indicates wether if SocksWork should wait for the response or not., (*8)

Example:, (*9)

<?php

$SocksWork->send((binary) "Hello world!");
echo $SocksWork->response; // Response to the packet

$packet = new PacketBuilder();
$packet->writeString("message", "Hello World!");

$SocksWork->send($packet);

echo $packet->readString("response"); // Response to the packet

To close the connection simply call the method \Alexya\SocksWork\SocksWork::close and when you want to reconnect call the method \Alexya\SocksWork\SocksWork::connect, to see if SocksWork is already connected use the method \Alexya\SocksWork\SocksWork::isConnected., (*10)

, (*11)

PacketBuilder

The class \Alexya\SocksWork\PacketBuilder provides an interface to build packets that will be sent with \Alexya\SocksWork\SocksWork, it also provides methods for reading the response., (*12)

The constructor accepts as parameter an object of type \Alexya\SocksWork\IEncoder that will be the encoder to use to write and read the packet (By default it's an instance of \Alexya\SocksWork\Encoders\StringEncoder)., (*13)

Once the constructor has been called you need to can start adding parameters to the packet by calling the methods of the encoder throw the instanced object., (*14)

Example:, (*15)

<?php

$packet = new PacketBuilder();

$packet->writeShort("id", 1);
$packet->writeString("name", "test");

$SocksWork->send($packet);

$id   = $packet->readShort("id");
$name = $packet->readString("name");

You can also extend this class for simplifying the instantation of the object:, (*16)

<?php

class SetUserName extends PacketBuilder
{
    private $_id = 1;

    public $id   = -1;
    public $name = "";

    public function onInstance(string $name)
    {
        $this->_encoder->writeShort("id", $this->_id);
        $this->_encoder->writeString("name", $name);
    }

    public function onResponse()
    {
        $this->id   = $this->_ecoder->readShort("id");
        $this->name = $this->_encoder->readString("name");
    }
}

$packet = new SetUserName("test");

$SocksWork->send($packet);

$id   = $packet->id;
$name = $packet->name;

The method \Alexya\SocksWork\PacketBuilder::onInstance is executed right after the constructor and receives as parameters the arguments sent to the constructor (without the instance of the encoder)., (*17)

For example:, (*18)

<?php

class Packet extends PacketBuilder
{
    public function onInstance()
    {
        foreach(func_get_args() as $key => $val) {
            echo "{$key} => {$value}\n";
        }
    }
}

$packet = new Packet(1, "test", "foo");
// Output:
// 0 => 1
// 1 => test
// 2 => foo

$packet = new Packet(new \Alexya\SocksWork\Encoders\String(), 1, "test");
// Output:
// 0 => 1
// 1 => test

The method \Alexya\SocksWork\PacketBuilder::onResponse is executed right after the response has been received., (*19)

, (*20)

Encoders

The encoders are the classes that will encode the data that SocksWork will send, they must extend the class \Alexya\SocksWork\Encoder. You can see the available encoders in the namespace \Alexya\SocksWork\Encoders., (*21)

The Versions

06/06 2017

dev-develop

dev-develop

Alexya's SocksWork components

  Sources   Download

GNU

The Requires

  • php >=7.1

 

by Avatar manulaiko

framework php php7 alexya sockets sockswork

06/06 2017

dev-master

9999999-dev

Alexya's SocksWork components

  Sources   Download

GNU

The Requires

  • php >=7.1

 

by Avatar manulaiko

framework php php7 alexya sockets sockswork

24/08 2016

3.0.1

3.0.1.0

Alexya's SocksWork components

  Sources   Download

GNU

The Requires

  • php >=7.0

 

by Avatar manulaiko

framework php php7 alexya sockets sockswork