Sfoglia il codice sorgente

More socket+stream fixes.

Mark Sibly 8 anni fa
parent
commit
9ebc339454

+ 11 - 17
modules/std/socket/native/socket.cpp

@@ -30,14 +30,11 @@ namespace bbSocket{
 		
 		int fiber;
 		int result=-1;
-		int waiting=0;
 				
 		Future():fiber( bbFiber::getCurrentFiber() ){}
 				
 		void dispatch(){
 		
-			if( !waiting ) {printf( "NOT WAITING!\n" );fflush( stdout );}
-				
 			bbFiber::resumeFiber( fiber );
 		}
 		
@@ -50,7 +47,6 @@ namespace bbSocket{
 		
 		int get(){
 		
-			waiting=1;
 			bbFiber::suspendCurrentFiber();
 			
 			return result;
@@ -65,14 +61,15 @@ namespace bbSocket{
 #endif
 	}
 	
-	void init(){
+	int init(){
 		static bool done;
-		if( done ) return;
+		if( done ) return 1;
 		done=true;
 #if _WIN32	
 		WSADATA wsa;
 		WSAStartup( MAKEWORD(2,2),&wsa );
 #endif
+		return 1;
 	}
 
 	void dontBlock( int sock ){
@@ -97,7 +94,7 @@ namespace bbSocket{
 		hints.ai_family=AF_UNSPEC;
 		hints.ai_socktype=(type==1) ? SOCK_DGRAM : SOCK_STREAM;
 		hints.ai_protocol=(type==1) ? IPPROTO_UDP : IPPROTO_TCP;
-		if( (flags&1)==1 ) hints.ai_flags=AI_PASSIVE;
+		if( (flags&1)==1 ) hints.ai_flags|=AI_PASSIVE;
 		if( (flags&6)==2 ) hints.ai_family=AF_INET;
 		if( (flags&6)==4 ) hints.ai_family=AF_INET6;
 		if( hostname && !hostname[0] ) hostname=0;
@@ -137,7 +134,7 @@ namespace bbSocket{
 		hints.ai_family=AF_UNSPEC;
 		hints.ai_socktype=(type==1) ? SOCK_DGRAM : SOCK_STREAM;
 		hints.ai_protocol=(type==1) ? IPPROTO_UDP : IPPROTO_TCP;
-		if( (flags&1)==1 ) hints.ai_flags=AI_PASSIVE;
+		if( (flags&1)==1 ) hints.ai_flags|=AI_PASSIVE;
 		if( (flags&6)==2 ) hints.ai_family=AF_INET;
 		if( (flags&6)==4 ) hints.ai_family=AF_INET6;
 		if( hostname && !hostname[0] ) hostname=0;
@@ -151,6 +148,7 @@ namespace bbSocket{
 		
 			sock=::socket( res->ai_family,res->ai_socktype,res->ai_protocol );
 			if( sock==-1 ) continue;
+			
 #if _WIN32		
 			if( res->ai_family==AF_INET6 ){
 				int value=0,sz=sizeof( value );	
@@ -180,8 +178,6 @@ namespace bbSocket{
 	
 	int connect( const char *hostname,const char *service,int type,int flags ){
 	
-		init();
-
 		int sock=-1;
 		
 		if( bbFiber::getCurrentFiber() ){
@@ -208,8 +204,6 @@ namespace bbSocket{
 	
 	int bind( const char *hostname,const char *service,int flags ){
 	
-		init();
-	
 		int sock=-1;
 		
 		if( bbFiber::getCurrentFiber() ){
@@ -236,8 +230,6 @@ namespace bbSocket{
 	
 	int listen( const char *hostname,const char *service,int backlog,int flags ){
 	
-		init();
-	
 		int sock=-1;
 		
 		if( bbFiber::getCurrentFiber() ){
@@ -341,7 +333,7 @@ namespace bbSocket{
 				future.set( ::send( socket,(const char*)data,size,0 ) );
 					
 			} );
-				
+
 			n=future.get();
 				
 			thread.join();
@@ -391,7 +383,7 @@ namespace bbSocket{
 	int recv( int socket,void *data,int size ){
 	
 		int n=-1;
-	
+		
 		if( bbFiber::getCurrentFiber() && canrecv( socket )==0 ){//<size ){
 		
 			Future future;
@@ -500,7 +492,9 @@ namespace bbSocket{
 	
 	int sockaddrname( const void *addr,int addrlen,char *host,char *service ){
 	
-		return getnameinfo( (const sockaddr*)addr,addrlen,host,1023,service,79,0 );
+		int flags=0;//NI_NUMERICHOST|NI_NUMERICSERV;
+	
+		return getnameinfo( (const sockaddr*)addr,addrlen,host,1023,service,79,flags );
 	}
 	
 	int select( int n_read,int *r_socks,int n_write,int *w_socks,int n_except,int *e_socks,int millis ){

+ 2 - 0
modules/std/socket/native/socket.h

@@ -6,6 +6,8 @@
 
 namespace bbSocket{
 
+	int init();
+
 	int connect( const char *hostname,const char *service,int type,int flags );
 	
 	int bind( const char *hostname,const char *service,int flags );

+ 9 - 1
modules/std/socket/socket.monkey2

@@ -10,6 +10,10 @@ Namespace std.socket
 
 Extern private
 
+#rem monkeydoc @hidden
+#end
+Function socket_init:int()="bbSocket::init"
+
 #rem monkeydoc @hidden
 #end
 Function socket_connect:Int( hostname:CString,service:CString,type:Int,flags:int )="bbSocket::connect"
@@ -77,7 +81,11 @@ Function socket_sockaddrname:Int( addr:Void Ptr,addrlen:Int,host:libc.char_t Ptr
 #rem monkeydoc @hidden
 #end
 Function socket_select:Int( n_read:Int,r_socks:Int ptr,n_write:Int,w_socks:Int Ptr,n_except:Int,e_socks:Int Ptr,millis:Int )="bbSocket::select"
-	
+
+Private
+
+Global _init:=socket_init()
+
 Public
 
 #rem monkeydoc The SocketType enum.

+ 8 - 12
modules/std/socket/socketstream.monkey2

@@ -74,7 +74,7 @@ Class SocketStream Extends std.stream.Stream
 
 	#end
 	Method Read:Int( buf:Void Ptr,count:Int ) Override
-	
+		
 		Return _socket.Receive( buf,count )
 	End
 	
@@ -94,21 +94,16 @@ Class SocketStream Extends std.stream.Stream
 
 	#end
 	Method Write:Int( buf:Void Ptr,count:Int ) Override
-	
-		Local sent:=0
 		
-		While count>0
+		Local pos:=0
 		
-			Local n:=_socket.Send( buf,count )
-			If n<0 Exit
-			
-			buf=Cast<Byte Ptr>( buf )+n
-			count-=n
-			sent+=n
-
+		While pos<count
+			Local n:=_socket.Send( Cast<UByte Ptr>( buf )+pos,count-pos )
+			If n<=0 Exit
+			pos+=n
 		Wend
 		
-		Return sent
+		Return pos
 	End
 
 	Private
@@ -116,3 +111,4 @@ Class SocketStream Extends std.stream.Stream
 	Field _socket:Socket
 
 End
+

+ 95 - 62
modules/std/stream/stream.monkey2

@@ -96,11 +96,11 @@ Class Stream Extends std.resource.Resource
 	#end
 	Property ByteOrder:ByteOrder()
 		
-		Return _tmpbuf.ByteOrder
+		Return _swap ? ByteOrder.BigEndian Else ByteOrder.LittleEndian
 		
 	Setter( byteOrder:ByteOrder )
 		
-		_tmpbuf=(byteOrder=ByteOrder.BigEndian ? _BEbuf Else _LEbuf)
+		_swap=(byteOrder=ByteOrder.BigEndian)
 	End
 	
 	#rem monkeydoc Reads as many bytes as possible from a stream into memory.
@@ -121,7 +121,7 @@ Class Stream Extends std.resource.Resource
 		Local pos:=0
 		
 		While pos<count
-			Local n:=Read( Cast<Byte Ptr>( buf )+pos,count-pos )
+			Local n:=Read( Cast<UByte Ptr>( buf )+pos,count-pos )
 			If n<=0 Exit
 			pos+=n
 		Wend
@@ -138,7 +138,7 @@ Class Stream Extends std.resource.Resource
 	
 		Local data:=New DataBuffer( count )
 		Local n:=ReadAll( data,0,count )
-		If n>=count Return data
+		If n=count Return data
 		Local tmp:=data.Slice( 0,n )
 		data.Discard()
 		Return tmp
@@ -168,7 +168,7 @@ Class Stream Extends std.resource.Resource
 		buf.Discard()
 		Return data
 	End
-	
+
 	#rem monkeydoc Reads data from the stream and throws it away.
 
 	@param count The number of bytes to skip.
@@ -199,9 +199,9 @@ Class Stream Extends std.resource.Resource
 	#end
 	Method ReadByte:Byte()
 		
-		If Read( _tmpbuf.Data,1 )=1 Return _tmpbuf.PeekByte( 0 )
-		
-		Return 0
+		Local n:Byte
+		Read( Varptr n,1 )
+		Return n
 	End
 	
 	#rem monkeydoc Reads an unsigned byte from the stream.
@@ -211,9 +211,9 @@ Class Stream Extends std.resource.Resource
 	#end
 	Method ReadUByte:UByte()
 		
-		If Read( _tmpbuf.Data,1 )=1 Return _tmpbuf.PeekUByte( 0 )
-		
-		Return 0
+		Local n:UByte
+		Read( Varptr n,1 )
+		Return n
 	End
 	
 	#rem monkeydoc Reads a 16 bit short from the stream.
@@ -222,10 +222,11 @@ Class Stream Extends std.resource.Resource
 	
 	#end
 	Method ReadShort:Short()
-		
-		If ReadAll( _tmpbuf.Data,2 )=2 Return _tmpbuf.PeekShort( 0 )
-		
-		Return 0
+
+		Local n:Short
+		If Read( Varptr n,2 )<>2 n=0
+		If _swap Swap2( Varptr n )
+		Return n
 	End
 	
 	#rem monkeydoc Reads a 16 bit unsigned short from the stream.
@@ -234,10 +235,11 @@ Class Stream Extends std.resource.Resource
 	
 	#end
 	Method ReadUShort:UShort()
-		
-		If ReadAll( _tmpbuf.Data,2 )=2 Return _tmpbuf.PeekUShort( 0 )
-		
-		Return 0
+
+		Local n:UShort
+		If Read( Varptr n,2 )<>2 n=0
+		If _swap Swap2( Varptr n )
+		Return n
 	End
 	
 	#rem monkeydoc Reads a 32 bit int from the stream.
@@ -247,9 +249,10 @@ Class Stream Extends std.resource.Resource
 	#end
 	Method ReadInt:Int()
 		
-		If ReadAll( _tmpbuf.Data,4 )=4 Return _tmpbuf.PeekInt( 0 )
-		
-		Return 0
+		Local n:Int
+		If Read( Varptr n,4 )<>4 n=0
+		If _swap Swap4( Varptr n )
+		Return n
 	End
 	
 	#rem monkeydoc Reads a 32 bit unsigned int from the stream.
@@ -259,9 +262,10 @@ Class Stream Extends std.resource.Resource
 	#end
 	Method ReadUInt:UInt()
 		
-		If ReadAll( _tmpbuf.Data,4 )=4 Return _tmpbuf.PeekUInt( 0 )
-		
-		Return 0
+		Local n:UInt
+		If Read( Varptr n,4 )<>4 n=0
+		If _swap Swap4( Varptr n )
+		Return n
 	End
 	
 	#rem monkeydoc Reads a 32 bit long from the stream.
@@ -271,9 +275,10 @@ Class Stream Extends std.resource.Resource
 	#end
 	Method ReadLong:Long()
 		
-		If ReadAll( _tmpbuf.Data,8 )=8 Return _tmpbuf.PeekLong( 0 )
-
-		Return 0
+		Local n:Long
+		If Read( Varptr n,8 )<>8 n=0
+		If _swap Swap8( Varptr n )
+		Return n
 	End
 	
 	#rem monkeydoc Reads a 32 bit unsigned long from the stream.
@@ -283,9 +288,10 @@ Class Stream Extends std.resource.Resource
 	#end
 	Method ReadULong:ULong()
 		
-		If ReadAll( _tmpbuf.Data,8 )=8 Return _tmpbuf.PeekULong( 0 )
-
-		Return 0
+		Local n:ULong
+		If Read( Varptr n,8 )<>8 n=0
+		If _swap Swap8( Varptr n )
+		Return n
 	End
 	
 	#rem monkeydoc Reads a 32 bit float from the stream.
@@ -294,10 +300,11 @@ Class Stream Extends std.resource.Resource
 	
 	#end
 	Method ReadFloat:Float()
-		
-		If ReadAll( _tmpbuf.Data,4 )=4 Return _tmpbuf.PeekFloat( 0 )
 
-		Return 0
+		Local n:Float
+		If Read( Varptr n,4 )<>4 n=0
+		If _swap Swap4( Varptr n )
+		Return n
 	End
 	
 	#rem monkeydoc Reads a 64 bit double from the stream.
@@ -307,9 +314,10 @@ Class Stream Extends std.resource.Resource
 	#end
 	Method ReadDouble:Double()
 		
-		If ReadAll( _tmpbuf.Data,8 )=8 Return _tmpbuf.PeekDouble( 0 )
-
-		Return 0
+		Local n:Double
+		If Read( Varptr n,8 )<>8 n=0
+		If _swap Swap8( Varptr n )
+		Return n
 	End
 	
 	#rem monkeydoc Reads the entire stream into a string.
@@ -329,7 +337,7 @@ Class Stream Extends std.resource.Resource
 	
 	#end
 	Method ReadSizedString:String()
-		Local n:=ReadInt()
+		Local n:=ReadInt() 
 		Local data:=ReadAll( n )
 		Local str:=data.PeekString( 0 )
 		data.Discard()
@@ -377,8 +385,8 @@ Class Stream Extends std.resource.Resource
 	
 	#end
 	Method WriteByte( data:Byte )
-		_tmpbuf.PokeByte( 0,data )
-		Write( _tmpbuf.Data,1 )
+		
+		Write( Varptr data,1 )
 	End
 	
 	#rem monkeydoc Write an unsigned byte to the stream.
@@ -387,8 +395,8 @@ Class Stream Extends std.resource.Resource
 
 	#end
 	Method WriteUByte( data:UByte )
-		_tmpbuf.PokeUByte( 0,data )
-		Write( _tmpbuf.Data,1 )
+		
+		Write( Varptr data,1 )
 	End
 	
 	#rem monkeydoc Writes a 16 bit short to the stream.
@@ -397,8 +405,9 @@ Class Stream Extends std.resource.Resource
 
 	#end
 	Method WriteShort( data:Short )
-		_tmpbuf.PokeShort( 0,data )
-		Write( _tmpbuf.Data,2 )
+
+		If _swap Swap2( Varptr data )
+		Write( Varptr data,2 )
 	End
 	
 	#rem monkeydoc Writes a 16 bit unsigned short to the stream.
@@ -407,8 +416,9 @@ Class Stream Extends std.resource.Resource
 
 	#end
 	Method WriteUShort( data:UShort )
-		_tmpbuf.PokeUShort( 0,data )
-		Write( _tmpbuf.Data,2 )
+
+		If _swap Swap2( Varptr data )
+		Write( Varptr data,2 )
 	End
 	
 	#rem monkeydoc Writes a 32 bit int to the stream.
@@ -417,8 +427,9 @@ Class Stream Extends std.resource.Resource
 
 	#end
 	Method WriteInt( data:Int )
-		_tmpbuf.PokeInt( 0,data )
-		Write( _tmpbuf.Data,4 )
+		
+		If _swap Swap4( Varptr data )
+		Write( Varptr data,4 )
 	End
 	
 	#rem monkeydoc Writes a 32 bit unsigned int to the stream.
@@ -427,8 +438,9 @@ Class Stream Extends std.resource.Resource
 
 	#end
 	Method WriteUInt( data:UInt )
-		_tmpbuf.PokeUInt( 0,data )
-		Write( _tmpbuf.Data,4 )
+
+		If _swap Swap4( Varptr data )
+		Write( Varptr data,4 )
 	End
 	
 	#rem monkeydoc Writes a 64 bit long to the stream.
@@ -437,8 +449,9 @@ Class Stream Extends std.resource.Resource
 
 	#end
 	Method WriteLong( data:Long )
-		_tmpbuf.PokeLong( 0,data )
-		Write( _tmpbuf.Data,8 )
+
+		If _swap Swap8( Varptr data )
+		Write( Varptr data,8 )
 	End
 	
 	#rem monkeydoc Writes a 64 bit unsigned long to the stream.
@@ -447,8 +460,9 @@ Class Stream Extends std.resource.Resource
 
 	#end
 	Method WriteULong( data:ULong )
-		_tmpbuf.PokeULong( 0,data )
-		Write( _tmpbuf.Data,8 )
+
+		If _swap Swap8( Varptr data )
+		Write( Varptr data,8 )
 	End
 	
 	#rem monkeydoc Writes a 32 bit float to the stream,
@@ -457,8 +471,9 @@ Class Stream Extends std.resource.Resource
 
 	#end
 	Method WriteFloat:Void( data:Float )
-		_tmpbuf.PokeFloat( 0,data )
-		Write( _tmpbuf.Data,4 )
+
+		If _swap Swap4( Varptr data )
+		Write( Varptr data,4 )
 	End
 	
 	#rem monkeydoc Writes a 64 bit double to the stream.
@@ -467,8 +482,9 @@ Class Stream Extends std.resource.Resource
 
 	#end
 	Method WriteDouble( data:Double )
-		_tmpbuf.PokeDouble( 0,data )
-		Write( _tmpbuf.Data,8 )
+
+		If _swap Swap8( Varptr data )
+		Write( Varptr data,8 )
 	End
 	
 	#rem monkeydoc Writes a string to the stream (NOT null terminated).
@@ -477,6 +493,7 @@ Class Stream Extends std.resource.Resource
 	
 	#end
 	Method WriteString( str:String )
+		
 		Local buf:=New DataBuffer( str.CStringLength )
 		buf.PokeString( 0,str )
 		Write( buf,0,buf.Length )
@@ -489,6 +506,7 @@ Class Stream Extends std.resource.Resource
 	
 	#end
 	Method WriteSizedString( str:String )
+		
 		WriteInt( str.CStringLength )
 		WriteString( str )
 	End
@@ -499,6 +517,7 @@ Class Stream Extends std.resource.Resource
 	
 	#end
 	Method WriteCString( str:String )
+		
 		WriteString( str )
 		WriteByte( 0 )
 	End
@@ -509,6 +528,7 @@ Class Stream Extends std.resource.Resource
 	
 	#end
 	Method WriteLine( str:String )
+		
 		WriteString( str )
 		WriteString( "~r~n" )
 	End
@@ -542,7 +562,8 @@ Class Stream Extends std.resource.Resource
 	Protected
 	
 	Method New()
-		_tmpbuf=_LEbuf
+		
+		_swap=false
 	End
 	
 	Method OnClose() Virtual
@@ -552,9 +573,21 @@ Class Stream Extends std.resource.Resource
 	
 	Private
 	
-	Field _tmpbuf:DataBuffer
+	Field _swap:Bool
 	
-	Global _BEbuf:=New DataBuffer( 8,std.memory.ByteOrder.BigEndian )
-	Global _LEbuf:=New DataBuffer( 8,std.memory.ByteOrder.LittleEndian )
+	Function Swap2( v:Void Ptr )
+		Local t:=Cast<UShort Ptr>( v )[0]
+		Cast<UShort Ptr>( v )[0]=(t Shr 8 & $ff) | (t & $ff) Shl 8
+	End
+	
+	Function Swap4( v:Void Ptr )
+		Local t:=Cast<UInt Ptr>( v )[0]
+		Cast<UInt Ptr>( v )[0]=(t Shr 24 & $ff) | (t & $ff) Shl 24 | (t Shr 8 & $ff00) | (t & $ff00) Shl 8
+	End
+	
+	Function Swap8( v:Void Ptr )
+		Local t:=Cast<ULong Ptr>( v )[0]
+		Cast<ULong Ptr>( v )[0]=(t Shr 56 & $ff) | (t & $ff) Shl 56 | (t Shr 40 & $ff00) | (t & $ff00) Shl 40 | (t Shr 24 & $ff0000) | (t & $ff0000) Shl 24 | (t Shr 8 & $ff000000) | (t & $ff000000) Shl 8
+	End
 
 End