Giter VIP home page Giter VIP logo

rust-chat's Introduction

Simple Rust 1-1 chat

A simple one-on-one chat application. It can run in either a server or client mode. As a server, it will wait for clients to connect. Upon the client's connection termination, it will continue listening for the next client. Once a connection is stabilized, client and server can send messages to each other. The message receiver will acknowledge it with an ACK, and the sender will measure the roundtrip time.

Usage

    chat [OPTIONS]

FLAGS:
        --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
    -h, --host <addr>      IP address to listen for connections (server) or connect to (client)
    -m, --mode <MODE>      server/client
    -p, --port <number>    Port to listen for connections (server) or connect to (client)

To end the application, type the command \quit

Protocol

The protocol is very simple: the header h indicates the message type (MESSAGE, ACK, DISCONNECTED), followed by the byte buffer contents, which holds the encoded message. When the message type is ACK or DISCONNECTED, contents is empty:

| h | contents |

Examples:

ACK:

| 0xA |

DISCONNECTED:

| 0xC |

MESSAGE "Hello":

| 0xB | 'h' 'e' 'l' 'l' 'o' |

The message contents are converted to a utf8 string on the application level.

Implementation

When a connection is stablished, both modes will call the process_connections() function, the client will call it once and return when it's finished, while the server will loop, always waiting for new connections. The process_connections() function will spawn 2 threads, one for receiving messages from the network, and another to read the console input. Whenever there's an event from those 2 threads, they will communicate it to the main thread via a mpsc channel.

Possible Improvements

  • Handle errors in try_send()
  • Handle unix signals like SIGINT, that force the termination of the application
  • Handle commands (such as "\quit") outside the process_connections() function. Actually, this improvement involves a more general issue: currently, client and server are implemented in the same function, process_connections(), exactly the same way. However, they will probably have different requirements in the future.

rust-chat's People

Contributors

rafccq avatar

Stargazers

Xsocket avatar

Watchers

 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.