Browse Source

Change encoding setter/getter to setEncoding function.

v0.7.4-release
Ryan 16 years ago
parent
commit
1b54e3d87d
  1. 68
      src/net.cc
  2. 6
      src/net.h
  3. 4
      test/test-pingpong.js
  4. 2
      test/test-reconnecting-socket.js

68
src/net.cc

@ -66,11 +66,7 @@ Connection::Initialize (v8::Handle<v8::Object> target)
NODE_SET_PROTOTYPE_METHOD(constructor_template, "close", Close); NODE_SET_PROTOTYPE_METHOD(constructor_template, "close", Close);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "fullClose", FullClose); NODE_SET_PROTOTYPE_METHOD(constructor_template, "fullClose", FullClose);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "forceClose", ForceClose); NODE_SET_PROTOTYPE_METHOD(constructor_template, "forceClose", ForceClose);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "setEncoding", SetEncoding);
constructor_template->PrototypeTemplate()->SetAccessor(
ENCODING_SYMBOL,
EncodingGetter,
EncodingSetter);
constructor_template->PrototypeTemplate()->SetAccessor( constructor_template->PrototypeTemplate()->SetAccessor(
READY_STATE_SYMBOL, READY_STATE_SYMBOL,
@ -98,41 +94,6 @@ Connection::ReadyStateGetter (Local<String> _, const AccessorInfo& info)
return ThrowException(String::New("This shouldn't happen.")); return ThrowException(String::New("This shouldn't happen."));
} }
Handle<Value>
Connection::EncodingGetter (Local<String> _, const AccessorInfo& info)
{
Connection *connection = NODE_UNWRAP(Connection, info.This());
if (!connection) return Handle<Value>();
HandleScope scope;
if (connection->encoding_ == UTF8)
return scope.Close(UTF8_SYMBOL);
else
return scope.Close(RAW_SYMBOL);
}
void
Connection::EncodingSetter (Local<String> _, Local<Value> value, const AccessorInfo& info)
{
Connection *connection = NODE_UNWRAP(Connection, info.This());
if (!connection) return;
if (!value->IsString()) {
connection->encoding_ = RAW;
return;
}
HandleScope scope;
Local<String> encoding = value->ToString();
char buf[5]; // need enough room for "utf8" or "raw"
encoding->WriteAscii(buf, 0, 4);
buf[4] = '\0';
if(strcasecmp(buf, "utf8") == 0)
connection->encoding_ = UTF8;
else
connection->encoding_ = RAW;
}
Connection::Connection (Handle<Object> handle) Connection::Connection (Handle<Object> handle)
: ObjectWrap(handle) : ObjectWrap(handle)
{ {
@ -234,6 +195,33 @@ Connection::Connect (const Arguments& args)
return Undefined(); return Undefined();
} }
Handle<Value>
Connection::SetEncoding (const Arguments& args)
{
HandleScope scope;
Connection *connection = NODE_UNWRAP(Connection, args.This());
if (!connection) return Handle<Value>();
if (!args[0]->IsString()) {
connection->encoding_ = RAW;
return scope.Close(RAW_SYMBOL);
}
Local<String> encoding = args[0]->ToString();
char buf[5]; // need enough room for "utf8" or "raw"
encoding->WriteAscii(buf, 0, 4);
buf[4] = '\0';
if(strcasecmp(buf, "utf8") == 0) {
connection->encoding_ = UTF8;
return scope.Close(UTF8_SYMBOL);
} else {
connection->encoding_ = RAW;
return scope.Close(RAW_SYMBOL);
}
}
int int
Connection::Resolve (eio_req *req) Connection::Resolve (eio_req *req)
{ {

6
src/net.h

@ -25,11 +25,7 @@ protected:
static v8::Handle<v8::Value> Close (const v8::Arguments& args); static v8::Handle<v8::Value> Close (const v8::Arguments& args);
static v8::Handle<v8::Value> FullClose (const v8::Arguments& args); static v8::Handle<v8::Value> FullClose (const v8::Arguments& args);
static v8::Handle<v8::Value> ForceClose (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> EncodingGetter (v8::Local<v8::String> _,
const v8::AccessorInfo& info);
static void EncodingSetter (v8::Local<v8::String> _,
v8::Local<v8::Value> value, const v8::AccessorInfo& info);
static v8::Handle<v8::Value> ReadyStateGetter (v8::Local<v8::String> _, static v8::Handle<v8::Value> ReadyStateGetter (v8::Local<v8::String> _,
const v8::AccessorInfo& info); const v8::AccessorInfo& info);

4
test/test-pingpong.js

@ -5,7 +5,7 @@ var N = 1000;
var count = 0; var count = 0;
function Ponger (socket) { function Ponger (socket) {
socket.encoding = "UTF8"; socket.setEncoding("utf8");
socket.timeout = 0; socket.timeout = 0;
puts("got socket."); puts("got socket.");
@ -40,7 +40,7 @@ function onLoad() {
var client = new node.tcp.Connection(); var client = new node.tcp.Connection();
assertEquals("closed", client.readyState); assertEquals("closed", client.readyState);
client.encoding = "UTF8"; client.setEncoding("utf8");
client.onConnect = function () { client.onConnect = function () {
assertEquals("open", client.readyState); assertEquals("open", client.readyState);

2
test/test-reconnecting-socket.js

@ -21,7 +21,7 @@ function onLoad () {
var count = 0; var count = 0;
var client = new node.tcp.Connection(); var client = new node.tcp.Connection();
client.encoding = "UTF8"; client.setEncoding("UTF8");
client.onConnect = function () { client.onConnect = function () {
puts("client connected"); puts("client connected");
}; };

Loading…
Cancel
Save