Exemplo n.º 1
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

        }
Exemplo n.º 2
0
        public Task<UdpReceiveResult> ReceiveAsync()
        {
            // workaround...
            int malloc_bytearray_paddingleft = 4;



            ConsoleExtensions.trace("enter UdpClient ReceiveAsync");

            var buff = stackalloc byte[0xfff];

            socket_h.sockaddr_in sender;
            var sizeof_sender = sizeof(socket_h.sockaddr_in);

            //ConsoleExtensions.trace("before recvfrom");

            // http://pubs.opengroup.org/onlinepubs/009695399/functions/recvfrom.html
            // Upon successful completion, recvfrom() shall return the length of the message in bytes. 
            var recvfromret = s.recvfrom(buff, 0xfff, 0, (socket_h.sockaddr*)&sender, &sizeof_sender);
            // sent by?

            //I/xNativeActivity(24024): X:\jsc.svn\examples\c\android\Test\NDKUdpClient\NDKUdpClient\xNativeActivity.cs:167 recvfrom:  116 errno: 22 Invalid argument
            //I/xNativeActivity(24024): X:\jsc.svn\examples\c\android\Test\NDKUdpClient\NDKUdpClient\xNativeActivity.cs:168 SenderAddrSize:  16 errno: 22 Invalid argument
            ConsoleExtensions.tracei("recvfrom: ", recvfromret);
            //tracei("sockaddr_in: ", sizeof_sender);

            buff[recvfromret] = 0;


            //Task<UdpReceiveResult> 

            // lets fill up GC. collect when thread shuts down?
            //var Buffer = new byte[recvfromret];


            // what about digitally signing the buffer?
            //var le_bytes = (byte*)stdlib_h.malloc(stdlib_h.malloc_bytearray_paddingleft + recvfromret);
            var le_bytes = (byte*)stdlib_h.malloc(malloc_bytearray_paddingleft + recvfromret);


            // I/xNativeActivity(12673): [2781936] \UdpClient.cs:110 recvfrom:  3
            //I/xNativeActivity(12673): [2781936] \UdpClient.cs:138 bytesLength:  50331648
            ConsoleExtensions.trace("set le_ipointer");
            var le_ipointer = (int*)le_bytes;
            *le_ipointer = recvfromret;

            //int32_5 = ((int*)byte_4);
            //*int32_5 = num3;

            {
                ConsoleExtensions.trace("get le_ipointer");
                var lei = *le_ipointer;
                ConsoleExtensions.tracei("lei: ", lei);
            }


            // Z:\jsc.svn\examples\c\Test\TestPointerOffset\Class1.cs

            #region ___bytes
            // byte_7 = ((unsigned char*)(&(byte_4[1])));
            var ___bytes = le_bytes + malloc_bytearray_paddingleft;
            for (int i = 0; i < recvfromret; i++)
            {
                var byte0 = buff[i];

                ConsoleExtensions.tracei("set byte0: ", byte0);

                //Buffer[i] = buff[i];
                ___bytes[i] = byte0;
            }
            #endregion

            {
                ConsoleExtensions.trace("get le_ipointer");
                var lei = *le_ipointer;
                ConsoleExtensions.tracei("lei: ", lei);
            }


            var bytes = __cast(___bytes);

            {
                ConsoleExtensions.trace("---");

                var byte0 = bytes[0];
                ConsoleExtensions.tracei("byte0: ", byte0);

                var bytesLength = bytes.Length;
                ConsoleExtensions.tracei("bytesLength: ", bytesLength);
            }

            // ptr?

            //{

            //    var xbytes = __cast(bytes);
            //    var xlebytes = xbytes - 4;
            //    var ile = (int*)xlebytes;
            //    ConsoleExtensions.tracei("ile: ", *ile);

            //}


            //I/xNativeActivity( 2541): [2777960] \UdpClient.cs:199 set task_Result_Buffer:  -192277436
            //I/xNativeActivity( 2541): [2777960] \UdpClient.cs:202 Result.Buffer.Length:  2
            //I/xNativeActivity( 2541): [2777960] \UdpClient.cs:211 got task_Result
            //I/xNativeActivity( 2541): [2777960] \UdpClient.cs:229 got task_Result_Buffer:  -867631102

            ConsoleExtensions.tracei64("set task_Result_Buffer: ", (long)__cast(bytes));


            //var Result = new UdpReceiveResult(bytes, null);
            var Result = new __UdpReceiveResult(bytes, null);

            //result13 = ScriptCoreLib_Shared_BCLImplementation_System_Net_Sockets___UdpReceiveResult__ctor_6000072(__new_ScriptCoreLib_Shared_BCLImplementation_System_Net_Sockets___UdpReceiveResult(1), byteArray11, NULL);
            //    ScriptCoreLibAndroidNDK_Library_ConsoleExtensions_tracei64("Result: ", (long long)result13, "z:\x5c""jsc.svn\x5c""core\x5c""ScriptCoreLibAndroidNDK\x5c""ScriptCoreLibAndroidNDK\x5c""BCLImplementation\x5c""System\x5c""Net\x5c""Sockets\x5c""UdpClient.cs", 212);


            // \UdpClient.cs:210 Result:  0
            ConsoleExtensions.tracei64("Result: ", (long)(object)Result);


            //typedef struct tag_ScriptCoreLib_Shared_BCLImplementation_System_Net_Sockets___UdpReceiveResult
            //{
            //    struct tag_ScriptCoreLibJava_BCLImplementation_System_Net___IPEndPoint* _RemoteEndPoint_k__BackingField;
            //    unsigned char* _Buffer_k__BackingField;
            //} ScriptCoreLib_Shared_BCLImplementation_System_Net_Sockets___UdpReceiveResult, *LPScriptCoreLib_Shared_BCLImplementation_System_Net_Sockets___UdpReceiveResult;


            //ConsoleExtensions.tracei("Result.Buffer.Length: ", Result.Buffer.Length);

            //var task = new __Task<UdpReceiveResult>();
            var task = new __Task<__UdpReceiveResult>();

            //typedef struct tag_ScriptCoreLibNative_BCLImplementation_System___Task_1
            //{
            //    void* _Result_k__BackingField;
            //} ScriptCoreLibNative_BCLImplementation_System___Task_1, *LPScriptCoreLibNative_BCLImplementation_System___Task_1;

            task.Result = (__UdpReceiveResult)(object)Result;


            var task_Result = task.Result;

            ConsoleExtensions.trace("got task_Result, crash?");

            ConsoleExtensions.tracei64("task_Result: ", (long)(object)task_Result);


            //Task<UdpReceiveResult> t = new __Task<UdpReceiveResult> { Result = Result };

            //     ScriptCoreLibAndroidNDK_Library_ConsoleExtensions_tracei("t.Result.Buffer.Length: ", (int)((signed int)(/* ldlen */ *(int*)(&(ScriptCoreLib_Shared_BCLImplementation_System_Net_Sockets___UdpReceiveResult_get_Buffer((ScriptCoreLib_Shared_BCLImplementation_System_Net_Sockets___UdpReceiveResult*)(&result18))[-4])))), "z:\x5c""jsc.svn\x5c""core\x5c""ScriptCoreLibAndroidNDK\x5c""ScriptCoreLibAndroidNDK\x5c""BCLImplementation\x5c""System\x5c""Net\x5c""Sockets\x5c""UdpClient.cs", 211);

            // struct??
            //if (task_Result == null)
            //{
            //}
            //else
            {
                //  byteArray19 = ScriptCoreLib_Shared_BCLImplementation_System_Net_Sockets___UdpReceiveResult_get_Buffer((ScriptCoreLib_Shared_BCLImplementation_System_Net_Sockets___UdpReceiveResult*)(&result18));


                var task_Result_Buffer = task_Result.Buffer;

                // \UdpClient.cs:226 got task_Result_Buffer:  848560130
                ConsoleExtensions.tracei64("got task_Result_Buffer: ", (long)__cast(task_Result_Buffer));

                ConsoleExtensions.tracei("t.Result.Buffer.Length: ", task_Result_Buffer.Length);
                //  t.Result.Buffer.Length:  2
            }

            return (Task<UdpReceiveResult>)(object)task;
        }