private static void ReceiveAsync(IntPtr info) { InternalReceiveAsync(info, false, (socket, socketobject, data, buffer, ofs, count, flags, remoteep, callback) => { bool success = false; if ((success = SocketExtension.BeginReceive(socket, buffer, ofs, count, flags, (result) => { int len = SocketExtension.EndReceive(socket, result, out SocketError error); NSJSVirtualMachine machine = socketobject.VirtualMachine; machine.Join((sender, state) => { if (len > 0) { for (int i = ofs; i < len; i++) { data[i] = buffer[i]; } } if (callback != null) { callback.Call(socketobject, NSJSInt32.New(machine, unchecked ((int)error)), NSJSInt32.New(machine, len)); } }); }))) { if (callback != null) { callback.CrossThreading = true; } data.CrossThreading = true; } return(success); });
private static void ProcessConnected(object sender, SocketAsyncEventArgs e) { using (e) { try { SocketContext context = e.UserToken as SocketContext; do { if (context == null) { break; } NSJSFunction function = context.ConnectedAsyncCallback; NSJSObject socket = context.This; context.ConnectedAsync = null; context.ConnectedAsyncCallback = null; if (function == null) { break; } NSJSVirtualMachine machine = function.VirtualMachine; if (machine == null) { break; } machine.Join((sendert, statet) => function.Call(socket, NSJSInt32.New(machine, unchecked ((int)e.SocketError)))); } while (false); } catch (Exception) { } } }
// [native] bool HttpClient.TryUploadAsync(string url, HttpPostValue[] blobs, HttpClientOptions options, HttpClientResponse response, HttpClientAsyncCallback callback) private static void TryUploadAsync(IntPtr info) { NSJSFunctionCallbackInfo arguments = NSJSFunctionCallbackInfo.From(info); bool success = false; if (arguments.Length > 4) { string url = (arguments[0] as NSJSString)?.Value; HttpClientOptions options = HttpClient.object2options(arguments[2] as NSJSObject); NSJSObject response = arguments[3] as NSJSObject; IEnumerable <HttpPostValue> blobs = HttpClient.array2blobs(arguments[1] as NSJSArray); if (options != null && response != null) { NSJSFunction callback = arguments[4] as NSJSFunction; if (callback != null) { callback.CrossThreading = true; } response.CrossThreading = true; bool fillToObject = false; HttpClientResponse responset = HttpClient.object2response(response); success = RESTClient.TryUploadAsync(url, blobs, options, responset, (error, buffer, count) => { NSJSVirtualMachine machine = arguments.VirtualMachine; if (error == HttpClientError.Success && !fillToObject) { fillToObject = true; fill2object(response, responset); } if (callback != null) { bool breakto = false; machine.Join((sender, state) => breakto = ((callback.Call ( NSJSInt32.New(machine, (int)error), NSJSValue.NullMerge(machine, buffer != null && count >= 0 ? NSJSUInt8Array.New(machine, buffer, count) : null) ) as NSJSBoolean)?.Value) == false); if (breakto) { return(false); } } return(count > 0); }); } } arguments.SetReturnValue(success); }
private static void ProcessAccept(object sender, SocketAsyncEventArgs e) { try { SocketContext context = e.UserToken as SocketContext; do { SOCKET client = e.AcceptSocket; SOCKET server = (SOCKET)sender; e.AcceptSocket = null; do { if (context == null) { break; } NSJSFunction function = context.AcceptAsyncCallback; NSJSObject socket = context.This; if (function == null) { break; } NSJSVirtualMachine machine = function.VirtualMachine; if (machine == null) { break; } machine.Join((sendert, statet) => function.Call(socket, NSJSInt32.New(machine, unchecked ((int)e.SocketError)), NSJSValue.NullMerge(machine, New(machine, client)))); } while (false); if (!server.AcceptAsync(e)) { ProcessAccept(server, e); } } while (false); } catch (Exception) { } }
public static NSJSValue As(this object value, NSJSVirtualMachine machine) { if (machine == null) { return(null); } if (value == null || value == DBNull.Value) { return(NSJSValue.Null(machine)); } if (value is NSJSValue) { return(value as NSJSValue); } Type typeid = value.GetType(); if (typeid == typeof(int) || typeid == typeof(short) || typeid == typeof(sbyte) || typeid == typeof(char)) { return(NSJSInt32.New(machine, Convert.ToInt32(value))); } else if (typeid == typeof(uint) || typeid == typeof(ushort) || typeid == typeof(byte)) { return(NSJSUInt32.New(machine, Convert.ToUInt32(value))); } else if (typeid == typeof(string)) { return(NSJSString.New(machine, value.ToString())); } else if (typeid == typeof(bool)) { return(NSJSBoolean.New(machine, Convert.ToBoolean(value))); } else if (typeid == typeof(DateTime)) { DateTime datetime = Convert.ToDateTime(value); if (NSJSDateTime.Invalid(datetime)) { datetime = NSJSDateTime.Min; } return(NSJSDateTime.New(machine, datetime)); } else if (typeid == typeof(float) || typeid == typeof(double)) { return(NSJSDouble.New(machine, Convert.ToDouble(value))); } else if (typeid == typeof(byte[])) { byte[] buffer = (byte[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSUInt8Array.New(machine, buffer)); } else if (typeid == typeof(sbyte[])) { sbyte[] buffer = (sbyte[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSInt8Array.New(machine, buffer)); } else if (typeid == typeof(short[])) { short[] buffer = (short[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSInt16Array.New(machine, buffer)); } else if (typeid == typeof(ushort[])) { ushort[] buffer = (ushort[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSUInt16Array.New(machine, buffer)); } else if (typeid == typeof(int[])) { int[] buffer = (int[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSInt32Array.New(machine, buffer)); } else if (typeid == typeof(uint[])) { uint[] buffer = (uint[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSUInt32Array.New(machine, buffer)); } else if (typeid == typeof(float[])) { float[] buffer = (float[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSFloat32Array.New(machine, buffer)); } else if (typeid == typeof(double[])) { double[] buffer = (double[])(object)value; if (buffer == null) { return(NSJSValue.Null(machine)); } return(NSJSFloat64Array.New(machine, buffer)); } return(NSJSValue.Null(machine)); }