Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a446d2
commit adfb8a7
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
print = global.print | ||
p = global.prettyPrint | ||
require = global.require | ||
|
||
throw = (err) { | ||
p(err) | ||
global.exit() | ||
} | ||
|
||
// Used to emit on errors when libuv badness happens | ||
check = (status) { | ||
if (status) { | ||
throw(require('uv').lastError()) | ||
} | ||
} | ||
|
||
Tcp = require('tcp') | ||
|
||
StreamPrototype = {} | ||
StreamPrototype.pipe = (self, stream) { | ||
self.onData = (chunk) { | ||
stream:write(chunk) | ||
} | ||
self.onEnd = () { | ||
stream:end() | ||
} | ||
} | ||
|
||
ClientPrototype = new StreamPrototype | ||
ClientPrototype.write = (self, chunk, callback) { | ||
check(self.socket:write(chunk, callback)) | ||
} | ||
ClientPrototype.end = (self) { | ||
check(self.socket:shutdown((status) { | ||
check(status) | ||
self.socket:close() | ||
})) | ||
} | ||
|
||
ServerPrototype = {} | ||
ServerPrototype.listen = (self, port, host, callback) { | ||
p(self, port, host, callback) | ||
if (!host) host = "0.0.0.0" | ||
print("bind", host, port) | ||
check(self.socket:bind(host, port)) | ||
check(self.listen(128, (status) { | ||
check(status) | ||
client = new ClientPrototype | ||
client.socket = socket | ||
socket = Tcp.create() | ||
check(self.socket:accept(socket)) | ||
self.onConnection(client) | ||
check(socket:readStart((nread, chunk) { | ||
if (nread == -1) { | ||
err = require('uv').lastError() | ||
if (err.name == "EOF") { | ||
client.onEnd() | ||
} else { | ||
throw(err) | ||
} | ||
return | ||
} | ||
if (nread > 0) { | ||
client.onData(chunk) | ||
return | ||
} | ||
})) | ||
})) | ||
} | ||
|
||
createServer = function (onConnection) { | ||
server = new ServerPrototype | ||
socket = Tcp.create() | ||
server.socket = socket | ||
server.onConnection = onConnection | ||
return server | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
// Make an echo server using the API | ||
createServer((client) { | ||
client:pipe(client) | ||
}):listen(8080) | ||
print("TCP echo server listening on port 8080") |