September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,40 +1,42 @@
|
|||
import asyncnet, asyncdispatch
|
||||
|
||||
type TClient = tuple[socket: AsyncSocket, name: string]
|
||||
var clients: seq[Client] = @[]
|
||||
type
|
||||
Client = tuple
|
||||
socket: AsyncSocket
|
||||
name: string
|
||||
connected: bool
|
||||
|
||||
var clients {.threadvar.}: seq[Client]
|
||||
|
||||
proc sendOthers(client: Client, line: string) {.async.} =
|
||||
for c in clients:
|
||||
if c != client:
|
||||
if c != client and c.connected:
|
||||
await c.socket.send(line & "\c\L")
|
||||
|
||||
proc processClient(socket: AsyncSocket) {.async.} =
|
||||
await socket.send("Please enter your name: ")
|
||||
let client: Client = (socket, await socket.recvLine())
|
||||
var client: Client = (socket, await socket.recvLine(), true)
|
||||
|
||||
clients.add client
|
||||
discard client.sendOthers "+++ " & client.name & " arrived +++"
|
||||
clients.add(client)
|
||||
asyncCheck client.sendOthers("+++ " & client.name & " arrived +++")
|
||||
|
||||
while true:
|
||||
let line = await client.socket.recvLine()
|
||||
if line == "":
|
||||
discard client.sendOthers "--- " & client.name & " leaves ---"
|
||||
break
|
||||
discard client.sendOthers client.name & "> " & line
|
||||
|
||||
for i,c in clients:
|
||||
if c == client:
|
||||
clients.del i
|
||||
break
|
||||
asyncCheck client.sendOthers("--- " & client.name & " leaves ---")
|
||||
client.connected = false
|
||||
return
|
||||
asyncCheck client.sendOthers(client.name & "> " & line)
|
||||
|
||||
proc serve() {.async.} =
|
||||
clients = @[]
|
||||
var server = newAsyncSocket()
|
||||
server.bindAddr(Port(4004))
|
||||
server.listen()
|
||||
|
||||
while true:
|
||||
let socket = await server.accept()
|
||||
discard processClient socket
|
||||
asyncCheck processClient(socket)
|
||||
|
||||
discard serve()
|
||||
asyncCheck serve()
|
||||
runForever()
|
||||
|
|
|
|||
49
Task/Chat-server/Zkl/chat-server.zkl
Normal file
49
Task/Chat-server/Zkl/chat-server.zkl
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
const PORT=23;
|
||||
|
||||
var users=Dictionary(); // ( handle:socket, ...)
|
||||
pipe:=Thread.Pipe(); // how server tells thread to connect to user
|
||||
|
||||
fcn accept(pipe){ // a thread waiting for the server to send a socket
|
||||
while(socket:=pipe.read()){
|
||||
println("Somebody is connecting ...");
|
||||
socket.read(); // telnet stuff
|
||||
while(True){ // get credentials
|
||||
reg name;
|
||||
socket.write("Your handle: "); // bottle neck
|
||||
try{ name = socket.read().text.strip() } catch(IOError){ continue }
|
||||
if(users.holds(name)) socket.write("Handle is already in use.\n");
|
||||
else if(name){
|
||||
users[name] = socket;
|
||||
chat.launch(name,socket); // thread
|
||||
broadcast(name, "+++ %s arrived +++".fmt(name));
|
||||
break; // wait for next connection
|
||||
}
|
||||
}//while
|
||||
}//while
|
||||
}.launch(pipe); // thread
|
||||
|
||||
fcn chat(name,socket){ // a thread, one per user
|
||||
try{
|
||||
socket.write("^D to disconnect\n");
|
||||
while(True){
|
||||
message:=socket.read().text.strip();
|
||||
if(message=="\xff\xec") break; // ^D to disconnect.
|
||||
broadcast(name, "%s> %s".fmt(name,message));
|
||||
}
|
||||
}catch{} // eg socket pukes
|
||||
users.del(name); socket.close();
|
||||
broadcast(name, "--- %s leaves ---".fmt(name));
|
||||
}
|
||||
|
||||
// Send a message to all users from the given name.
|
||||
fcn broadcast(name, message){ // called from user thread
|
||||
println(message); // log message to server console
|
||||
users.pump(Void,'wrap([(toName,socket)]){
|
||||
if(toName != name) try{ socket.write(message + "\n") } catch(IOError){}
|
||||
});
|
||||
}
|
||||
|
||||
// Set up the server socket.
|
||||
server:=Network.TCPServerSocket.open(PORT);
|
||||
println("Listening on %s:%s".fmt(server.hostname,server.port));
|
||||
server.listen(pipe); // Main event loop
|
||||
Loading…
Add table
Add a link
Reference in a new issue