Ejemplo n.º 1
0
    public static void ConvertToUTF8(string filePath)
    {
        filePath = filePath.ToLower();

        if (filePath.EndsWith(".meta"))
        {
            return;
        }

        if (filePath.EndsWith(".txt") || filePath.EndsWith(".csv") || filePath.EndsWith(".xml") || filePath.EndsWith(".lua"))
        {
            byte[]   data     = File.ReadAllBytes(filePath);
            TextType textType = GetEncodingType(data);
            switch (textType)
            {
            case TextType.Default: data = Encoding.Convert(Encoding.Default, Encoding.UTF8, data); break;

            case TextType.UTF8_Without_BOM: return;

            case TextType.UTF8_BOM:
            {
                BufferWriter writer = new BufferWriter(data.Length - 3);
                writer.Write(data, 3, data.Length - 3);
                data = writer.GetBuffer();
            }
            break;

            case TextType.Unicode: data = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, data); break;

            case TextType.Big_Unicode: data = Encoding.Convert(Encoding.BigEndianUnicode, Encoding.UTF8, data); break;
            }

            File.WriteAllBytes(filePath, data);
        }
    }
Ejemplo n.º 2
0
            public SendTask()
                : base(300)/*间隔300毫秒一直执行的定时器任务*/
            {
                BufferWriter bw = new BufferWriter();

                bw.Write(SzExtensions.GetId());/*发送同步消息唯一id*/
                msg = bw.GetBuffer();
                tmp = new List <IOSession>(NettyPool.Sessions.Values);
            }
Ejemplo n.º 3
0
            public void ChannelRead(IOSession session, int msgId, byte[] buffer)
            {
                /*收到服务器发送来的消息*/
                BufferReader br = new BufferReader(buffer);

                if (msgId == 2)
                {
                    /*消息唯一回复id*/
                    long mmid = br.ReadInt64();
                    /*收到消息回复消息处理*/
                    log.Error("收到服务器消息:" + session.ID + "  消息唯一回复id" + mmid);

                    BufferWriter bw = new BufferWriter();
                    bw.Write(mmid);
                    session.WriteAndFlush(255, bw.GetBuffer());
                }
            }
Ejemplo n.º 4
0
        public void WriteAndFlush(int msgId, byte[] buf)
        {
            BufferWriter outBuf = new BufferWriter();

            this.mershaEncoder.Encoder(this, msgId, buf, outBuf);
            byte[] buffer = outBuf.GetBuffer();

            try
            {
                if (!this.IsDispose)
                {
                    //封包
                    int size = this._Socket.Send(buffer, 0, buffer.Length, SocketFlags.None, out SenderError);
                    if (log.IsDebugEnabled())
                    {
                        log.Debug("发送消息Length:" + buf.Length + " flush:" + size);
                    }
                    CheckSocketError(SenderError);
                    LastRecvTime = Utils.TimeUtil.CurrentTimeMillis();
                }
            }
            catch (System.ObjectDisposedException) { this.Close("链接已经被关闭"); }
            catch (System.Net.Sockets.SocketException) { this.Close("链接已经被关闭"); }
            catch (Exception ex)
            {
                if (isServer)
                {
                    NettyPool.SessionHandler.ExceptionCaught(this, ex);
                }
                else
                {
                    NettyPool.ClientSessionHandler.ExceptionCaught(this, ex);
                }
            }
            finally
            {
                outBuf.Close();
                outBuf.Dispose();
            }
        }
Ejemplo n.º 5
0
    public static void Write(string key, object value, EStorageType ePath)
    {
        if (value != null)
        {
            BufferWriter buf = new BufferWriter();
            buf.WriteObject(value);
            Fragment[] fragment;
            byte[]     bytes = buf.GetBuffer(out fragment);

            //整个这段是加密
            {
                MemoryStream ms = new MemoryStream();
                for (int i = 0; i < fragment.Length; ++i)
                {
                    ms.Write(bytes, fragment[i].begin, fragment[i].length);
                }
                ms.Flush();
                bytes = ms.ToArray();
                var newb = new byte[bytes.Length];
                Array.Copy(bytes, newb, ms.Length);

                //加密保存
                //bytes = FileUtility.CopyFrom(newb, 5);

                fragment           = new Fragment[1];
                fragment[0].begin  = 0;
                fragment[0].length = bytes.Length;
            }

            try
            {
                if (bytes != null && fragment.Length > 0)
                {
                    using (FileStream stm = new FileStream(GetStoragePath(ePath) + "/" + key, FileMode.Create))
                    {
                        for (int i = 0; i < fragment.Length; ++i)
                        {
                            stm.Write(bytes, fragment[i].begin, fragment[i].length);
                        }
                        stm.Flush();
                        stm.Close();
                    }
                }
            }
            catch (IOException e)
            {
                if (e.Message.Contains("Disk full"))
                {
                    Debug.LogError("存储空间不足,请清理存储空间!");
                    //UITip.ShowInfo(G.R(""), UITip.TipsType.Error);
                }
                else
                {
                    throw;
                }
            }
        }
        else
        {
            string path = GetStoragePath(ePath) + "/" + key;
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
    }