Revision 822d7fa2 src/net.cc

View differences:

src/net.cc
29 29
/* ai_next       */ , NULL
30 30
                    };
31 31

  
32
class Server {
33
public:
34
  Server (Handle<Object> handle, int backlog);
35
  ~Server ();
36

  
37
  static Handle<Value> New (const Arguments& args);
38
  static Handle<Value> ListenTCP (const Arguments& args);
39
  static Handle<Value> Close (const Arguments& args);
40

  
41
private:
42
  static oi_socket* OnConnection (oi_server *, struct sockaddr *, socklen_t);
43
  static Server* Unwrap (Handle<Object> handle);
44
  static void MakeWeak (Persistent<Value> _, void *data);
45
  oi_server server_;
46
  Persistent<Object> handle_;
47
};
32 48

  
33 49
class Socket {
34 50
public:
35
  Socket (Handle<Object> obj, double timeout);
51
  Socket (Handle<Object> handle, double timeout);
36 52
  ~Socket ();
37 53

  
38
  void SetEncoding (enum encoding);
54
  void SetEncoding (Handle<Value>);
39 55
  void SetTimeout (double);
40 56

  
41 57
  static Handle<Value> New (const Arguments& args);
42 58
  static Handle<Value> Write (const Arguments& args);
43 59
  static Handle<Value> Close (const Arguments& args);
44 60
  static Handle<Value> ConnectTCP (const Arguments& args);
61
  static Handle<Value> SetEncoding (const Arguments& args);
45 62

  
63
private:
46 64
  static void OnConnect (oi_socket *socket);
47 65
  static void OnRead (oi_socket *s, const void *buf, size_t count);
48 66
  static void OnDrain (oi_socket *s);
......
50 68
  static void OnClose (oi_socket *s);
51 69
  static void OnTimeout (oi_socket *s);
52 70

  
53
  char *host_;
54
  char *port_;
55

  
56
private:
57 71
  static int Resolve (eio_req *req);
58 72
  static int AfterResolve (eio_req *req);
59 73

  
......
63 77
  enum {UTF8, RAW} encoding_;
64 78
  oi_socket socket_;
65 79
  Persistent<Object> handle_;
80

  
81
  char *host_;
82
  char *port_;
66 83
};
67 84

  
85
Server::Server (Handle<Object> handle, int backlog)
86
{
87
  oi_server_init(&server_, backlog);
88
  server_.on_connection = Server::OnConnection;
89
//  server_.on_error      = Server::OnError;
90
  server_.data = this;
91

  
92
  HandleScope scope;
93
  handle_ = Persistent<Object>::New(handle);
94
  handle_->SetInternalField(0, External::New(this));
95
  handle_.MakeWeak(this, Server::MakeWeak);
96
}
97

  
98
Server::~Server ()
99
{
100
  oi_server_close(&server_);
101
  oi_server_detach(&server_);
102
  handle_.Dispose();
103
  handle_.Clear(); // necessary? 
104
}
105

  
106
Handle<Value>
107
Server::New (const Arguments& args)
108
{
109
  ;
110
}
111

  
112
Handle<Value>
113
Server::ListenTCP (const Arguments& args)
114
{
115
  ;
116
}
117

  
118
Handle<Value>
119
Server::Close (const Arguments& args)
120
{
121
  ;
122
}
123

  
124
oi_socket*
125
Server::OnConnection (oi_server *, struct sockaddr *remote_addr, socklen_t remote_addr_len)
126
{
127
  ; 
128
}
129

  
130
Server*
131
Server::Unwrap (Handle<Object> handle)
132
{
133
  HandleScope scope;
134
  Handle<External> field = Handle<External>::Cast(handle->GetInternalField(0));
135
  Server* server = static_cast<Server*>(field->Value());
136
  return server;
137
}
138

  
139
void
140
Server::MakeWeak (Persistent<Value> _, void *data)
141
{
142
  Server *s = static_cast<Server*> (data);
143
  delete s;
144
}
145

  
68 146
Handle<Value>
69 147
Socket::New(const Arguments& args)
70 148
{
......
105 183
  return args.This();
106 184
}
107 185

  
186
void
187
Socket::SetEncoding (Handle<Value> encoding_value)
188
{
189
  if (encoding_value->IsString()) {
190
    HandleScope scope;
191
    Local<String> encoding_string = encoding_value->ToString();
192
    char buf[5]; // need enough room for "utf8" or "raw"
193
    encoding_string->WriteAscii(buf, 0, 4);
194
    buf[4] = '\0';
195
    if(strcasecmp(buf, "utf8") == 0)
196
      encoding_ = UTF8;
197
    else
198
      encoding_ = RAW;
199
  }
200
}
201

  
108 202
Socket*
109 203
Socket::Unwrap (Handle<Object> handle) 
110 204
{
......
232 326

  
233 327
  HandleScope scope;
234 328
  handle_ = Persistent<Object>::New(handle);
235
  handle_->SetInternalField (0, External::New(this));
236
  handle_.MakeWeak (this, Socket::MakeWeak);
329
  handle_->SetInternalField(0, External::New(this));
330
  handle_.MakeWeak(this, Socket::MakeWeak);
237 331

  
238
  encoding_ = UTF8;
332
  encoding_ = UTF8; // default encoding.
239 333
  host_ = NULL;
240 334
  port_ = NULL;
241 335
}
......
251 345
}
252 346

  
253 347
Handle<Value>
348
Socket::SetEncoding (const Arguments& args) 
349
{
350
  Socket *socket = Socket::Unwrap(args.Holder());
351
  socket->SetEncoding(args[0]);
352
  return Undefined();
353
}
354

  
355
Handle<Value>
254 356
Socket::Write (const Arguments& args) 
255 357
{
256 358
  HandleScope scope;
......
410 512
  Socket *socket = static_cast<Socket*> (s->data);
411 513
  HandleScope scope;
412 514

  
413
  printf("timeout\n");
414

  
415 515
  Handle<Value> ontimeout_value = socket->handle_->Get( String::NewSymbol("onTimeout") );
416 516
  if (!ontimeout_value->IsFunction()) return; 
417 517
  Handle<Function> ontimeout = Handle<Function>::Cast(ontimeout_value);
......
438 538
  //NODE_SET_METHOD(socket_template->InstanceTemplate(), "connectUNIX", Socket::ConnectUNIX);
439 539
  NODE_SET_METHOD(socket_template->InstanceTemplate(), "write", Socket::Write);
440 540
  NODE_SET_METHOD(socket_template->InstanceTemplate(), "close", Socket::Close);
541
  NODE_SET_METHOD(socket_template->InstanceTemplate(), "setEncoding", Socket::SetEncoding);
542

  
543
  Local<FunctionTemplate> server_template = FunctionTemplate::New(Server::New);
544
  server_template->InstanceTemplate()->SetInternalFieldCount(1);
545
  target->Set(String::NewSymbol("Server"), server_template->GetFunction());
441 546

  
547
  NODE_SET_METHOD(server_template->InstanceTemplate(), "listenTCP", Server::ListenTCP);
442 548
}
443 549

  

Also available in: Unified diff