Exemplo n.º 1
0
        public void SendData(int ConnectID, NetMessage msg)
        {
            if (msg == null)
            {
                return;
            }

            ISafeNetworkStream stream = this.contextKeyManager.GetNetStream(ConnectID);

            if (stream == null)
            {
                return;
            }

            try
            {
                byte[] bMsg = msg.ToStream();
                stream.Write(bMsg, 0, bMsg.Length);

                if (this.ServiceDirectCommitted != null)
                {
                    this.ServiceDirectCommitted(ConnectID, msg);
                }
            }
            catch (Exception ee)
            {
                if (ee is System.IO.IOException)                //正在读写流的时候,连接断开
                {
                    this.DisposeOneConnection(ConnectID, DisconnectedCause.NetworkError);
                }

                throw ee;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// ReceiveData 从网络读取指定长度的数据
        /// </summary>
        public static byte[] ReceiveData(ISafeNetworkStream stream, int size)
        {
            byte[] result = new byte[size];

            NetHelper.ReceiveData(stream, result, 0, size);

            return(result);
        }
Exemplo n.º 3
0
        public void DisposeOneConnection(int connectID, DisconnectedCause cause)
        {
            ISafeNetworkStream stream = this.contextKeyManager.GetNetStream(connectID);

            if (stream == null)
            {
                return;
            }

            this.contextKeyManager.RemoveContextKey(connectID);
            stream.Close();

            if (this.SomeOneDisConnected != null)
            {
                this.SomeOneDisConnected(connectID, cause);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// ReceiveData 从网络读取指定长度的数据 ,存放在buff中offset处
        /// </summary>
        public static void ReceiveData(ISafeNetworkStream stream, byte[] buff, int offset, int size)
        {
            int readCount  = 0;
            int totalCount = 0;
            int curOffset  = offset;

            while (totalCount < size)
            {
                int exceptSize = size - totalCount;
                readCount = stream.Read(buff, curOffset, exceptSize);
                if (readCount == 0)
                {
                    throw new IOException("NetworkStream Interruptted !");
                }
                curOffset  += readCount;
                totalCount += readCount;
            }
        }
Exemplo n.º 5
0
        public ISafeNetworkStream GetNetStream(int streamHashCode)
        {
            ISafeNetworkStream tar = null;

            this.rWlock.AcquireReaderLock(this.timeOut);

            foreach (ContextKey key in this.contextKeyList)
            {
                if (key.NetStream.GetHashCode() == streamHashCode)
                {
                    tar = key.NetStream;
                    break;
                }
            }

            this.rWlock.ReleaseReaderLock();

            return(tar);
        }
        public RecieveResult RecieveNetMessage(ISafeNetworkStream stream, byte[] revBuff, out NetMessage msg)
        {
            msg = null;

            int headerLen = this.contractHelper.MessageHeaderLength;

            NetHelper.RecieveData(stream, revBuff, 0, headerLen);
            IMessageHeader header = this.contractHelper.ParseMessageHeader(revBuff, 0);

            if (!this.contractHelper.ValidateMessageToken(header))
            {
                return(RecieveResult.MessageTokenInvalid);
            }

            if ((headerLen + header.MessageBodyLength) > this.maxMessageSize)
            {
                return(RecieveResult.MessageSizeOverflow);
            }

            msg        = new NetMessage();
            msg.Header = header;

            if (header.MessageBodyLength > 0)
            {
                if ((header.MessageBodyLength + headerLen) <= revBuff.Length)
                {
                    NetHelper.RecieveData(stream, revBuff, 0, header.MessageBodyLength);
                    msg.Body = revBuff;
                }
                else
                {
                    byte[] rentBuff = this.bufferPool.RentBuffer(header.MessageBodyLength);

                    NetHelper.RecieveData(stream, rentBuff, 0, header.MessageBodyLength);
                    msg.Body = rentBuff;
                }
            }

            return(RecieveResult.Succeed);
        }
Exemplo n.º 7
0
 public ContextKey(ISafeNetworkStream net_Stream, int buffSize)
 {
     this.netStream = net_Stream;
     this.buffer    = new byte[buffSize];
 }
 public void SendNetMessage(ISafeNetworkStream stream, NetMessage msg)
 {
     byte[] bMsg = msg.ToStream();
     stream.Write(bMsg, 0, bMsg.Length);
 }