Ejemplo n.º 1
0
        //public static void SendPacket_VF<T_Data>(this NetClient _Client, WLN_PacketType _PacketType, T_Data _Data, bool _Flush = true)
        //{
        //    NetOutgoingMessage newMessage = _Client.CreateMessage();
        //    newMessage.Write((byte)_PacketType);
        //    var memoryReturnBuffer = new System.IO.MemoryStream();
        //    Serializer.Serialize(memoryReturnBuffer, _Data);
        //    memoryReturnBuffer.Position = 0;
        //    byte[] returnBytes = new byte[memoryReturnBuffer.Length];
        //    memoryReturnBuffer.Read(returnBytes, 0, (int)memoryReturnBuffer.Length);
        //    newMessage.Write((int)returnBytes.Length);
        //    newMessage.Write(returnBytes);
        //    _Client.SendMessage(newMessage, NetDeliveryMethod.ReliableOrdered, 0);
        //    if (_Flush == true)
        //        _Client.FlushSendQueue();
        //}
        //public static void SendPacket_VF<T_Data>(this NetConnection _Connection, WLN_PacketType _PacketType, T_Data _Data)
        //{
        //    NetOutgoingMessage newMessage = _Connection.Peer.CreateMessage();
        //    newMessage.Write((byte)_PacketType);
        //    var memoryReturnBuffer = new System.IO.MemoryStream();
        //    Serializer.Serialize(memoryReturnBuffer, _Data);
        //    memoryReturnBuffer.Position = 0;
        //    byte[] returnBytes = new byte[memoryReturnBuffer.Length];
        //    memoryReturnBuffer.Read(returnBytes, 0, (int)memoryReturnBuffer.Length);
        //    newMessage.Write((int)returnBytes.Length);
        //    newMessage.Write(returnBytes);
        //    _Connection.SendMessage(newMessage, NetDeliveryMethod.ReliableOrdered, 0);
        //}
        //public static T_Data RecvPacketData_VF<T_Data>(this NetIncomingMessage _Message)
        //{
        //    int packetLength = _Message.ReadInt32();
        //    byte[] rawData = _Message.ReadBytes(packetLength);
        //    var memoryBuffer = new System.IO.MemoryStream(rawData);
        //    T_Data dataReceived = Serializer.Deserialize<T_Data>(memoryBuffer);
        //    return dataReceived;
        //}
        public static bool RecvPacket_VF <T_Data>(this VF.NetworkClient _Client, WLN_PacketType _PacketType, out T_Data _RetData, TimeSpan _Timeout)
        {
            DateTime startRecv = DateTime.Now;

            if (_Client.WaitForConnect(_Timeout) == false)
            {
                throw new Exception("Not Connected");
            }
            VF.NetworkIncommingMessage msg;
            while (_Client.IsConnected())
            {
                msg = _Client.WaitMessage(_Timeout);
                if (msg != null)
                {
                    if (msg.MessageType == VF.NetworkMessageType.RawData)
                    {
                        WLN_PacketType packetType = (WLN_PacketType)msg.ReadByte();
                        if (packetType == _PacketType)
                        {
                            _RetData = msg.ReadClass <T_Data>();
                            return(true);
                        }
                        else
                        {
                            _RetData = default(T_Data);
                            return(false);// throw new Exception("Unexpected Packet");
                        }
                    }
                }
                if ((DateTime.Now - startRecv) > _Timeout)
                {
                    throw new TimeoutException("Could not receive packet within the time period");
                }
            }
            _RetData = default(T_Data);
            throw new Exception("Not Connected");
        }
Ejemplo n.º 2
0
        internal static List <string> SendAddonData(string _UserID, string _AddonName, WowVersionEnum _WowVersion, string _ClearLuaVariableName, int _LuaVariableDataLengthThreshold, out bool _SentAll)
        {
            var savedVariableFiles = WowUtility.GetSavedVariableFilePaths(_AddonName, _WowVersion);//For Accounts only

            _LuaVariableDataLengthThreshold = _LuaVariableDataLengthThreshold + 12 /*newlines = {} osv osv*/ + _ClearLuaVariableName.Length;
            VF.NetworkClient netClient = sm_Client;
            if (netClient != null && netClient.IsConnected() == false)
            {
                netClient.Disconnect();
                netClient = null;
            }
            List <string> sentAddonFiles = new List <string>();

            _SentAll = true;//Default läge
            try
            {
                foreach (var luaFilePath in savedVariableFiles)
                {
                    string luaFileData    = System.IO.File.ReadAllText(luaFilePath);
                    string resultFileData = "";
                    string variableData   = "";
                    if (WowUtility.ExtractLuaVariableFromFile(luaFileData, _ClearLuaVariableName, out resultFileData, out variableData) == true)
                    {
                        if (variableData.Length < _LuaVariableDataLengthThreshold)
                        {
                            continue;
                        }
                        if (netClient == null)
                        {
                            Logger.ConsoleWriteLine("Trying to connect to server...", ConsoleColor.Gray);
                            netClient = new VF.NetworkClient(g_Host, g_Port); //realmplayers.com
                            Logger.ConsoleWriteLine("Connected to server!", ConsoleColor.Green);
                        }

                        WLN_UploadPacket_AddonData addonData = new WLN_UploadPacket_AddonData();
                        addonData.AddonName  = _AddonName;
                        addonData.UserID     = _UserID;
                        addonData.FileStatus = WowUtility.GetFileStatus(luaFilePath);
                        addonData.Data       = luaFileData;
                        var newMessage = netClient.CreateMessage();
                        newMessage.WriteByte((byte)WLN_PacketType.Upload_AddonData);
                        newMessage.WriteClass(addonData);
                        netClient.WaitForConnect(TimeSpan.FromSeconds(60));
                        netClient.SendMessage(newMessage);
                        Logger.ConsoleWriteLine("Sent SavedVariables file \"" + luaFilePath + "\". Waiting for Response...", ConsoleColor.Gray);
                        WLN_UploadPacket_SuccessResponse response;
                        if (netClient.RecvPacket_VF(WLN_PacketType.Upload_SuccessResponse, out response, TimeSpan.FromSeconds(60)) == true)
                        {
                            Logger.ConsoleWriteLine("Received Response. Data was sent successfully!. Deleting old data", ConsoleColor.Gray);
                            sentAddonFiles.Add(luaFilePath);
                            if (response.MessageToUser != "")
                            {
                                Utility.MessageBoxShow(response.MessageToUser, "Message from Server");
                            }
                            //TODO: Do something with "response.MessageToUser"
                            System.IO.File.WriteAllText(luaFilePath, resultFileData); //Save the lua file with the variable cleared

                            Logger.ConsoleWriteLine("Operation Successfull! Preparing for next file.", ConsoleColor.Green);
                        }
                        else
                        {
                            Logger.ConsoleWriteLine("Operation Failed! Preparing for next file.", ConsoleColor.Red);
                            _SentAll = false;
                        }
                    }
                    else
                    {
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                _SentAll = false;
            }
            finally
            {
                if (_SentAll == false)
                {
                    if (netClient != null)
                    {
                        netClient.Disconnect();
                        netClient = null;
                    }
                }
                sm_Client = netClient;
            }
            return(sentAddonFiles);
        }