Example #1
0
 internal static void InvokeSentData(int packetType, object text, int number1, float number2, float number3,
                                     float number4, int number5, int number6, int number7)
 {
     SentData?.Invoke(null,
                      new SentDataEventArgs(packetType, (NetworkText)text, number1, number2, number3, number4, number5,
                                            number6, number7));
 }
Example #2
0
    void UpdatePlayers()
    {
        if (lastestGameState.players.Length > 0)
        {
            foreach (NetworkMan.Player player in lastestGameState.players)
            {
                string playerID = player.id;
                //currentPlayers[player.id].GetComponent<Renderer>().material.color = new Color(player.color.R, player.color.G, player.color.B);
                if (player.id != myAddress)
                {
                    currentPlayers[player.id].GetComponent <Transform>().position = new Vector3(player.pos.X, player.pos.Y, player.pos.Z);
                }
            }
            foreach (Player player in lastestGameState.players)
            {
                if (player.id == myAddress)
                {
                    SentData playerData = new SentData();
                    playerData.posData.X = currentPlayers[player.id].GetComponent <Transform>().position.x;
                    playerData.posData.Y = currentPlayers[player.id].GetComponent <Transform>().position.y;
                    playerData.posData.Z = currentPlayers[player.id].GetComponent <Transform>().position.z;

                    string json     = JsonUtility.ToJson(playerData);
                    Byte[] sendJson = Encoding.UTF8.GetBytes(json);
                    udp.Send(sendJson, sendJson.Length);
                }
            }
            lastestGameState.players = new Player[0];
        }
    }
Example #3
0
        public void OnSentData(byte[] data)
        {
            SentDataEventArgs sentArgs = new SentDataEventArgs();

            sentArgs.SentData = data;
            SentData?.Invoke(this, sentArgs);
        }
Example #4
0
 private object ExtractDataFromString(string msg, SentData type)
 {
     if (type == SentData.Vector3)
     {
         if (msg.Count(f => f == '/') == 2)
         {
             string[] extracted = msg.Split('/');
             return(new Vector3((float)ExtractDataFromString(extracted[0], SentData.Float), (float)ExtractDataFromString(extracted[1], SentData.Float), (float)ExtractDataFromString(extracted[2], SentData.Float)));
         }
     }
     else if (type == SentData.Float)
     {
         float ret;
         if (float.TryParse(msg.Replace(',', '.'), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out ret))
         {
             return(ret);
         }
         else
         {
             return(0f);
         }
     }
     else if (type == SentData.Bool)
     {
         return(msg == "1" || msg == "true");
     }
     else if (type == SentData.Int)
     {
         int ret;
         if (Int32.TryParse(msg, out ret))
         {
             return(ret);
         }
         else
         {
             return(0);
         }
     }
     return(null);
 }
        public void SendToTarget()
        {
            var SendWait = new ManualResetEvent(false);

            while (!Cancel.IsCancellationRequested)
            {
                RecvedData.WaitOne(-1);;
                RecvedData.Reset();
                foreach (var key in Targets.Keys)
                {
                    TargetInfo trget = null;
                    try
                    {
                        if (Targets.ContainsKey(key))
                        {
                            trget = Targets[key];
                        }

                        if (null != trget && !trget.Exit)
                        {
                            List <byte> readPyld = null;

                            var payload = new List <byte>();
                            while (trget.ReadQueue.Count > 0)
                            {
                                trget.ReadQueue.TryDequeue(out readPyld);
                                payload.AddRange(readPyld);
                            }
                            if (payload.Count > 0)
                            {
                                Task.Factory.StartNew(() =>
                                {
                                    List <byte> toSend = null;
                                    try
                                    {
                                        toSend = CmdCommshandler.Send(trget, "asyncUpload", payload, out bool connectionDead);
                                        ImplantComms.LogMessage($"[{trget.TargetId}] Received {toSend?.Count() ?? 0} bytes after sending {payload?.Count ?? 0 } bytes");
                                        if (null == toSend || connectionDead)
                                        {
                                            ImplantComms.LogError($"[{trget.TargetId}] Connection looks dead EXITING");
                                            trget.Exit = true;
                                        }
                                        else if (toSend.Count > 0)
                                        {
                                            trget.WriteQueue.Enqueue(toSend);
                                            SentData.Set();
                                        }
                                    }
                                    catch
                                    {
                                        trget.Exit = true;
                                        ImplantComms.LogError($"[{trget.TargetId}] Couldn't send {toSend?.Count()} bytes");
                                    }
                                });
                                ImplantComms.LogMessage($"[{trget.TargetId}] {payload?.Count() ?? 0} bytes arrived from target about to send back");
                            }
                        }
                        SendWait.WaitOne(BeaconTime);
                    }
                    catch (Exception ex)
                    {
                        if (null != trget)
                        {
                            trget.Exit = true;
                        }
                        ImplantComms.LogError($"[{trget.TargetId}] Error during Send Data loop {ex}");
                    }
                }
            }
        }
        public void WriteToSocket()
        {
            while (!Cancel.IsCancellationRequested)
            {
                SentData.WaitOne(-1);
                SentData.Reset();
                foreach (var key in Targets.Keys)
                {
                    TargetInfo    trget  = null;
                    NetworkStream stream = null;
                    try
                    {
                        if (Targets.ContainsKey(key))
                        {
                            trget = Targets[key];
                        }

                        if (null != trget && !trget.Exit)
                        {
                            stream = trget.TargetTcpClient.GetStream();
                            if (trget.WriteQueue.Count > 0)
                            {
                                var         toSend = new List <byte>();
                                List <byte> pyld   = null;
                                while (trget.WriteQueue.Count() > 0)
                                {
                                    trget.WriteQueue.TryDequeue(out pyld);
                                    if (pyld.Count > 0)
                                    {
                                        toSend.AddRange(pyld);
                                    }
                                }
                                if (IsConnected(trget.TargetTcpClient.Client))
                                {
                                    if (toSend != null && toSend.Count() > 0)
                                    {
                                        TotalBytesWritten += toSend.Count;
                                        stream.Write(toSend.ToArray(), 0, toSend.Count());
                                        stream.Flush();
                                        ImplantComms.LogMessage($"[{trget.TargetId}] Written {toSend.Count()} from client");
                                    }
                                }
                                else
                                if (null != trget)
                                {
                                    trget.Exit = true;
                                }
                            }
                        }
                    }
                    catch
                    {
                        if (null != trget)
                        {
                            trget.Exit = true;
                        }
                    }

                    if (!trget?.TargetTcpClient?.Connected ?? false || (trget?.Exit ?? true))
                    {
                        try { if (null != stream)
                              {
                                  stream.Close();
                              }
                        }
                        catch { /*Dont relly care if exception thrown here*/ }

                        try { if (null != trget?.TargetTcpClient)
                              {
                                  trget.TargetTcpClient.Close();
                              }
                        }
                        catch { /*Dont relly care if exception thrown here*/ }
                        if (Targets.ContainsKey(key))
                        {
                            Targets.TryRemove(key, out TargetInfo tgexit);
                        }
                        ImplantComms.LogMessage($"[{trget.TargetId}] Connection has been closed");
                    }
                }
            }
        }