protected virtual void HandlePackage(byte[] buffer) { MemoryStream ms = new MemoryStream(buffer); CytarStreamReader cr = new CytarStreamReader(ms); var cid = cr.ReadInt32(); string apiName = cr.ReadString(); if (apiName == APIReturnHandlerAPI) { var returnID = cr.ReadInt32(); OnAPIReturn(returnID, ms); return; } else if (apiName == ErrorHandlerAPI) { OnErrorCallback(cid, cr.ReadObject <RemoteException>()); return; } try { var(result, isVoid) = CallAPI(apiName, ms); CallRemoteAPI(APIReturnHandlerAPI, cid, result); } catch (Exception ex) { CallRemoteAPI(ErrorHandlerAPI, new RemoteException(ex.Message)); } }
public static object Deserialize(Type type, byte[] data) { using (MemoryStream ms = new MemoryStream(data)) using (CytarStreamReader cr = new CytarStreamReader(ms)) { return(cr.ReadObject(type)); } }
public virtual void OnAPIReturn(int cid, Stream returnStream) { if (!RemoteCallingRecode.ContainsKey(cid)) { return; } CytarStreamReader cr = new CytarStreamReader(returnStream); if (RemoteCallingRecode[cid].ReturnType == typeof(void)) { RemoteCallingRecode[cid].Return(null); } else { RemoteCallingRecode[cid].Return(cr.ReadObject(RemoteCallingRecode[cid].ReturnType)); } RemoteCallingRecode.Remove(cid); }
public virtual (object, bool) CallAPI(string apiName, Stream stream) { var api = GetAPI(apiName); var paramsList = new List <object>(); CytarStreamReader cr = new CytarStreamReader(stream); foreach (var paramType in api.Parameters) { if (paramType.IsSubclassOf(typeof(Session))) { paramsList.Add(this); } else { paramsList.Add(cr.ReadObject(paramType)); } } return(api.Method.Invoke(api.APIContext, paramsList.ToArray()), api.Method.ReturnType == typeof(void)); }