public void sendRequestNoReplay(ToxId toxid, ToxRequest req, out bool status) { status = true; try { if (toxid.ToString() == tox.Id.ToString()) { // request was sent to itself status = true; } } catch (ObjectDisposedException) { status = false; return; } byte[] reqContent = req.getBytes(); int packageNum = reqContent.Length / MAX_MSG_LENGTH + 1; bool res = false; for (int i = 0; i < packageNum; i++) { byte[] mcontent = null; 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; } } }
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)); }