mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
202 lines
5.1 KiB
202 lines
5.1 KiB
15 years ago
|
// Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
|
||
|
#ifndef SRC_NET_H_
|
||
|
#define SRC_NET_H_
|
||
16 years ago
|
|
||
15 years ago
|
#include <node.h>
|
||
15 years ago
|
#include <node_events.h>
|
||
16 years ago
|
#include <v8.h>
|
||
16 years ago
|
#include <evcom.h>
|
||
16 years ago
|
|
||
16 years ago
|
namespace node {
|
||
16 years ago
|
|
||
16 years ago
|
class Server;
|
||
16 years ago
|
|
||
16 years ago
|
class Connection : public EventEmitter {
|
||
15 years ago
|
public:
|
||
|
static void Initialize(v8::Handle<v8::Object> target);
|
||
16 years ago
|
|
||
15 years ago
|
protected:
|
||
16 years ago
|
/* v8 interface */
|
||
|
static v8::Persistent<v8::FunctionTemplate> constructor_template;
|
||
15 years ago
|
static v8::Handle<v8::Value> New(const v8::Arguments& args);
|
||
|
static v8::Handle<v8::Value> Connect(const v8::Arguments& args);
|
||
|
static v8::Handle<v8::Value> Send(const v8::Arguments& args);
|
||
|
static v8::Handle<v8::Value> SendUtf8(const v8::Arguments& args);
|
||
|
static v8::Handle<v8::Value> Close(const v8::Arguments& args);
|
||
|
static v8::Handle<v8::Value> ForceClose(const v8::Arguments& args);
|
||
|
static v8::Handle<v8::Value> SetEncoding(const v8::Arguments& args);
|
||
|
static v8::Handle<v8::Value> ReadPause(const v8::Arguments& args);
|
||
|
static v8::Handle<v8::Value> ReadResume(const v8::Arguments& args);
|
||
|
static v8::Handle<v8::Value> SetTimeout(const v8::Arguments& args);
|
||
15 years ago
|
static v8::Handle<v8::Value> SetNoDelay(const v8::Arguments& args);
|
||
15 years ago
|
|
||
|
static v8::Handle<v8::Value> ReadyStateGetter(v8::Local<v8::String> _,
|
||
16 years ago
|
const v8::AccessorInfo& info);
|
||
15 years ago
|
static v8::Handle<v8::Value> FDGetter(v8::Local<v8::String> _,
|
||
|
const v8::AccessorInfo& info);
|
||
16 years ago
|
|
||
15 years ago
|
Connection() : EventEmitter() {
|
||
15 years ago
|
encoding_ = BINARY;
|
||
16 years ago
|
|
||
|
host_ = NULL;
|
||
|
port_ = NULL;
|
||
|
|
||
|
Init();
|
||
|
}
|
||
15 years ago
|
virtual ~Connection();
|
||
|
|
||
|
int Connect(struct sockaddr *address) {
|
||
|
return evcom_stream_connect(&stream_, address);
|
||
|
}
|
||
|
|
||
|
void Send(const char *buf, size_t len) {
|
||
|
evcom_stream_write(&stream_, buf, len);
|
||
|
}
|
||
|
|
||
|
void Close() {
|
||
|
evcom_stream_close(&stream_);
|
||
|
}
|
||
16 years ago
|
|
||
15 years ago
|
void ForceClose() {
|
||
|
evcom_stream_force_close(&stream_);
|
||
16 years ago
|
}
|
||
15 years ago
|
|
||
|
void ReadPause() {
|
||
|
evcom_stream_read_pause(&stream_);
|
||
|
}
|
||
|
|
||
|
void ReadResume() {
|
||
|
evcom_stream_read_resume(&stream_);
|
||
|
}
|
||
|
|
||
|
void SetTimeout(float timeout) {
|
||
15 years ago
|
evcom_stream_reset_timeout(&stream_, timeout);
|
||
|
}
|
||
16 years ago
|
|
||
15 years ago
|
void SetNoDelay(bool no_delay) {
|
||
|
evcom_stream_set_no_delay(&stream_, no_delay);
|
||
|
}
|
||
|
|
||
15 years ago
|
virtual void OnConnect();
|
||
|
virtual void OnReceive(const void *buf, size_t len);
|
||
|
virtual void OnEOF();
|
||
|
virtual void OnClose();
|
||
|
virtual void OnTimeout();
|
||
16 years ago
|
|
||
15 years ago
|
v8::Local<v8::Object> GetProtocol();
|
||
16 years ago
|
|
||
15 years ago
|
enum evcom_stream_state ReadyState() {
|
||
15 years ago
|
return evcom_stream_state(&stream_);
|
||
|
}
|
||
|
|
||
16 years ago
|
enum encoding encoding_;
|
||
15 years ago
|
bool resolving_;
|
||
16 years ago
|
|
||
15 years ago
|
private:
|
||
16 years ago
|
|
||
16 years ago
|
/* liboi callbacks */
|
||
15 years ago
|
static void on_connect(evcom_stream *s) {
|
||
|
Connection *connection = static_cast<Connection*>(s->data);
|
||
16 years ago
|
connection->OnConnect();
|
||
|
}
|
||
|
|
||
15 years ago
|
static void on_read(evcom_stream *s, const void *buf, size_t len) {
|
||
|
Connection *connection = static_cast<Connection*>(s->data);
|
||
16 years ago
|
assert(connection->attached_);
|
||
16 years ago
|
if (len == 0)
|
||
|
connection->OnEOF();
|
||
|
else
|
||
|
connection->OnReceive(buf, len);
|
||
16 years ago
|
}
|
||
|
|
||
15 years ago
|
static void on_close(evcom_stream *s) {
|
||
|
Connection *connection = static_cast<Connection*>(s->data);
|
||
16 years ago
|
|
||
15 years ago
|
evcom_stream_detach(s);
|
||
|
|
||
15 years ago
|
assert(connection->stream_.recvfd < 0);
|
||
|
assert(connection->stream_.sendfd < 0);
|
||
16 years ago
|
|
||
15 years ago
|
connection->OnClose();
|
||
16 years ago
|
|
||
16 years ago
|
assert(connection->attached_);
|
||
16 years ago
|
|
||
16 years ago
|
connection->Detach();
|
||
16 years ago
|
}
|
||
|
|
||
15 years ago
|
static void on_timeout(evcom_stream *s) {
|
||
|
Connection *connection = static_cast<Connection*>(s->data);
|
||
16 years ago
|
connection->OnTimeout();
|
||
|
}
|
||
16 years ago
|
|
||
15 years ago
|
void Init(); // constructor helper.
|
||
16 years ago
|
|
||
15 years ago
|
static int Resolve(eio_req *req);
|
||
|
static int AfterResolve(eio_req *req);
|
||
16 years ago
|
char *host_;
|
||
|
char *port_;
|
||
15 years ago
|
evcom_stream stream_;
|
||
16 years ago
|
|
||
16 years ago
|
friend class Server;
|
||
16 years ago
|
};
|
||
16 years ago
|
|
||
16 years ago
|
class Server : public EventEmitter {
|
||
15 years ago
|
public:
|
||
|
static void Initialize(v8::Handle<v8::Object> target);
|
||
16 years ago
|
|
||
15 years ago
|
protected:
|
||
16 years ago
|
static v8::Persistent<v8::FunctionTemplate> constructor_template;
|
||
15 years ago
|
static v8::Handle<v8::Value> New(const v8::Arguments& args);
|
||
|
static v8::Handle<v8::Value> Listen(const v8::Arguments& args);
|
||
|
static v8::Handle<v8::Value> Close(const v8::Arguments& args);
|
||
16 years ago
|
|
||
15 years ago
|
Server() : EventEmitter() {
|
||
16 years ago
|
evcom_server_init(&server_);
|
||
16 years ago
|
server_.on_connection = Server::on_connection;
|
||
16 years ago
|
server_.on_close = Server::on_close;
|
||
16 years ago
|
server_.data = this;
|
||
|
}
|
||
16 years ago
|
|
||
15 years ago
|
virtual ~Server() {
|
||
15 years ago
|
assert(server_.fd >= 0);
|
||
16 years ago
|
}
|
||
16 years ago
|
|
||
15 years ago
|
int Listen(struct sockaddr *address, int backlog) {
|
||
|
int r = evcom_server_listen(&server_, address, backlog);
|
||
|
if (r != 0) return r;
|
||
|
evcom_server_attach(EV_DEFAULT_ &server_);
|
||
16 years ago
|
Attach();
|
||
16 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
15 years ago
|
void Close() {
|
||
|
evcom_server_close(&server_);
|
||
16 years ago
|
}
|
||
|
|
||
15 years ago
|
virtual v8::Handle<v8::FunctionTemplate> GetConnectionTemplate();
|
||
|
virtual Connection* UnwrapConnection(v8::Local<v8::Object> connection);
|
||
|
|
||
|
private:
|
||
|
Connection* OnConnection(struct sockaddr *addr);
|
||
16 years ago
|
|
||
15 years ago
|
static evcom_stream* on_connection(evcom_server *s, struct sockaddr *addr) {
|
||
|
Server *server = static_cast<Server*>(s->data);
|
||
|
Connection *connection = server->OnConnection(addr);
|
||
15 years ago
|
return &connection->stream_;
|
||
16 years ago
|
}
|
||
|
|
||
15 years ago
|
void OnClose(int errorno);
|
||
|
|
||
|
static void on_close(evcom_server *s) {
|
||
|
Server *server = static_cast<Server*>(s->data);
|
||
15 years ago
|
evcom_server_detach(s);
|
||
|
server->OnClose(s->errorno);
|
||
16 years ago
|
server->Detach();
|
||
|
}
|
||
|
|
||
16 years ago
|
evcom_server server_;
|
||
16 years ago
|
};
|
||
16 years ago
|
|
||
15 years ago
|
} // namespace node
|
||
|
#endif // SRC_NET_H_
|