Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enabled candor modules to be built-in as well
- Loading branch information
1 parent
18bcbfb
commit 074b022
Showing
6 changed files
with
135 additions
and
80 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
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,81 @@ | ||
require = global.require | ||
|
||
throw = (err) { | ||
global.prettyPrint(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) { | ||
if (!host) host = "0.0.0.0" | ||
check(self.socket:bind(host, port)) | ||
check(self.socket:listen(128, (status) { | ||
check(status) | ||
client = new ClientPrototype | ||
socket = Tcp.create() | ||
client.socket = socket | ||
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) { | ||
p("createServer", onConnection) | ||
server = new ServerPrototype | ||
socket = Tcp.create() | ||
server.socket = socket | ||
server.onConnection = onConnection | ||
return server | ||
} | ||
|
||
return { | ||
StreamPrototype: StreamPrototype, | ||
ClientPrototype: ClientPrototype, | ||
ServerPrototype: ServerPrototype, | ||
createServer: createServer | ||
} |
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
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
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,28 @@ | ||
#include "cio_libs.h" | ||
|
||
#include "candor.h" | ||
|
||
#include <stdio.h> | ||
|
||
using namespace candor; | ||
|
||
|
||
#define XX(name) \ | ||
extern char _binary_lib_##name##_can_start; \ | ||
extern char _binary_lib_##name##_can_end; \ | ||
static Handle<Object> module_##name; \ | ||
Object* cio_##name##_module() { \ | ||
if (!module_##name.IsEmpty()) { \ | ||
return *module_##name; \ | ||
} \ | ||
char* code = &_binary_lib_##name##_can_start; \ | ||
int len = &_binary_lib_##name##_can_end - \ | ||
&_binary_lib_##name##_can_start; \ | ||
Function* fn = Function::New(code, len); \ | ||
module_##name.Wrap(fn->Call(0, NULL)); \ | ||
return *module_##name; \ | ||
} \ | ||
|
||
CIO_LIB_MAP(XX) | ||
#undef XX | ||
|
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,13 @@ | ||
#ifndef CIO_LIBS_H | ||
#define CIO_LIBS_H | ||
|
||
#include "candor.h" | ||
|
||
#define CIO_LIB_MAP(XX) \ | ||
XX(net) | ||
|
||
#define XX(name) candor::Object* cio_##name##_module(); | ||
CIO_LIB_MAP(XX) | ||
#undef XX | ||
|
||
#endif |