方針

TCPソケットを開く処理はRustで実装する。 Emacs側はプロセスを起動するだけに止め標準入出力でそのプログラムとやり取りする。

実装

[package]
name = "emacs-tcp"
version = "0.1.0"
authors = ["TakesxiSximada <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log = { version = "0.4" }
#+end_srcl


#+caption: src/main.rs
#+begin_src rust
use std::io::Write;
use std::io::Read;
use std::net::TcpListener;
use std::net::TcpStream;

fn handle(mut stream: TcpStream) {
    let mut buffer = [0; 1024];
    stream.read(&mut buffer).unwrap();
    println!("Receive: {:?}", buffer);
    let response = "HTTP/1.1 200 OK\r\n\r\nyay!!\r\n\r\n";
    stream.write(response.as_bytes()).unwrap();
    stream.flush().unwrap();
}

fn main() {
    let listener = TcpListener::bind("localhost:7878").unwrap();
    for stream in listener.incoming() {
	let stream = stream.unwrap();
	handle(stream);
    }
}
Cargo.toml
(defun networking-tcp-listen ()
  (interactive)
  (make-process
   :name "networking-tcp-listen"
   :buffer (get-buffer-create "*Networking TCP Listen*")
   :commmand '("./target/debug/emacs-tcp")))
			      
(provide 'networking)
networking.el