public bool sendMsg(ToxId toxid, string msg) { // check if this message is send to itself if (toxid.ToString() == tox.Id.ToString()) { return(false); // this is not allowed } // wait toxcore online int maxOnlineWaitTime = 20000; // 20s int onlineWaitCount = 0; while (!tox.IsConnected) { Thread.Sleep(10); onlineWaitCount += 10; if (onlineWaitCount > maxOnlineWaitTime) { return(false); } } ToxKey toxkey = toxid.PublicKey; int friendNum = tox.GetFriendByPublicKey(toxkey); if (friendNum == -1) { int res = tox.AddFriend(toxid, "add friend"); if (res != (int)ToxErrorFriendAdd.Ok) { return(false); } friendNum = tox.GetFriendByPublicKey(toxkey); } int waitCount = 0; int maxCount = 500; if (connectedList.IndexOf(toxkey.GetString()) == -1) { maxCount = 200 * 1000; // first time wait for 200s } while (tox.GetFriendConnectionStatus(friendNum) == ToxConnectionStatus.None && waitCount < maxCount) { if (waitCount % 1000 == 0) { Console.WriteLine("target is offline." + waitCount / 1000); } waitCount += 10; Thread.Sleep(10); } if (waitCount == maxCount) { Console.WriteLine("Connect Failed"); connectedList.Remove(toxkey.GetString()); return(false); } connectedList.Add(toxkey.GetString()); int msgRes = tox.SendMessage(friendNum, msg, ToxMessageType.Message); return(msgRes > 0); }
public void TestToxFriendPublicKey() { var error = ToxErrorFriendGetPublicKey.Ok; var publicKey = _tox2.GetFriendPublicKey(0, out error); if (error != ToxErrorFriendGetPublicKey.Ok) { Assert.Fail("Could not get friend public key, error: {0}", error); } var error2 = ToxErrorFriendByPublicKey.Ok; int friend = _tox2.GetFriendByPublicKey(publicKey, out error2); if (friend != 0 || error2 != ToxErrorFriendByPublicKey.Ok) { Assert.Fail("Could not get friend by public key, error: {0}, friend: {1}", error2, friend); } }
public bool sendMsg(ToxId toxid, byte[] msg, int timeout = 20) { try { lock (sendLock) { // check if this message is send to itself if (toxid.ToString() == tox.Id.ToString()) { return(false); // this is not allowed } // wait toxcore online int maxOnlineWaitTime = 200000; // 200s int onlineWaitCount = 0; while (!tox.IsConnected) { Thread.Sleep(10); onlineWaitCount += 10; if (onlineWaitCount > maxOnlineWaitTime) { return(false); } } ToxKey toxkey = toxid.PublicKey; int friendNum = tox.GetFriendByPublicKey(toxkey); if (friendNum == -1) { int res = tox.AddFriend(toxid, "add friend"); if (res != (int)ToxErrorFriendAdd.Ok) { return(false); } friendNum = tox.GetFriendByPublicKey(toxkey); } if (tox.GetFriendConnectionStatus(friendNum) == ToxConnectionStatus.None) { return(false); } var mesError = new ToxErrorFriendCustomPacket(); // retry send message int retryCount = 0; while (retryCount < 60) { byte[] msgToSend = new byte[msg.Length + 1]; msgToSend[0] = 170; // The first byte must be in the range 160-191. msg.CopyTo(msgToSend, 1); bool msgRes = tox.FriendSendLosslessPacket(friendNum, msgToSend, out mesError); if (msgRes) { break; } Utils.Utils.Log("Event: " + mesError, true); Console.WriteLine(Utils.Utils.UnixTimeNow() + " Event: " + mesError); if (mesError == ToxErrorFriendCustomPacket.SendQ) { Thread.Sleep(20); continue; } retryCount++; Thread.Sleep(100); } if (retryCount == 60) { tox.DeleteFriend(friendNum); return(false); } return(true); } } catch (Exception e) { Utils.Utils.Log(e.StackTrace, true); return(false); } }
public bool sendMsg(ToxId toxid, byte[] msg) { lock (sendLock) { // check if this message is send to itself if (toxid.ToString() == tox.Id.ToString()) { return(false); // this is not allowed } // wait toxcore online int maxOnlineWaitTime = 20000; // 20s int onlineWaitCount = 0; while (!tox.IsConnected) { Thread.Sleep(10); onlineWaitCount += 10; if (onlineWaitCount > maxOnlineWaitTime) { return(false); } } ToxKey toxkey = toxid.PublicKey; int friendNum = tox.GetFriendByPublicKey(toxkey); if (friendNum == -1) { int res = tox.AddFriend(toxid, "add friend"); if (res != (int)ToxErrorFriendAdd.Ok) { return(false); } friendNum = tox.GetFriendByPublicKey(toxkey); } int waitCount = 0; int maxCount = 500; if (connectedList.IndexOf(toxkey.GetString()) == -1) { maxCount = 200 * 1000; // first time wait for 200s } while (tox.GetFriendConnectionStatus(friendNum) == ToxConnectionStatus.None && waitCount < maxCount) { if (waitCount % 1000 == 0) { Utils.Utils.LogUtils("Event: target is offline " + waitCount / 1000); } waitCount += 10; Thread.Sleep(10); } if (waitCount == maxCount) { Utils.Utils.LogUtils("Event: Connect Failed"); connectedList.Remove(toxkey.GetString()); return(false); } if (connectedList.IndexOf(toxkey.GetString()) == -1) { connectedList.Add(toxkey.GetString()); } var mesError = new ToxErrorFriendCustomPacket(); // retry send message int retryCount = 0; while (retryCount < 10) { byte[] msgToSend = new byte[msg.Length + 1]; msgToSend [0] = 170; // The first byte must be in the range 160-191. msg.CopyTo(msgToSend, 1); bool msgRes = tox.FriendSendLosslessPacket(friendNum, msgToSend, out mesError); if (msgRes) { break; } Utils.Utils.LogUtils("Event: " + mesError); if (mesError == ToxErrorFriendCustomPacket.SendQ) { Thread.Sleep(10); continue; } retryCount++; Thread.Sleep(100); } if (retryCount == 10) { return(false); } return(true); } }