コード例 #1
0
        /// <summary>
        /// SendProtocol
        /// </summary>
        /// <param name="state"></param>
        /// <param name="protocol"></param>
        /// <param name="sobj"></param>
        public static void SendProtocol(SocketState state, CommunicateProtocol protocol, IHSSocket sobj)
        {
            if (protocol == null)
            {
                throw new Exception("Protocol should not be null.");
            }
            if (state == null)
            {
                throw new Exception("StateClient should not be null.");
            }
            if (sobj == null)
            {
                throw new Exception("client should not be null.");
            }

            sobj.SyncBufferSend(state, protocol.GetHeaderBytes());

            if (protocol.GetStream1().Length > 0)
            {
                sobj.SyncBufferSend(state, protocol.GetStream1());
            }

            if (protocol.GetStream2() != null)
            {
                var fs = protocol.GetStream2() as Stream;
                if (fs != null)
                {
                    fs.Seek(0, SeekOrigin.Begin);
                    int rIndex  = 0;
                    int fileLen = (int)fs.Length;
                    while (rIndex < fs.Length)
                    {
                        byte[] cache = new byte[GlobalConfig.Protocol_Buffer_Len];
                        int    len   = fs.Read(cache, 0, fileLen - rIndex >= cache.Length ? cache.Length : fileLen - rIndex);
                        if (len < cache.Length)
                        {
                            byte[] bytes = new byte[len];
                            Array.Copy(cache, 0, bytes, 0, bytes.Length);
                            sobj.SyncBufferSend(state, bytes);
                        }
                        else
                        {
                            sobj.SyncBufferSend(state, cache);
                        }
                        rIndex += len;
                    }
                }
            }
        }