Пример #1
0
        public EndPoint LocalEndPoint_internal(out int error)
        {
            error = 0;
            java.net.InetSocketAddress localAddr = null;

            try
            {
                localAddr = (java.net.InetSocketAddress)jSocket.getLocalSocketAddress();
            }
            catch (Exception e)
            {
                localAddr = null;
#if DEBUG
                Console.WriteLine("Caught exception during LocalEndPoint_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
            }

            if (localAddr == null || localAddr.getAddress() == null || localAddr.getPort() < 0)
            {
                return(null);
            }

            IPHostEntry lipa = Dns.Resolve(localAddr.getHostName());
            IPEndPoint  ret  = new IPEndPoint(lipa.AddressList[0], localAddr.getPort());
            return(ret);
        }
Пример #2
0
        public EndPoint RemoteEndPoint_internal(out int error)
        {
            error = 0;
            java.net.InetSocketAddress remoteAddr = null;

            if (jSocket == null || !jSocket.isBound())
            {
                return(null);
            }

            try
            {
                remoteAddr = (java.net.InetSocketAddress)jSocket.getRemoteSocketAddress();
            }
            catch (Exception e)
            {
                remoteAddr = null;
#if DEBUG
                Console.WriteLine("Caught exception during RemoteEndPoint_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
            }

            if (remoteAddr == null || remoteAddr.getAddress() == null || remoteAddr.getPort() <= 0)
            {
                error = 10057;                 //WSAENOTCONN (Socket is not connected)
                return(null);
            }

            IPHostEntry lipa = Dns.Resolve(remoteAddr.getHostName());
            IPEndPoint  ret  = new IPEndPoint(lipa.AddressList[0], remoteAddr.getPort());
            return(ret);
        }
Пример #3
0
        public void Bind(EndPoint local_end)
        {
            var lEndPoint = (IPEndPoint)local_end;

            if (lEndPoint.Address == null)
            {
                lEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), lEndPoint.Port);
            }
                        #if cooper
            var lAddress = java.net.InetAddress.getByAddress(lEndPoint.Address.GetAddressBytes());
            fSocketAddress = new java.net.InetSocketAddress(lAddress, lEndPoint.Port);
            fIsServer      = true;
            fServerHandle  = new java.net.ServerSocket();
                        #else
            void *lPointer;
            int   lSize;
                        #if posix || toffee || darwin
            rtl.__struct_sockaddr_in  lIPv4;
            rtl.__struct_sockaddr_in6 lIPv6;
                        #if posix && !darwin && !android
            rtl.__CONST_SOCKADDR_ARG lSockAddr;
                        #endif
                        #else
            rtl.SOCKADDR_IN lIPv4;
            sockaddr_in6    lIPv6;
                        #endif

            IPEndPointToNative(lEndPoint, out lIPv4, out lIPv6, out lPointer, out lSize);
                        #if posix && !darwin && !android
            lSockAddr.__sockaddr__    = (rtl.__struct_sockaddr *)lPointer;
            lSockAddr.__sockaddr_in__ = (rtl.__struct_sockaddr_in *)lPointer;
            if (rtl.__Global.bind(fHandle, lSockAddr, lSize) != 0)
            {
                throw new Exception("Error calling bind function");
            }
                        #elif toffee || darwin || android
            if (rtl.bind(fHandle, (rtl.__struct_sockaddr *)lPointer, lSize) != 0)
            {
                throw new Exception("Error calling bind function");
            }
                        #elif island && windows
            if (rtl.bind(fHandle, lPointer, lSize) != 0)
            {
                throw new Exception("Error calling bind function");
            }
                        #else
            throw new Exception("Error calling bind function");
                        #endif
                        #endif
            LocalEndPoint = lEndPoint;
        }
Пример #4
0
        public void Bind_internal(EndPoint sa, out int error)
        {
            error = 0;
            IPEndPoint addr = sa as IPEndPoint;

            if (addr == null)
            {
                error = 10044;                 //WSAESOCKTNOSUPPORT (Socket type not supported)
                return;
            }

            if (jSocket == null || jSocket.isBound() || jSocket.isConnected() || jSocketChannel.isConnectionPending())
            {
                error = 10022;                 //WSAEINVAL (Invalid argument)
                return;
            }

            try
            {
                // This code I need because a bug in the java.nio.channels.SocketAdapter, which
                // returns local port 0 if the socket is not connected (even if the socket is bound)
                // so I need temporary use regular socket (not channel socket) to bind it to the
                // local address and use this address in the LocalPoint property and to create the
                // actual client/server channel sockets
                // The bug #5076965 (SocketChannel does not report local address after binding to a wildcard )
                // See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5076965
                java.net.Socket jTempSocket = new java.net.Socket();
                jTempSocket.bind(new java.net.InetSocketAddress(java.net.InetAddress.getByName(addr.Address.ToString()),
                                                                addr.Port));
                jTempLocalSocketAddress = (java.net.InetSocketAddress)jTempSocket.getLocalSocketAddress();
                jTempSocket.close();
                jSocket.bind(jTempLocalSocketAddress);
            }
            catch (Exception e)
            {
                error = 10048;                 //WSAEADDRINUSE (Address already in use)
#if DEBUG
                Console.WriteLine("Caught exception during Bind_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
            }
        }
Пример #5
0
		public void Bind_internal(EndPoint sa, out int error)
		{
			error = 0;
			IPEndPoint addr = sa as IPEndPoint;
			if (addr == null)
			{
				error = 10044; //WSAESOCKTNOSUPPORT (Socket type not supported)
				return;
			}

			if (jSocket == null || jSocket.isBound() || jSocket.isConnected() || jSocketChannel.isConnectionPending())
			{
				error = 10022; //WSAEINVAL (Invalid argument)
				return;
			}

			try
			{
				// This code I need because a bug in the java.nio.channels.SocketAdapter, which 
				// returns local port 0 if the socket is not connected (even if the socket is bound)
				// so I need temporary use regular socket (not channel socket) to bind it to the 
				// local address and use this address in the LocalPoint property and to create the 
				// actual client/server channel sockets
				// The bug #5076965 (SocketChannel does not report local address after binding to a wildcard )
				// See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5076965
				java.net.Socket jTempSocket = new java.net.Socket();
				jTempSocket.bind(new java.net.InetSocketAddress(java.net.InetAddress.getByName(addr.Address.ToString()),
									                        addr.Port));
				jTempLocalSocketAddress = (java.net.InetSocketAddress)jTempSocket.getLocalSocketAddress();
				jTempSocket.close();
				jSocket.bind(jTempLocalSocketAddress);
			}
			catch (Exception e)
			{
				error = 10048; //WSAEADDRINUSE (Address already in use)
#if DEBUG
				Console.WriteLine("Caught exception during Bind_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
#endif
			}
		}
Пример #6
0
        public __UdpClient(xConstructorArguments args)
        {
            //Console.WriteLine("enter __UdpClient ctor");


            // http://stackoverflow.com/questions/8558791/multicastsocket-vs-datagramsocket-in-broadcasting-to-multiple-clients
            // you must use MulticastSocket for receiving the multicasts; for sending them, again, you can use either DatagramSocket or MulticastSocket, and there is no difference in efficiency.

            //var buffer = new sbyte[0x10000];

            // X:\jsc.svn\market\synergy\javascript\chrome\chrome\BCLImplementation\System\Net\Sockets\UdpClient.cs
            //var buffer = new sbyte[0x1000];
            var buffer = new sbyte[MaxData];

            //E/dalvikvm-heap(14366): Out of memory on a 1048592-byte allocation.
            //I/dalvikvm(14366): "Thread-4310" prio=5 tid=827 RUNNABLE



            // tested by
            // X:\jsc.svn\examples\java\android\vr\OVRMyCubeWorldNDK\OVRMyCubeWorld\ApplicationActivity.cs

            #region vJoinMulticastGroup
            this.vJoinMulticastGroup = (IPAddress multicastAddr, IPAddress localAddress) =>
           {
               // http://developer.android.com/reference/java/net/InetSocketAddress.html
               // http://developer.android.com/reference/java/net/SocketAddress.html

               Console.WriteLine("enter vJoinMulticastGroup");
               // at this point we have to jump back in time and get a multicast socket.

               __IPAddress __multicastAddr = multicastAddr;
               __IPAddress __localAddress = localAddress;

               __NetworkInterface nic = __localAddress.InternalNetworkInterface;

               // X:\jsc.svn\examples\java\android\LANBroadcastListener\LANBroadcastListener\ApplicationActivity.cs
               // X:\jsc.svn\examples\java\android\forms\FormsUDPJoinGroup\FormsUDPJoinGroup\ApplicationControl.cs

               try
               {
                   args.xMulticastSocket.joinGroup(
                       new java.net.InetSocketAddress(
                            __multicastAddr.InternalAddress,
                            args.port
                       ),

                       nic
                   );
               }
               catch
               {
                   throw;
               }

           };
            #endregion


            #region vReceiveAsync
            this.vReceiveAsync = delegate
            {
                var c = new TaskCompletionSource<__UdpReceiveResult>();

                __Task.Run(
                    delegate
                    {
                        // http://stackoverflow.com/questions/10808512/datagrampacket-equivalent
                        // http://tutorials.jenkov.com/java-networking/udp-datagram-sockets.html


                        // tested by?
                        var packet = new java.net.DatagramPacket(buffer, buffer.Length);

                        try
                        {
                            args.xDatagramSocket.receive(packet);


                            var xbuffer = new byte[packet.getLength()];


                            Array.Copy(
                                buffer, xbuffer,
                                xbuffer.Length
                            );

                            var x = new __UdpReceiveResult(
                                buffer:
                                    xbuffer,

                                remoteEndPoint:
                                    new __IPEndPoint(
                                        new __IPAddress { InternalAddress = packet.getAddress() },
                                        port: packet.getPort()
                                    )
                            );

                            c.SetResult(x);
                        }
                        catch
                        {
                            // fault? 
                        }
                    }
                );


                return c.Task;
            };
            #endregion

            //Console.WriteLine("enter __UdpClient before this.Client");

            this.Client = new __Socket
           {
               #region vBind
               vBind = (EndPoint localEP) =>
               {
                   // https://sites.google.com/a/jsc-solutions.net/work/knowledge-base/15-dualvr/20150721/ovroculus360photoshud

                   try
                   {
                       Console.WriteLine("enter __UdpClient vBind " + new { args.xDatagramSocket, localEP });


                       var v4 = localEP as IPEndPoint;
                       if (v4 != null)
                       {
                           __IPAddress v4a = v4.Address;



                           // http://developer.android.com/reference/java/net/InetSocketAddress.html
                           var x = new java.net.InetSocketAddress(v4a.InternalAddress, v4.Port);

                           Console.WriteLine("before __UdpClient vBind " + new { v4a.InternalAddress, v4.Port });

                           // Z:\jsc.svn\examples\java\android\vr\OVRWindWheelNDK\UDPWindWheel\Program.cs
                           // is this the first bind?

                           if (args.xDatagramSocket == null)
                           {
                               var xDatagramSocket = new java.net.DatagramSocket(v4.Port);

                               args.vDatagramSocket = () => xDatagramSocket;
                           }
                           else
                           {
                               args.xDatagramSocket.bind(x);
                           }
                       }

                   }
                   catch (Exception err)
                   {
                       Console.WriteLine("err __UdpClient vBind " + new { err.Message, err.StackTrace });
                       //throw;


                       throw new InvalidOperationException();
                   }
               }
               #endregion
           };



            //Console.WriteLine("enter __UdpClient after this.Client " + new { this.Client });


            #region vSend
            this.vSend = (byte[] datagram, int bytes, string hostname, int port) =>
            {
                //I/System.Console(22987): 59cb:0001 about to Send
                //I/System.Console(22987): 59cb:0001 enter __UdpClient vSend
                //I/System.Console(22987): 59cb:0001 err __UdpClient vSend { Message = , StackTrace = android.os.NetworkOnMainThreadException
                //I/System.Console(22987):        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
                //I/System.Console(22987):        at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:276)
                //I/System.Console(22987):        at libcore.io.IoBridge.sendto(IoBridge.java:513)
                //I/System.Console(22987):        at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:184)
                //I/System.Console(22987):        at java.net.DatagramSocket.send(DatagramSocket.java:305)
                //I/System.Console(22987):        at ScriptCoreLibJava.BCLImplementation.System.Net.Sockets.__UdpClient___c__DisplayClass12.__ctor_b__6(__UdpClient___c__DisplayClass12.java:109)

                var t = this.vSendAsync(datagram, bytes, hostname, port);



                return t.Result;
            };
            #endregion


            #region vSendAsync
            this.vSendAsync = (byte[] datagram, int bytes, string hostname, int port) =>
            {
                var c = new TaskCompletionSource<int>();
                __Task.Run(
                    delegate
                    {
                        try
                        {
                            var a = global::java.net.InetAddress.getByName(hostname);
                            var packet = new java.net.DatagramPacket(
                                (sbyte[])(object)datagram,
                                bytes, a, port
                            );
                            args.xDatagramSocket.send(packet);
                            // retval tested?
                            c.SetResult(
                                packet.getLength()
                            );
                        }
                        catch
                        {
                            throw;
                        }
                    }
                );
                return c.Task;
            };
            #endregion

            #region vSendAsync2
            this.vSendAsync2 = (byte[] datagram, int bytes, IPEndPoint endPoint) =>
            {
                var c = new TaskCompletionSource<int>();
                __Task.Run(
                    delegate
                    {
                        try
                        {
                            var packet = new java.net.DatagramPacket(
                                (sbyte[])(object)datagram,
                                bytes, (__IPAddress)endPoint.Address, endPoint.Port
                            );
                            args.xDatagramSocket.send(packet);
                            // retval tested?
                            c.SetResult(
                                packet.getLength()
                            );
                        }
                        catch
                        {
                            throw;
                        }
                    }
                );
                return c.Task;
            };
            #endregion

            #region vClose
            this.vClose = delegate
            {
                try
                {
                    args.xDatagramSocket.close();
                }
                catch
                {
                }
            };
            #endregion

        }
Пример #7
0
        public __UdpClient(xConstructorArguments args)
        {
            //Console.WriteLine("enter __UdpClient ctor");


            // http://stackoverflow.com/questions/8558791/multicastsocket-vs-datagramsocket-in-broadcasting-to-multiple-clients
            // you must use MulticastSocket for receiving the multicasts; for sending them, again, you can use either DatagramSocket or MulticastSocket, and there is no difference in efficiency.

            //var buffer = new sbyte[0x10000];

            // X:\jsc.svn\market\synergy\javascript\chrome\chrome\BCLImplementation\System\Net\Sockets\UdpClient.cs
            //var buffer = new sbyte[0x1000];
            var buffer = new sbyte[MaxData];

            //E/dalvikvm-heap(14366): Out of memory on a 1048592-byte allocation.
            //I/dalvikvm(14366): "Thread-4310" prio=5 tid=827 RUNNABLE



            // tested by
            // X:\jsc.svn\examples\java\android\vr\OVRMyCubeWorldNDK\OVRMyCubeWorld\ApplicationActivity.cs

            #region vJoinMulticastGroup
            this.vJoinMulticastGroup = (IPAddress multicastAddr, IPAddress localAddress) =>
            {
                // http://developer.android.com/reference/java/net/InetSocketAddress.html
                // http://developer.android.com/reference/java/net/SocketAddress.html

                Console.WriteLine("enter vJoinMulticastGroup");
                // at this point we have to jump back in time and get a multicast socket.

                __IPAddress __multicastAddr = multicastAddr;
                __IPAddress __localAddress  = localAddress;

                __NetworkInterface nic = __localAddress.InternalNetworkInterface;

                // X:\jsc.svn\examples\java\android\LANBroadcastListener\LANBroadcastListener\ApplicationActivity.cs
                // X:\jsc.svn\examples\java\android\forms\FormsUDPJoinGroup\FormsUDPJoinGroup\ApplicationControl.cs

                try
                {
                    args.xMulticastSocket.joinGroup(
                        new java.net.InetSocketAddress(
                            __multicastAddr.InternalAddress,
                            args.port
                            ),

                        nic
                        );
                }
                catch
                {
                    throw;
                }
            };
            #endregion


            #region vReceiveAsync
            this.vReceiveAsync = delegate
            {
                var c = new TaskCompletionSource <__UdpReceiveResult>();

                __Task.Run(
                    delegate
                {
                    // http://stackoverflow.com/questions/10808512/datagrampacket-equivalent
                    // http://tutorials.jenkov.com/java-networking/udp-datagram-sockets.html


                    // tested by?
                    var packet = new java.net.DatagramPacket(buffer, buffer.Length);

                    try
                    {
                        args.xDatagramSocket.receive(packet);


                        var xbuffer = new byte[packet.getLength()];


                        Array.Copy(
                            buffer, xbuffer,
                            xbuffer.Length
                            );

                        var x = new __UdpReceiveResult(
                            buffer:
                            xbuffer,

                            remoteEndPoint:
                            new __IPEndPoint(
                                new __IPAddress {
                            InternalAddress = packet.getAddress()
                        },
                                port: packet.getPort()
                                )
                            );

                        c.SetResult(x);
                    }
                    catch
                    {
                        // fault?
                    }
                }
                    );


                return(c.Task);
            };
            #endregion

            //Console.WriteLine("enter __UdpClient before this.Client");

            this.Client = new __Socket
            {
                #region vBind
                vBind = (EndPoint localEP) =>
                {
                    // https://sites.google.com/a/jsc-solutions.net/work/knowledge-base/15-dualvr/20150721/ovroculus360photoshud

                    try
                    {
                        Console.WriteLine("enter __UdpClient vBind " + new { args.xDatagramSocket, localEP });


                        var v4 = localEP as IPEndPoint;
                        if (v4 != null)
                        {
                            __IPAddress v4a = v4.Address;



                            // http://developer.android.com/reference/java/net/InetSocketAddress.html
                            var x = new java.net.InetSocketAddress(v4a.InternalAddress, v4.Port);

                            Console.WriteLine("before __UdpClient vBind " + new { v4a.InternalAddress, v4.Port });

                            // Z:\jsc.svn\examples\java\android\vr\OVRWindWheelNDK\UDPWindWheel\Program.cs
                            // is this the first bind?

                            if (args.xDatagramSocket == null)
                            {
                                var xDatagramSocket = new java.net.DatagramSocket(v4.Port);

                                args.vDatagramSocket = () => xDatagramSocket;
                            }
                            else
                            {
                                args.xDatagramSocket.bind(x);
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        Console.WriteLine("err __UdpClient vBind " + new { err.Message, err.StackTrace });
                        //throw;


                        throw new InvalidOperationException();
                    }
                }
                #endregion
            };



            //Console.WriteLine("enter __UdpClient after this.Client " + new { this.Client });


            #region vSend
            this.vSend = (byte[] datagram, int bytes, string hostname, int port) =>
            {
                //I/System.Console(22987): 59cb:0001 about to Send
                //I/System.Console(22987): 59cb:0001 enter __UdpClient vSend
                //I/System.Console(22987): 59cb:0001 err __UdpClient vSend { Message = , StackTrace = android.os.NetworkOnMainThreadException
                //I/System.Console(22987):        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
                //I/System.Console(22987):        at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:276)
                //I/System.Console(22987):        at libcore.io.IoBridge.sendto(IoBridge.java:513)
                //I/System.Console(22987):        at java.net.PlainDatagramSocketImpl.send(PlainDatagramSocketImpl.java:184)
                //I/System.Console(22987):        at java.net.DatagramSocket.send(DatagramSocket.java:305)
                //I/System.Console(22987):        at ScriptCoreLibJava.BCLImplementation.System.Net.Sockets.__UdpClient___c__DisplayClass12.__ctor_b__6(__UdpClient___c__DisplayClass12.java:109)

                var t = this.vSendAsync(datagram, bytes, hostname, port);



                return(t.Result);
            };
            #endregion


            #region vSendAsync
            this.vSendAsync = (byte[] datagram, int bytes, string hostname, int port) =>
            {
                var c = new TaskCompletionSource <int>();
                __Task.Run(
                    delegate
                {
                    try
                    {
                        var a      = global::java.net.InetAddress.getByName(hostname);
                        var packet = new java.net.DatagramPacket(
                            (sbyte[])(object)datagram,
                            bytes, a, port
                            );
                        args.xDatagramSocket.send(packet);
                        // retval tested?
                        c.SetResult(
                            packet.getLength()
                            );
                    }
                    catch
                    {
                        throw;
                    }
                }
                    );
                return(c.Task);
            };
            #endregion

            #region vSendAsync2
            this.vSendAsync2 = (byte[] datagram, int bytes, IPEndPoint endPoint) =>
            {
                var c = new TaskCompletionSource <int>();
                __Task.Run(
                    delegate
                {
                    try
                    {
                        var packet = new java.net.DatagramPacket(
                            (sbyte[])(object)datagram,
                            bytes, (__IPAddress)endPoint.Address, endPoint.Port
                            );
                        args.xDatagramSocket.send(packet);
                        // retval tested?
                        c.SetResult(
                            packet.getLength()
                            );
                    }
                    catch
                    {
                        throw;
                    }
                }
                    );
                return(c.Task);
            };
            #endregion

            #region vClose
            this.vClose = delegate
            {
                try
                {
                    args.xDatagramSocket.close();
                }
                catch
                {
                }
            };
            #endregion
        }