Exemple #1
0
        public void WriteBytes(byte[] v)
        {
            int len = GFWEncoding.SwapInt32(v.Length);

            this.writer.Write(len);
            this.writer.Write(v);
        }
Exemple #2
0
        public void WriteString(string v)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(v);
            ushort len   = GFWEncoding.SwapUInt16((ushort)bytes.Length);

            this.writer.Write(len);
            this.writer.Write(bytes);
        }
        private void SendData()
        {
            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            while (this.mConnectState)
            {
                try
                {
                    if (this.mSendBuffers.Count == 0)
                    {
                        Thread.Sleep(1);
                        continue;
                    }
                    ByteBuffer buffer = null;
                    lock (this.mSendBuffers)
                    {
                        buffer = this.mSendBuffers.Dequeue();
                    }
                    if (buffer != null)
                    {
                        if (this.mSock != null && this.mSock.Connected)
                        {
                            byte[] message = buffer.ToBytes();
                            ms.SetLength(0L);
                            ms.Position = 0L;
                            ushort msglen = GFWEncoding.SwapUInt16((ushort)(message.Length + 4));
                            writer.Write(msglen);
                            ushort flag = GFWEncoding.SwapUInt16(1000);
                            writer.Write(flag);
                            writer.Write(message);
                            writer.Flush();
                            this.mSock.Send(ms.ToArray());
                        }
                        else
                        {
                            this.mConnectState = false;
                        }
                        buffer.Close();
                        buffer = null;
                    }
                }
                catch (Exception ex)
                {
                    Debug.LogWarning("Socket SendData Error:" + ex.Message);
                }
                Thread.Sleep(1);
            }
        }
 private void OnReceive(byte[] bytes, int length)
 {
     this.readStream.Seek(0L, SeekOrigin.End);
     this.readStream.Write(bytes, 0, length);
     this.readStream.Seek(0L, SeekOrigin.Begin);
     while (this.readStream.Length - this.readStream.Position > 4L)
     {
         uint messageLen = this.reader.ReadUInt32();
         uint len        = GFWEncoding.SwapUInt32(messageLen);
         len -= 4u;
         if (!(this.readStream.Length - this.readStream.Position >= (long)((ulong)len)))
         {
             this.readStream.Position = this.readStream.Position - 4L;
             break;
         }
         ByteBuffer buffer = new ByteBuffer(this.reader.ReadBytes((int)len));
         DispatchMessage(Protocal.Message, buffer);
     }
     byte[] leftover = this.reader.ReadBytes((int)(this.readStream.Length - this.readStream.Position));
     this.readStream.SetLength(0L);
     this.readStream.Write(leftover, 0, leftover.Length);
 }
Exemple #5
0
 public void WriteUlong(ulong v)
 {
     v = GFWEncoding.SwapUInt64(v);
     this.writer.Write(v);
 }
Exemple #6
0
 public void WriteLong(long v)
 {
     v = GFWEncoding.SwapInt64(v);
     this.writer.Write(v);
 }
Exemple #7
0
 public void WriteInt(int v)
 {
     v = GFWEncoding.SwapInt32(v);
     writer.Write(v);
 }
Exemple #8
0
 public void WriteUint(uint v)
 {
     v = GFWEncoding.SwapUInt32(v);
     this.writer.Write(v);
 }
Exemple #9
0
 public void WriteShort(short v)
 {
     v = GFWEncoding.SwapInt16(v);
     writer.Write(v);
 }
Exemple #10
0
 public void WriteUshort(ushort v)
 {
     v = GFWEncoding.SwapUInt16(v);
     writer.Write(v);
 }
Exemple #11
0
 public ulong ReadUlong()
 {
     return(GFWEncoding.SwapUInt64(this.reader.ReadUInt64()));
 }
Exemple #12
0
 public long ReadLong()
 {
     return(GFWEncoding.SwapInt64(this.reader.ReadInt64()));
 }
Exemple #13
0
 public uint ReadUint()
 {
     return(GFWEncoding.SwapUInt32(this.reader.ReadUInt32()));
 }
Exemple #14
0
 public int ReadInt()
 {
     return(GFWEncoding.SwapInt32(this.reader.ReadInt32()));
 }
Exemple #15
0
 public ushort ReadUshort()
 {
     return(GFWEncoding.SwapUInt16(this.reader.ReadUInt16()));
 }
Exemple #16
0
 public short ReadShort()
 {
     return(GFWEncoding.SwapInt16(this.reader.ReadInt16()));
 }