[Jslib] new socket.js

Stephen Knight stknight at ultralifebatteries.com
Tue Sep 30 15:52:49 EDT 2003

Greetings all (however many "all" might be):

I've successfully updated socket.js (thanks to those that helped) to handle synchronous
communication, so I figure it's time to submit the changes back.

I've provided some information on what I've done below.

Unless somebody objects, I guess I'll be working out with Pete how to commit this new version.

thank you for your time.

I Issues:
As usual, there are some issues:
1) the interfaces are not the same as the original.  Since the original didn't work (therefore,
nobody was using it), I don't think that will be much of a problem.  However, the new interfaces are
  more tuned to where I was going (which might not be acceptable to everybody...anybody?).  More on
this later.

2) My changes do not currently support asynchronous communication.  Not that I have anything against
it, but my application doesn't do that so it's difficult to develop/test against.  Suggestions are

II Socket interfaces:
Socket( [class_id] )		n/a		: constructor
						: class_id(String) => class identifier (optional)

By default, Socket uses the result of Component.classes[
"@mozilla.org/network/socket-transport-service;1" ] for creating a socket service.

If the argument "class_id" is given then the results of Component.classesByID[ class_id ] is used
for creating a socket service.

open( host, port, [binary] )	true		: opens socket
						: host(String) => tcp/ip address
						: port(Number) => socket port
						: binary(Boolean) => if true then binary comm (optional)

By default, open(...) creates a scriptable input-stream.
If the argument "binary" is 'false' then a binary input-stream will be used.

close()				nothing		: closes socket

write( String )			true on success	: writes data to socket
						: String => data to write out
Failure on write will cause an exception to be thrown (actually passing along the exception thrown
by the socket call).  If an exception is thrown than the Socket will no longer believe it is
connected (altho, close() should probably still be invoked).

read( Number )			String		: reads data from socket
						: Number => number of bytes to read
Attempts to read more bytes than exists on the port are trimmed to the actual bytes available.
Other failures will cause an exception to be thrown (passed along).  If an exception is thrown than
the Socket will no longer believe it is connected (altho, close() should probably still be invoked).

available()			Number		: bytes waiting on socket

If the connection was not actually made (open() really failed) or was lost then an exception will be

III Attributes

All of these attributes are used by open(), so they should be tweaked for your application somewhere
between "foo = new Socket" and "foo.open()"

1)  nsISocketTransport.openInputStream arguments:
	openInputFlags = 0;                  open flags
	openInputSegmentSize = 0;            segmentSize
	openInputSegmentCount = 0;           segmentCount

2)  nsISocketTransport.openOutputStream arguments:	
	openOutputFlags = 0;                 open flags	
	openOutputSegmentSize = 0;           segmentSize
	openOutputSegmentCount = 0;          segmentCount

Stephen Knight
Ultralife Batteries, Inc.

More information about the Jslib mailing list