Git Product home page Git Product logo

cheetah's People

Contributors

bausshf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cheetah's Issues

Client indentificaton

in this example you wrote:

module simple_socket_server_example;

import vibe.d : logInfo;

import cheetah;

shared static this() {
  // Creates a server listening at 127.0.0.1:8899
  auto server = new SocketServer!Client("127.0.0.1", 8899);

  // Attaches events to the server
  server.attach(SocketEventType.connect, new SocketEvent!Client(&onConnect));
  server.attach(SocketEventType.receive, [
    new SocketEvent!Client(&onReceiveHead),
    new SocketEvent!Client(&onReceiveBody)
  ]);
  server.attach(SocketEventType.disconnect, new SocketEvent!Client(&onDisconnect));
  server.attach(SocketEventType.error, new SocketEvent!Client(&onError));

  // Sets up events that has to be local per client.
  // This is usually used with events using moveNext() ...
  server.copyReceiveEvents = true;

  // Starts the server
  server.start();
}

/// Custom client wrapper that we use to handle custom packet specifications
class Client {
  size_t expectedAmount;
  ushort type;
}

/// Handling connections
void onConnect(SocketEventArgs!Client e) {
  logInfo("A client has connected ...");

  e.client.data = new Client;
}

/// Handling disconnections
void onDisconnect(SocketEventArgs!Client e) {
  logInfo("A client has disconnected ...");
}

/// Handling receive for the packet head
void onReceiveHead(SocketEventArgs!Client e) {
  e.client.read(4 - e.currentReceiveAmount);

  if (e.currentReceiveAmount >= 4) {
    auto ptr = e.buffer.ptr;

    e.client.data.expectedAmount = (*cast(ushort*)(ptr));
    e.client.data.type = (*cast(ushort*)(ptr + 2));

    logInfo("Received head ...");
    import std.conv : to;
    logInfo(to!string(e.client.data.expectedAmount));
    logInfo(to!string(e.client.data.type));

    e.resetReceive(e.currentReceiveAmount);
    e.client.moveNext(SocketEventType.receive);
  }
}

/// Handling receive for the packet body
void onReceiveBody(SocketEventArgs!Client e) {
  e.client.read(e.client.data.expectedAmount - e.currentReceiveAmount);

  if (e.currentReceiveAmount >= e.client.data.expectedAmount) {
    logInfo("Received body ...");
    import std.conv : to;
    logInfo(to!string(e.buffer[4 .. $]));

    e.resetReceive(e.currentReceiveAmount - e.client.data.expectedAmount);
    e.client.moveNext(SocketEventType.receive);
  }
}

/// Handling errors
void onError(SocketEventArgs!Client e) {
  logInfo("Error: " ~ e.error.toString());

  if (e.client) {
    e.client.close();
  }
  else if (e.server) {
    e.server.stop();
  }
}
  1. How to I get access to e.client.id or any embeddable param to identify client who sent a message?
  2. Does cheetah support broadcasting a message to multiple clients?
  3. How do I direct a message to a particular client like:
Client[] clients = .....
...
client[x].send(fromID, mesage);

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.