Ejemplo n.º 1
0
        public override void write(ConnectionHandlerContext ctx, object msg)
        {
            if (msg is ByteBuf)
            {
                tempBuf.Clear();
                ByteBuf buf = (ByteBuf)msg;
                if (LengthBytes == 2)
                {
                    ushort len = (ushort)buf.remainBytes();
                    tempBuf.writeUInt16(len);
                }
                else if (LengthBytes == 4)
                {
                    uint len = (uint)buf.remainBytes();
                    tempBuf.writeUInt32(len);
                }
                else if (LengthBytes == 8)
                {
                    ulong len = (ulong)buf.remainBytes();
                    tempBuf.writeUInt64(len);
                }
                tempBuf.writeBytes(buf);

                ctx.fireWrite(tempBuf);
            }
        }
Ejemplo n.º 2
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.º 3
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);
        }