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); } }
internal static extern bool FriendSendLosslessPacket(ToxHandle tox, uint friendNumber, byte[] data, uint length, ref ToxErrorFriendCustomPacket error);
internal static extern bool FriendSendLossyPacket(ToxHandle tox, uint friendNumber, byte[] data, uint length, ref ToxErrorFriendCustomPacket error);
/// <summary> /// Sends a custom lossy packet to a friend. /// Lossy packets are like UDP packets, they may never reach the other side, arrive more than once or arrive in the wrong order. /// </summary> /// <param name="friendNumber">The friend to send the packet to.</param> /// <param name="data">The data to send. The first byte must be in the range 200-254. The maximum length of the data is ToxConstants.MaxCustomPacketSize</param> /// <param name="error"></param> /// <returns>True on success.</returns> public bool FriendSendLossyPacket(int friendNumber, byte[] data, out ToxErrorFriendCustomPacket error) { ThrowIfDisposed(); if (data == null) throw new ArgumentNullException("data"); error = ToxErrorFriendCustomPacket.Ok; return ToxFunctions.FriendSendLossyPacket(_tox, (uint)friendNumber, data, (uint)data.Length, ref error); }
public static extern bool SendLosslessPacket(ToxHandle tox, UInt32 friendNumber, Byte[] data, SizeT length, ref ToxErrorFriendCustomPacket error);
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); } }