Ejemplo n.º 1
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();
            }
        }