Mark Sibly 8 lat temu
rodzic
commit
88b090b19a

+ 18 - 15
modules/std/socket/native/socket.cpp

@@ -85,7 +85,7 @@ namespace bbSocket{
 #endif
 	}
 	
-	int _connect( const char *hostname,const char *service,int type ){
+	int _connect( const char *hostname,const char *service,int type,int flags ){
 	
 		init();
 		
@@ -94,6 +94,8 @@ namespace bbSocket{
 
 		hints.ai_family=AF_UNSPEC;
 		hints.ai_socktype=(type==1) ? SOCK_DGRAM : SOCK_STREAM;
+		hints.ai_flags=((flags&1) ? AI_PASSIVE : 0) | ((flags&2) ? AI_NUMERICHOST : 0);
+		if( hostname && !hostname[0] ) hostname=0;
 
 		addrinfo *pres=0;
 		if( getaddrinfo( hostname,service,&hints,&pres ) ) return -1;
@@ -117,7 +119,7 @@ namespace bbSocket{
 		return sock;
 	}
 	
-	int _bind( const char *hostname,const char *service,int type ){
+	int _bind( const char *hostname,const char *service,int type,int flags ){
 	
 		init();
 		
@@ -126,7 +128,8 @@ namespace bbSocket{
 		
 		hints.ai_family=AF_UNSPEC;
 		hints.ai_socktype=(type==1) ? SOCK_DGRAM : SOCK_STREAM;
-		hints.ai_flags=AI_PASSIVE;
+		hints.ai_flags=((flags&1) ? AI_PASSIVE : 0) | ((flags&2) ? AI_NUMERICHOST : 0);
+		if( hostname && !hostname[0] ) hostname=0;
 		
 		addrinfo *pres=0;
 		if( getaddrinfo( hostname,service,&hints,&pres ) ) return -1;
@@ -150,16 +153,16 @@ namespace bbSocket{
 		return sock;
 	}
 	
-	int _listen( const char *hostname,const char *service,int queue,int type ){
+	int _listen( const char *hostname,const char *service,int backlog,int flags ){
 
-		int sock=_bind( hostname,service,type );
+		int sock=_bind( hostname,service,2,flags );
 			
-		if( sock!=-1 ) ::listen( sock,queue );
+		if( sock!=-1 ) ::listen( sock,backlog );
 		
 		return sock;
 	}
 	
-	int connect( const char *hostname,const char *service,int type ){
+	int connect( const char *hostname,const char *service,int type,int flags ){
 
 		int sock=-1;
 		
@@ -169,7 +172,7 @@ namespace bbSocket{
 			
 			std::thread thread( [=,&future](){
 
-				future.set( _connect( hostname,service,type ) );
+				future.set( _connect( hostname,service,type,flags ) );
 	
 			} );
 			
@@ -179,13 +182,13 @@ namespace bbSocket{
 
 		}else{
 		
-			sock=_connect( hostname,service,type );
+			sock=_connect( hostname,service,type,flags );
 		}
 		
 		return sock;
 	}
 	
-	int bind( const char *hostname,const char *service ){
+	int bind( const char *hostname,const char *service,int flags ){
 	
 		int sock=-1;
 		
@@ -195,7 +198,7 @@ namespace bbSocket{
 			
 			std::thread thread( [=,&future](){
 			
-				future.set( _bind( hostname,service,1 ) );
+				future.set( _bind( hostname,service,1,flags ) );
 	
 			} );
 			
@@ -205,13 +208,13 @@ namespace bbSocket{
 			
 		}else{
 		
-			sock=_bind( hostname,service,1 );
+			sock=_bind( hostname,service,1,flags );
 		}
 		
 		return sock;
 	}
 	
-	int listen( const char *hostname,const char *service,int queue ){
+	int listen( const char *hostname,const char *service,int backlog,int flags ){
 	
 		int sock=-1;
 		
@@ -221,7 +224,7 @@ namespace bbSocket{
 			
 			std::thread thread( [=,&future](){
 			
-				future.set( _listen( hostname,service,queue,0 ) );
+				future.set( _listen( hostname,service,backlog,flags ) );
 	
 			} );
 			
@@ -231,7 +234,7 @@ namespace bbSocket{
 			
 		}else{
 		
-			sock=_listen( hostname,service,queue,0 );
+			sock=_listen( hostname,service,backlog,flags );
 		}
 		
 		return sock;

+ 3 - 3
modules/std/socket/native/socket.h

@@ -6,11 +6,11 @@
 
 namespace bbSocket{
 
-	int connect( const char *hostname,const char *service,int type );
+	int connect( const char *hostname,const char *service,int type,int flags );
 	
-	int bind( const char *hostname,const char *service );
+	int bind( const char *hostname,const char *service,int flags );
 
-	int listen( const char *hostname,const char *service,int queue );
+	int listen( const char *hostname,const char *service,int backlog,int flags );
 
 	int accept( int socket );
 	

+ 26 - 13
modules/std/socket/socket.monkey2

@@ -12,15 +12,15 @@ Extern private
 
 #rem monkeydoc @hidden
 #end
-Function socket_connect:Int( hostname:CString,service:CString,type:Int )="bbSocket::connect"
+Function socket_connect:Int( hostname:CString,service:CString,type:Int,flags:int)="bbSocket::connect"
 
 #rem monkeydoc @hidden
 #end
-Function socket_bind:Int( hostname:CString,service:CString )="bbSocket::bind"
+Function socket_bind:Int( hostname:CString,service:CString,flags:int )="bbSocket::bind"
 
 #rem monkeydoc @hidden
 #end
-Function socket_listen:Int( hostname:CString,service:CString,queue:Int )="bbSocket::listen"
+Function socket_listen:Int( hostname:CString,service:CString,backlog:Int,flags:int )="bbSocket::listen"
 
 #rem monkeydoc @hidden
 #end
@@ -85,7 +85,7 @@ Public
 | SocketType	| Description
 |:--------------|:-----------
 | `Stream`		| Reliable stream, eg: TCP.
-| `Datagram`	| Unreliable datagram, eg: UDP.
+| `Datagram`	| Unreliable datagrams, eg: UDP.
 
 #end
 Enum SocketType
@@ -93,6 +93,19 @@ Enum SocketType
 	Datagram=1
 End
 
+#rem monkeydoc The SocketFlags enum.
+
+| SocketFlags	| Description
+|:--------------|:-----------
+| `Passive`		| for Bind and Listen only. Indicates socket accepts connections from any address. If not set, socket accepts loopback connections only.
+| `NumericHost`	| Socket name does not need to be looked up. Faster.
+
+#end
+Enum SocketFlags
+	Passive=1
+	NumericHost=2
+End
+
 #rem monkeydoc The SocketAddress class.
 
 A socket address encapsulates the hostname and service of a socket.
@@ -402,9 +415,9 @@ Class Socket Extends std.resource.Resource
 	@return A new socket.
 	
 	#end	
-	Function Connect:Socket( hostname:String,service:String,type:SocketType=SocketType.Stream )
+	Function Connect:Socket( hostname:String,service:String,type:SocketType )
 
-		Local socket:=socket_connect( hostname,service,type )
+		Local socket:=socket_connect( hostname,service,type,0 )
 		If socket=-1 Return Null
 		
 		Return New Socket( socket )
@@ -412,16 +425,16 @@ Class Socket Extends std.resource.Resource
 
 	#rem monkeydoc Creates a datagram server socket.
 	
-	Returns a new datagram server socket that can be connected to by datagram clients.
+	Returns a new datagram server socket listening at 'service' if successful.
 	
 	Returns null upon failure.
 
 	@return A new socket.
 	
 	#end
-	Function Bind:Socket( hostname:String,service:String )
-	
-		Local socket:=socket_bind( hostname,service )
+	Function Bind:Socket( service:String,loopback:bool=False )
+
+		Local socket:=socket_bind( "",service,loopback ? 0 Else 1 )
 		If socket=-1 Return Null
 		
 		Return New Socket( socket )
@@ -436,9 +449,9 @@ Class Socket Extends std.resource.Resource
 	@return A new socket.
 	
 	#end
-	Function Listen:Socket( hostname:String,service:String,queue:Int=128 )
-	
-		Local socket:=socket_listen( hostname,service,queue )
+	Function Listen:Socket( service:String,backlog:Int=32,loopback:bool=False )
+
+		Local socket:=socket_listen( "",service,backlog,loopback ? 0 Else 1 )
 		If socket=-1 Return Null
 		
 		Return New Socket( socket )