Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 174 lines (134 sloc) 4.877 kb
63a9cd3 @ry everything is changed. i've waited much too long to commit.
ry authored
1 #ifndef node_net_h
2 #define node_net_h
3
cbd342a @ry Publicize Socket and Server for eventual cooperation with HttpServer.
ry authored
4 #include "node.h"
63a9cd3 @ry everything is changed. i've waited much too long to commit.
ry authored
5 #include <v8.h>
cbd342a @ry Publicize Socket and Server for eventual cooperation with HttpServer.
ry authored
6 #include <oi_socket.h>
63a9cd3 @ry everything is changed. i've waited much too long to commit.
ry authored
7
cf1c580 @ry Create a node namespace
ry authored
8 namespace node {
63a9cd3 @ry everything is changed. i've waited much too long to commit.
ry authored
9
09c2ae5 @ry Slight change in tcp connection constructor
ry authored
10 class Acceptor;
11
15d24d8 @ry Major refactor of network code
ry authored
12 class Connection : public ObjectWrap {
cbd342a @ry Publicize Socket and Server for eventual cooperation with HttpServer.
ry authored
13 public:
15d24d8 @ry Major refactor of network code
ry authored
14 static void Initialize (v8::Handle<v8::Object> target);
cbd342a @ry Publicize Socket and Server for eventual cooperation with HttpServer.
ry authored
15
baed9d5 @ry Inform V8 of external allocations.
ry authored
16 virtual size_t size (void) { return sizeof(Connection); };
17
15d24d8 @ry Major refactor of network code
ry authored
18 protected:
38726e7 @ry various clean ups; HTTPConnection (js side) inherits from TCPConnection
ry authored
19 /* v8 interface */
20 static v8::Persistent<v8::FunctionTemplate> constructor_template;
9a63d8e @ry Remove v8 prefix from function template callbacks.
ry authored
21 static v8::Handle<v8::Value> New (const v8::Arguments& args);
22 static v8::Handle<v8::Value> Connect (const v8::Arguments& args);
23 static v8::Handle<v8::Value> Send (const v8::Arguments& args);
24 static v8::Handle<v8::Value> SendUtf8 (const v8::Arguments& args);
25 static v8::Handle<v8::Value> Close (const v8::Arguments& args);
26 static v8::Handle<v8::Value> FullClose (const v8::Arguments& args);
27 static v8::Handle<v8::Value> ForceClose (const v8::Arguments& args);
1b54e3d @ry Change encoding setter/getter to setEncoding function.
ry authored
28 static v8::Handle<v8::Value> SetEncoding (const v8::Arguments& args);
69ab87c @ry Fix crash in oi_socket; add ready state reader to Connection objects.
ry authored
29
30 static v8::Handle<v8::Value> ReadyStateGetter (v8::Local<v8::String> _,
31 const v8::AccessorInfo& info);
cbd342a @ry Publicize Socket and Server for eventual cooperation with HttpServer.
ry authored
32
73fb24f @ry Relatively large update to TCP API. No more "protocol".
ry authored
33 Connection (v8::Handle<v8::Object> handle);
3045038 @ry update oi_socket - modify node code to match
ry authored
34 virtual ~Connection ();
38726e7 @ry various clean ups; HTTPConnection (js side) inherits from TCPConnection
ry authored
35
36 int Connect (struct addrinfo *address) { return oi_socket_connect (&socket_, address); }
3045038 @ry update oi_socket - modify node code to match
ry authored
37 void Send (oi_buf *buf) { oi_socket_write(&socket_, buf); }
38 void Close (void) { oi_socket_close(&socket_); }
39 void FullClose (void) { oi_socket_full_close(&socket_); }
40 void ForceClose (void) { oi_socket_force_close(&socket_); }
38726e7 @ry various clean ups; HTTPConnection (js side) inherits from TCPConnection
ry authored
41
42 void SetAcceptor (v8::Handle<v8::Object> acceptor_handle);
43
5a071ad @ry Begin refactor of http.cc. Remove libebb add http_parser.
ry authored
44 virtual void OnConnect (void);
45 virtual void OnReceive (const void *buf, size_t len);
46 virtual void OnDrain (void);
47 virtual void OnEOF (void);
48 virtual void OnDisconnect (void);
49 virtual void OnTimeout (void);
cbd342a @ry Publicize Socket and Server for eventual cooperation with HttpServer.
ry authored
50
15d24d8 @ry Major refactor of network code
ry authored
51 v8::Local<v8::Object> GetProtocol (void);
cbd342a @ry Publicize Socket and Server for eventual cooperation with HttpServer.
ry authored
52
38726e7 @ry various clean ups; HTTPConnection (js side) inherits from TCPConnection
ry authored
53 enum encoding encoding_;
54
310eed0 @ry Clean up readyState handling. Add test.
ry authored
55 enum readyState { OPEN, CLOSED, READ_ONLY, WRITE_ONLY };
56 enum readyState ReadyState ( )
57 {
58 if (socket_.got_full_close)
59 return CLOSED;
60
61 if (socket_.got_half_close)
62 return (socket_.read_action == NULL ? CLOSED : READ_ONLY);
63
64 if (socket_.read_action && socket_.write_action)
65 return OPEN;
66 else if (socket_.write_action)
67 return WRITE_ONLY;
68 else if (socket_.read_action)
69 return READ_ONLY;
70
71 return CLOSED;
72 }
73
38726e7 @ry various clean ups; HTTPConnection (js side) inherits from TCPConnection
ry authored
74 private:
75
1306283 @ry cleanup: rename some of the callbacks
ry authored
76 /* liboi callbacks */
77 static void on_connect (oi_socket *s) {
15d24d8 @ry Major refactor of network code
ry authored
78 Connection *connection = static_cast<Connection*> (s->data);
79 connection->OnConnect();
80 }
81
1306283 @ry cleanup: rename some of the callbacks
ry authored
82 static void on_read (oi_socket *s, const void *buf, size_t len) {
15d24d8 @ry Major refactor of network code
ry authored
83 Connection *connection = static_cast<Connection*> (s->data);
55d840e @ry Add profile window around socket on_read.
ry authored
84 v8::V8::ResumeProfiler();
1713386 @ry add Connection::SendEOF. modify test accordingly.
ry authored
85 if (len == 0)
86 connection->OnEOF();
87 else
88 connection->OnReceive(buf, len);
55d840e @ry Add profile window around socket on_read.
ry authored
89 v8::V8::PauseProfiler();
15d24d8 @ry Major refactor of network code
ry authored
90 }
91
1306283 @ry cleanup: rename some of the callbacks
ry authored
92 static void on_drain (oi_socket *s) {
15d24d8 @ry Major refactor of network code
ry authored
93 Connection *connection = static_cast<Connection*> (s->data);
94 connection->OnDrain();
95 }
96
1306283 @ry cleanup: rename some of the callbacks
ry authored
97 static void on_close (oi_socket *s) {
15d24d8 @ry Major refactor of network code
ry authored
98 Connection *connection = static_cast<Connection*> (s->data);
99 connection->OnDisconnect();
ae7c631 @ry Various fixes to make the web server not give off so many errors.
ry authored
100
4353109 @ry don't die on EPIPE
ry authored
101 /*
ae7c631 @ry Various fixes to make the web server not give off so many errors.
ry authored
102 if (s->errorno)
103 printf("socket died with error %d\n", s->errorno);
4353109 @ry don't die on EPIPE
ry authored
104 */
ae7c631 @ry Various fixes to make the web server not give off so many errors.
ry authored
105
cfd6162 @ry Reference counting. Network bugs.
ry authored
106 connection->Detach();
15d24d8 @ry Major refactor of network code
ry authored
107 }
108
1306283 @ry cleanup: rename some of the callbacks
ry authored
109 static void on_timeout (oi_socket *s) {
15d24d8 @ry Major refactor of network code
ry authored
110 Connection *connection = static_cast<Connection*> (s->data);
111 connection->OnTimeout();
112 }
cbd342a @ry Publicize Socket and Server for eventual cooperation with HttpServer.
ry authored
113
82e7736 @ry HTTP Client: add fix to allow TCP connection to reconnect.
ry authored
114 void Init (void); // constructor helper.
115
cbd342a @ry Publicize Socket and Server for eventual cooperation with HttpServer.
ry authored
116 static int Resolve (eio_req *req);
117 static int AfterResolve (eio_req *req);
118 char *host_;
119 char *port_;
15d24d8 @ry Major refactor of network code
ry authored
120 oi_socket socket_;
121
122 friend class Acceptor;
123 };
cbd342a @ry Publicize Socket and Server for eventual cooperation with HttpServer.
ry authored
124
15d24d8 @ry Major refactor of network code
ry authored
125 class Acceptor : public ObjectWrap {
126 public:
127 static void Initialize (v8::Handle<v8::Object> target);
128
baed9d5 @ry Inform V8 of external allocations.
ry authored
129 virtual size_t size (void) { return sizeof(Acceptor); };
130
38726e7 @ry various clean ups; HTTPConnection (js side) inherits from TCPConnection
ry authored
131 protected:
132 static v8::Persistent<v8::FunctionTemplate> constructor_template;
9a63d8e @ry Remove v8 prefix from function template callbacks.
ry authored
133 static v8::Handle<v8::Value> New (const v8::Arguments& args);
134 static v8::Handle<v8::Value> Listen (const v8::Arguments& args);
135 static v8::Handle<v8::Value> Close (const v8::Arguments& args);
38726e7 @ry various clean ups; HTTPConnection (js side) inherits from TCPConnection
ry authored
136
9c3770d @ry Implement HTTPServer (untested!)
ry authored
137 Acceptor (v8::Handle<v8::Object> handle,
73fb24f @ry Relatively large update to TCP API. No more "protocol".
ry authored
138 v8::Handle<v8::Function> connection_handler,
9c3770d @ry Implement HTTPServer (untested!)
ry authored
139 v8::Handle<v8::Object> options);
baed9d5 @ry Inform V8 of external allocations.
ry authored
140 virtual ~Acceptor () { Close(); }
15d24d8 @ry Major refactor of network code
ry authored
141
73fb24f @ry Relatively large update to TCP API. No more "protocol".
ry authored
142 v8::Local<v8::Function> GetConnectionHandler (void);
9c3770d @ry Implement HTTPServer (untested!)
ry authored
143
15d24d8 @ry Major refactor of network code
ry authored
144 int Listen (struct addrinfo *address) {
145 int r = oi_server_listen (&server_, address);
146 if(r != 0) return r;
147 oi_server_attach (EV_DEFAULT_ &server_);
cfd6162 @ry Reference counting. Network bugs.
ry authored
148 Attach();
15d24d8 @ry Major refactor of network code
ry authored
149 return 0;
150 }
151
152 void Close ( ) {
153 oi_server_close (&server_);
cfd6162 @ry Reference counting. Network bugs.
ry authored
154 Detach();
15d24d8 @ry Major refactor of network code
ry authored
155 }
156
38726e7 @ry various clean ups; HTTPConnection (js side) inherits from TCPConnection
ry authored
157 virtual Connection* OnConnection (struct sockaddr *addr, socklen_t len);
15d24d8 @ry Major refactor of network code
ry authored
158
159 private:
1306283 @ry cleanup: rename some of the callbacks
ry authored
160 static oi_socket* on_connection (oi_server *s, struct sockaddr *addr, socklen_t len) {
15d24d8 @ry Major refactor of network code
ry authored
161 Acceptor *acceptor = static_cast<Acceptor*> (s->data);
162 Connection *connection = acceptor->OnConnection (addr, len);
163 if (connection)
164 return &connection->socket_;
165 else
166 return NULL;
167 }
168
169 oi_server server_;
cbd342a @ry Publicize Socket and Server for eventual cooperation with HttpServer.
ry authored
170 };
cf1c580 @ry Create a node namespace
ry authored
171
172 } // namespace node
63a9cd3 @ry everything is changed. i've waited much too long to commit.
ry authored
173 #endif
Something went wrong with that request. Please try again.