void newReqReceived(Package receivedPackage, int friendNum) { string mcontentCache = ""; if (mPackageCache.ContainsKey(receivedPackage.uuid)) { mcontentCache = mPackageCache[receivedPackage.uuid].content; } mcontentCache += receivedPackage.content; // check if this is a response if (mPendingReqList.ContainsKey(receivedPackage.uuid)) { mPendingReqList[receivedPackage.uuid](JsonConvert.DeserializeObject <ToxResponse>(mcontentCache)); mPendingReqList.Remove(receivedPackage.uuid); return; } ToxRequest newReq = JsonConvert.DeserializeObject <ToxRequest>(mcontentCache); Task.Factory.StartNew(async() => { // send response to http server Console.WriteLine("new Req: " + JsonConvert.SerializeObject(newReq)); ToxResponse mRes = await RequestProxy.sendRequest(this, newReq); sendResponse(mRes, tox.GetFriendPublicKey(friendNum)); }); }
public Task <ToxResponse> sendRequest(ToxId toxid, ToxRequest req, out bool status) { if (toxid.ToString() == tox.Id.ToString()) { // request was sent to itself status = true; return(RequestProxy.sendRequest(this, req)); } string reqContent = JsonConvert.SerializeObject(req); int packageNum = reqContent.Length / MAX_MSG_LENGTH + 1; bool res = false; for (int i = 0; i < packageNum; i++) { string mcontent = ""; if (i * MAX_MSG_LENGTH + MAX_MSG_LENGTH > reqContent.Length) { mcontent = reqContent.Substring(i * MAX_MSG_LENGTH); } else { mcontent = reqContent.Substring(i * MAX_MSG_LENGTH, MAX_MSG_LENGTH); } res = sendMsg(toxid, JsonConvert.SerializeObject(new Package { uuid = req.uuid, totalCount = packageNum, currentCount = i, content = mcontent, })); if (!res) { status = false; return(Task.Factory.StartNew <ToxResponse>(() => { return null; })); } } status = res; bool isResponseReceived = false; ToxResponse mRes = null; if (res) { mPendingReqList.Add(req.uuid, (response) => { isResponseReceived = true; mRes = response; }); } return(Task.Factory.StartNew(() => { while (!isResponseReceived) { Thread.Sleep(10); } return mRes; })); }
public Task <ToxResponse> sendRequest(ToxId toxid, ToxRequest req, out bool status, int timeout = 200) { try { if (toxid.ToString() == tox.Id.ToString()) { // request was sent to itself status = true; return(RequestProxy.sendRequest(this, req)); } } catch (ObjectDisposedException) { status = false; return(Task.Factory.StartNew <ToxResponse>(() => { return null; }, TaskCreationOptions.LongRunning)); } byte[] reqContent = req.getBytes(); int packageNum = reqContent.Length / MAX_MSG_LENGTH + 1; bool res = false; ToxResponse mRes = null; object reslock = new object(); bool resFlag = false; lock (mPendingReqLock) { mPendingReqList.Add(req.uuid, (response) => { mRes = response; lock (reslock) { resFlag = true; Utils.Utils.Log("Event: Callback called, ReqId: " + req.uuid); Monitor.PulseAll(reslock); Utils.Utils.Log("Event: Pulse Lock, ReqId: " + req.uuid); } }); } for (int i = 0; i < packageNum; i++) { byte[] mcontent; if (i * MAX_MSG_LENGTH + MAX_MSG_LENGTH > reqContent.Length) { mcontent = Utils.Utils.subArray(reqContent, i * MAX_MSG_LENGTH); } else { mcontent = Utils.Utils.subArray(reqContent, i * MAX_MSG_LENGTH, MAX_MSG_LENGTH); } res = sendMsgDebug(toxid, new Package { uuid = req.uuid, totalCount = packageNum, currentCount = i, content = mcontent, totalSize = (uint)reqContent.Length, startIndex = (uint)(i * MAX_MSG_LENGTH), }, timeout); if (!res) { status = false; return(Task.Factory.StartNew <ToxResponse>(() => { lock (mPendingReqLock) { mPendingReqList.Remove(req.uuid); } return null; }, TaskCreationOptions.LongRunning)); } } status = res; Utils.Utils.Log("Event: return async, ReqId: " + req.uuid); return(Task.Factory.StartNew(() => { Task.Run(() => { // timeout count thread int timeoutCount = 0; while (timeoutCount < timeout * 1000) { Thread.Sleep(1); timeoutCount += 1; lock (mPendingReqLock) { if (!mPendingReqList.Keys.Contains(req.uuid)) { // already received response return; } } } Console.WriteLine(Utils.Utils.UnixTimeNow() + " Timeout Happends ReqId: " + req.uuid); lock (mPendingReqLock) { if (mPendingReqList.Keys.Contains(req.uuid)) { mRes = null; mPendingReqList.Remove(req.uuid); lock (reslock) { resFlag = true; Utils.Utils.Log("Event: Callback Timeout, ReqId: " + req.uuid); Monitor.PulseAll(reslock); Utils.Utils.Log("Event: Pulse Lock, ReqId: " + req.uuid); } } } }); Utils.Utils.Log("Event: Response locked, ReqId: " + req.uuid); lock (reslock) { while (!resFlag) { Monitor.Wait(reslock); } } Utils.Utils.Log("Event: Response unlocked, ReqId: " + req.uuid); return mRes; }, TaskCreationOptions.LongRunning)); }
public Task <ToxResponse> sendRequest(ToxId toxid, ToxRequest req, out bool status) { if (toxid.ToString() == tox.Id.ToString()) { // request was sent to itself status = true; return(RequestProxy.sendRequest(this, req)); } byte[] reqContent = req.getBytes(); int packageNum = reqContent.Length / MAX_MSG_LENGTH + 1; bool res = false; ToxResponse mRes = null; object reslock = new object(); bool resFlag = false; lock (mPendingReqLock) { mPendingReqList.Add(req.uuid, (response) => { mRes = response; lock (reslock) { resFlag = true; Utils.Utils.LogUtils("Event: Callback called, ReqId: " + req.uuid); Monitor.PulseAll(reslock); Utils.Utils.LogUtils("Event: Pulse Lock, ReqId: " + req.uuid); } }); } for (int i = 0; i < packageNum; i++) { byte[] mcontent; if (i * MAX_MSG_LENGTH + MAX_MSG_LENGTH > reqContent.Length) { mcontent = Utils.Utils.subArray(reqContent, i * MAX_MSG_LENGTH); } else { mcontent = Utils.Utils.subArray(reqContent, i * MAX_MSG_LENGTH, MAX_MSG_LENGTH); } res = sendMsg(toxid, new Package { uuid = req.uuid, totalCount = packageNum, currentCount = i, content = mcontent, totalSize = (uint)reqContent.Length, startIndex = (uint)(i * MAX_MSG_LENGTH), }.toBytes()); if (!res) { status = false; return(Task.Factory.StartNew <ToxResponse> (() => { mPendingReqList.Remove(req.uuid); return null; }, TaskCreationOptions.LongRunning)); } } status = res; Utils.Utils.LogUtils("Event: return async, ReqId: " + req.uuid); return(Task.Factory.StartNew(() => { Utils.Utils.LogUtils("Event: Response locked, ReqId: " + req.uuid); lock (reslock) { while (!resFlag) { Monitor.Wait(reslock); } } Utils.Utils.LogUtils("Event: Response unlocked, ReqId: " + req.uuid); return mRes; }, TaskCreationOptions.LongRunning)); }