On 06/11/2010 08:29 PM, Paolo Bonzini wrote:

> 
> Hard to say without seeing the patch. :-)

This is Work In Progress so there is no ChangeLog entry.

The change in Sockets.st is to make:

    Sockets.DatagramSocket local: '0.0.0.0' port: 23000

Somehow using '23.0.0.0' will not throw an exception, and the bind fails
but no exception is thrown as well (it bound to 0.0.0.0:0). There is at
least another bug, I should create the localAddr first and then try to
derive the addressClass from that or create a local bind.



> 
>> 2.) I am using the DatagramSocket>>next selector to receive the
>> datagram, I had to increase the default buffersize... but now all
>> (Datagram>>data)>>size return the size. I looked down into
>> AbstrackSocketImpl.st and the receive does not check the return value of
>> TCPrecvfrom at all. I have added a dataSize to Datagram and set the
>> return value there...

The second part is to introduce two new selectors to Datagram. One is
#dataSize the other is #dataSize, the Socket Impl. will set the return
value of the TCPrecvfrom as dataSize. This way a caller can find out how
big a datagram was, on the sending side I only want to send as much data
as I have received, so I introduced a #payloadSize which decides how
much of data to send. If dataSize is not set (or reset) it will fallback
to data size.


so how does that look?
diff --git a/packages/sockets/AbstractSocketImpl.st b/packages/sockets/AbstractSocketImpl.st
index a71410e..096d0f2 100644
--- a/packages/sockets/AbstractSocketImpl.st
+++ b/packages/sockets/AbstractSocketImpl.st
@@ -647,12 +647,12 @@ implementations.'>
 	 to only peek for it without removing it from the queue."
 
 	<category: 'socket operations'>
-	| address port data from addrLen fd |
+	| address port data from addrLen fd read |
 	data := ByteArray new: self bufferSize.
 	from := ByteArray new: 128.
 	addrLen := CInt gcValue: 128.
 	(fd := self fd) isNil ifTrue: [ ^SystemExceptions.EndOfStream signal ].
-	self 
+	read := self 
 	    receive: fd
 	    buffer: data
 	    size: data size
@@ -662,6 +662,7 @@ implementations.'>
 	port := ValueHolder new.
 	^aDatagram
 	    data: data;
+	    dataSize: read;
 	    address: (SocketAddress fromSockAddr: from port: port);
 	    port: port value;
 	    yourself
@@ -681,7 +682,7 @@ implementations.'>
 	self 
 	    send: fd
 	    buffer: aDatagram data
-	    size: aDatagram data size
+	    size: aDatagram payloadSize
 	    flags: self flags
 	    to: receiver
 	    size: size
diff --git a/packages/sockets/Datagram.st b/packages/sockets/Datagram.st
index 0793601..0e81ba3 100644
--- a/packages/sockets/Datagram.st
+++ b/packages/sockets/Datagram.st
@@ -32,7 +32,7 @@
 
 
 Object subclass: Datagram [
-    | data address port |
+    | data address port dataSize |
     
     <category: 'Sockets-Protocols'>
     <comment: '
@@ -125,9 +125,33 @@ This class can also be used for receiving data from the network.'>
 	"Set the data attached to the datagram"
 
 	<category: 'accessing'>
-	data := aByteArray
+	data := aByteArray.
+	dataSize := nil.
     ]
 
+    dataSize [
+	<comment: 'Answer the size of the message.'>
+	<category: 'accessing'>
+
+	^dataSize
+    ]
+
+    dataSize: aSize [
+	<comment: 'I am called to update the size...'>
+	<category: 'accessing'>
+
+	dataSize := aSize		
+    ]
+
+    payloadSize [
+	<comment: 'I determine the payload size.'>
+        <category: 'accessing'>
+        dataSize isNil
+	    ifTrue: [^data size]
+	    ifFalse: [^dataSize]
+    ]
+
+
     get [
 	"Parse the data attached to the datagram through a newly created
 	 ObjectDumper, and answer the resulting object.  This method is
diff --git a/packages/sockets/Sockets.st b/packages/sockets/Sockets.st
index a279c42..a1033ee 100644
--- a/packages/sockets/Sockets.st
+++ b/packages/sockets/Sockets.st
@@ -631,6 +631,20 @@ but it is done for cleanliness and symmetry.'>
 	    port: remotePort
     ]
 
+    DatagramSocket class >> addressFromString: ipAddressOrString [
+	| addr |
+
+	ipAddressOrString isString 
+	    ifTrue: [
+		addr := SocketAddress byName: ipAddressOrString.
+		addr isNil 
+		    ifTrue: 
+			[self error: 'cannot resolve host name ' , ipAddressOrString printString]]
+	    ifFalse: [addr := ipAddressOrString].
+
+	^ addr
+    ]
+
     DatagramSocket class >> remote: ipAddressOrString port: remotePort local: ipAddress port: localPort [
 	"Create a new socket and bind it to the given host (passed as a
 	 String to be resolved or as a SocketAddress), and to the given remotePort.
@@ -639,22 +653,16 @@ but it is done for cleanliness and symmetry.'>
 
 	<category: 'instance creation'>
 	| localAddr remoteAddr addressClass |
-	ipAddressOrString isString 
-	    ifTrue: [
-		remoteAddr := SocketAddress byName: ipAddressOrString.
-		remoteAddr isNil 
-		    ifTrue: 
-			[self error: 'cannot resolve host name ' , ipAddressOrString printString]]
-	    ifFalse: [remoteAddr := ipAddressOrString].
+	remoteAddr := self addressFromString: ipAddressOrString.
 	addressClass := remoteAddr isNil 
 		    ifTrue: [self defaultAddressClass]
 		    ifFalse: [remoteAddr class].
-	addressClass := ipAddress isNil 
+	addressClass := (ipAddress isNil or: [ipAddress isString])
 		    ifTrue: [addressClass]
 		    ifFalse: [ipAddress class].
-	localAddr := ipAddress isNil 
-		    ifTrue: [addressClass anyLocalAddress]
-		    ifFalse: [ipAddress].
+
+	localAddr := self addressFromString: ipAddress.
+	localAddr ifNil: [localAddr := addressClass anyLocalAddress].
 	^(addressClass newSocket: self)
 	    remote: remoteAddr
 	    port: remotePort
_______________________________________________
help-smalltalk mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-smalltalk

Reply via email to