public override void invokeRead(object message)
        {
            //base.invokeRead (message);
            ByteBuf  buf      = (ByteBuf)message;
            Encoding encoding = System.Text.UTF8Encoding.Default;

            System.Console.WriteLine("Not Implemented buffer " + encoding.GetString(buf.getBuffer(), buf.getReaderIndex(), buf.getWriterIndex()));
        }
Ejemplo n.º 2
0
 public void writeBytes(ByteBuf buf, int maxSize)
 {
     if (buf.remainBytes() > maxSize)
     {
         writeBytes(buf.getBuffer(), buf.getReaderIndex(), maxSize);
         buf.skipReaderIndex(maxSize);
     }
     else
     {
         writeBytes(buf);
     }
 }
Ejemplo n.º 3
0
        public void writeBytes(ByteBuf buf)
        {
            int maxSize     = getCapacity() - writerIndex;
            int remainBytes = buf.remainBytes();

            if (remainBytes < maxSize)
            {
                maxSize = remainBytes;
            }
            writeBytes(buf.getBuffer(), buf.getReaderIndex(), maxSize);
            buf.skipReaderIndex(maxSize);
        }
Ejemplo n.º 4
0
        public override void read(ConnectionHandlerContext ctx, object msg)
        {
            ByteBuf buf = (ByteBuf)msg;
            //System.Console.WriteLine("receive bytes " + buf.remainBytes());
            MemoryStream ms  = new MemoryStream(buf.getBuffer(), buf.getReaderIndex(), buf.remainBytes());
            T            obj = ProtoBuf.Serializer.Deserialize <T>(ms);

            buf.skipReaderIndex((int)ms.Position);
            if (receiveObjFunc != null)
            {
                receiveObjFunc(obj);
            }
        }
        public override void invokeWrite(object message)
        {
            ByteBuf buf = (ByteBuf)message;

            /*
             * var output = new System.IO.FileStream(Application.dataPath + "\\buf.txt", System.IO.FileMode.Append);
             * var writer = new System.IO.StreamWriter(output, System.Text.Encoding.UTF8);
             * writer.WriteLine("buf.remainBytes() = " + buf.remainBytes().ToString());
             * writer.Close();
             * output.Close();
             */



            Socket clientSocket = this.connection.getClientSocket();

            clientSocket.Send(buf.getBuffer(), buf.getReaderIndex(), buf.remainBytes(), SocketFlags.None);
        }
Ejemplo n.º 6
0
        void ReadMessage()
        {
            ByteBuf buf = recvBuf;

            buf.Clear();
            int bytes = 0;

            try {
                if (clientSocket.Poll(PollTimeDuration, SelectMode.SelectRead))
                {
                    bytes = clientSocket.Available;
                    if (bytes == 0)
                    {
                        connected = false;
                        UnityEngine.Debug.LogError("the connection disconnected!");
                        pipeLine.fireDeactive();
                    }
                    else
                    {
                        bytes = clientSocket.Receive(buf.getBuffer());
                    }
                }
                else if (clientSocket.Poll(PollTimeDuration, SelectMode.SelectError))
                {
                    pipeLine.fireDeactive();
                    UnityEngine.Debug.Log("SelectError, the connection disconnected!");
                }
            } catch (SocketException exp) {
                {
                    UnityEngine.Debug.LogException(exp);
                }
            }
            //UnityEngine.Debug.LogError("Sleep ......");
            Thread.Sleep(100);
            if (bytes > 0)
            {
                buf.setWriteIndex(bytes);
                pipeLine.fireRead(buf);
                pipeLine.readComplete();
            }
        }
Ejemplo n.º 7
0
        public bool UpdateLogic()
        {
            if (!connected)
            {
                return(false);
            }
            // Check Receive
            ByteBuf buf = recvBuf;

            buf.Clear();
            int bytes = 0;

            try
            {
                if (clientSocket.Poll(0, SelectMode.SelectRead))
                {
                    bytes = clientSocket.Available;
                    if (bytes == 0)
                    {
                        connected = false;
                        pipeLine.fireDeactive();
                    }
                    else
                    {
                        bytes = clientSocket.Receive(buf.getBuffer());
                    }
                }
            }
            catch (SocketException exp)
            {
                {
                    UnityEngine.Debug.LogException(exp);
                }
            }

            if (bytes > 0)
            {
                buf.setWriteIndex(bytes);
                pipeLine.fireRead(buf);
                pipeLine.readComplete();
            }
            if (!connected)
            {
                return(false);
            }
            // 写 线程!!
            TimeSpan timeSpan = new TimeSpan(DateTime.Now.Ticks - lastWritePingTicks);

            if (timeSpan.TotalSeconds > PingTime)
            {
                lastWritePingTicks = DateTime.Now.Ticks;
                if (PingProc != null)
                {
                    PingProc();
                }
            }
            object data = null;

            lock (this)
            {
                if (dataToWriteList.Count > 0)
                {
                    data = dataToWriteList.Dequeue();
                }
            }

            if (data != null)
            {
                pipeLine.write(data);
            }
            return(connected);
        }