public void Init(Syscalls syscalls, Core core, Runtime runtime) { runtime.RegisterCleaner(delegate() { foreach (KeyValuePair <int, Connection> p in mConnections) { p.Value.close(); } mConnections.Clear(); }); mResultHandler = delegate(int handle, int connOp, int result) { Memory evt = new Memory(4 * 4); evt.WriteInt32(MoSync.Struct.MAEvent.type, MoSync.Constants.EVENT_TYPE_CONN); evt.WriteInt32(MoSync.Struct.MAEvent.conn.handle, handle); evt.WriteInt32(MoSync.Struct.MAEvent.conn.opType, connOp); evt.WriteInt32(MoSync.Struct.MAEvent.conn.result, result); runtime.PostEvent(new Event(evt)); }; syscalls.maConnect = delegate(int _url) { String url = core.GetDataMemory().ReadStringAtAddress(_url); //Util.Log("maConnect(" + url + ")\n"); if (url.StartsWith("btspp")) { return(MoSync.Constants.CONNERR_UNAVAILABLE); } Uri uri = new Uri(url); Connection c; if (uri.Scheme.Equals("socket")) { c = new SocketConnection(uri, mNextConnHandle); } else if (uri.Scheme.Equals("http") || uri.Scheme.Equals("https")) { c = new WebRequestConnection(uri, mNextConnHandle, MoSync.Constants.HTTP_GET); } else { return(MoSync.Constants.CONNERR_GENERIC); } c.connect(mResultHandler); mConnections.Add(mNextConnHandle, c); return(mNextConnHandle++); }; syscalls.maConnClose = delegate(int _conn) { Connection c = mConnections[_conn]; c.close(); mConnections.Remove(_conn); }; syscalls.maConnGetAddr = delegate(int _conn, int _addr) { if (_conn == MoSync.Constants.HANDLE_LOCAL) // unavailable { return(-1); } Connection c = mConnections[_conn]; return(c.getAddr(core.GetDataMemory(), _addr)); }; syscalls.maConnRead = delegate(int _conn, int _dst, int _size) { Connection c = mConnections[_conn]; c.recv(core.GetDataMemory().GetData(), _dst, _size, mResultHandler); }; DataDelegate dataDelegate = delegate(int _conn, int _data, CommDelegate cd) { Connection c = mConnections[_conn]; Resource res = runtime.GetResource(MoSync.Constants.RT_BINARY, _data); Stream s = (Stream)res.GetInternalObject(); runtime.SetResourceRaw(_data, Resource.Flux); MemoryStream mem = null; if (s.GetType() == typeof(MemoryStream)) { mem = (MemoryStream)s; } else { MoSync.Util.CriticalError("Only binaries (non-ubins) are allowed for maConn(Read/Write)(To/From)Data"); } cd(c, mem.GetBuffer(), delegate(int handle, int connOp, int result) { runtime.SetResourceRaw(_data, res); mResultHandler(handle, connOp, result); }); }; syscalls.maConnReadToData = delegate(int _conn, int _data, int _offset, int _size) { dataDelegate(_conn, _data, delegate(Connection c, byte[] buf, ResultHandler rh) { c.recv(buf, _offset, _size, rh); }); }; syscalls.maConnWrite = delegate(int _conn, int _src, int _size) { Connection c = mConnections[_conn]; c.write(core.GetDataMemory().GetData(), _src, _size, mResultHandler); }; syscalls.maConnWriteFromData = delegate(int _conn, int _data, int _offset, int _size) { dataDelegate(_conn, _data, delegate(Connection c, byte[] buf, ResultHandler rh) { c.write(buf, _offset, _size, rh); }); }; syscalls.maHttpCreate = delegate(int _url, int _method) { String url = core.GetDataMemory().ReadStringAtAddress(_url); //Util.Log("maHttpCreate(" + url + ")\n"); Uri uri = new Uri(url); WebRequestConnection c = new WebRequestConnection(uri, mNextConnHandle, _method); mConnections.Add(mNextConnHandle, c); return(mNextConnHandle++); }; syscalls.maHttpFinish = delegate(int _conn) { WebRequestConnection c = (WebRequestConnection)mConnections[_conn]; c.connect(delegate(int handle, int connOp, int result) { mResultHandler(handle, MoSync.Constants.CONNOP_FINISH, result); }); }; syscalls.maHttpSetRequestHeader = delegate(int _conn, int _key, int _value) { WebRequestConnection c = (WebRequestConnection)mConnections[_conn]; String key = core.GetDataMemory().ReadStringAtAddress(_key); String value = core.GetDataMemory().ReadStringAtAddress(_value); if (value.Length > 0) { c.setRequestHeader(key, value); } }; syscalls.maHttpGetResponseHeader = delegate(int _conn, int _key, int _buffer, int _bufSize) { WebRequestConnection c = (WebRequestConnection)mConnections[_conn]; String key = core.GetDataMemory().ReadStringAtAddress(_key); String value = c.getResponseHeader(key); if (value == null) { return(MoSync.Constants.CONNERR_NOHEADER); } if (value.Length + 1 <= _bufSize) { core.GetDataMemory().WriteStringAtAddress(_buffer, value, _bufSize); } return(value.Length); }; }