Esempio n. 1
0
 private void ReceiveHead(IAsyncResult ar)
 {
     try
     {
         int len = conn.EndReceive(ar);
         if (len > 0)
         {
             currHeadIndex += len;
             if (currHeadIndex < headLength)
             {
                 conn.BeginReceive(headCache, currHeadIndex, headLength - currHeadIndex, SocketFlags.None
                                   , new AsyncCallback(ReceiveHead), null);
             }
             else
             {
                 bodyLength = ZNetConfig.isLittleEndian ? ZTool.ConvertToIntLittleEndian(headCache)
                     : ZTool.ConvertToIntBigEndian(headCache);
                 initBodyCache();
                 conn.BeginReceive(bodyCache, 0, bodyLength, SocketFlags.None
                                   , new AsyncCallback(ReceiveBody), null);
             }
         }
         else
         {
             ZLogger.Debug(" ZReceive 002:用户" + connectId + " 连接已断开", LogType.Warning);
             Close();
         }
     }
     catch (System.Exception ex)
     {
         ZLogger.Debug(" ZReceive 003:用户" + connectId + " 连接已断开", LogType.Warning);
         Close();
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 头部长度加在数据前面
        /// </summary>
        /// <param name="data"></param>
        /// <param name="IsLittleEndian"></param>
        /// <returns></returns>
        public static byte[] Pack(byte[] data, bool IsLittleEndian = true)
        {
            int length = data.Length;

            byte[] head = IsLittleEndian ? ZTool.ConvertBytesLittleEndian(length) : ZTool.ConvertBytesBigEndian(length);
            byte[] temp = new byte[data.Length + 4];
            head.CopyTo(temp, 0);
            data.CopyTo(temp, 4);
            return(temp);
        }
Esempio n. 3
0
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="data"></param>
        public void Send(Socket conn, System.Object obj, string connectId = null)
        {
            string datastr = null;

            try
            {
                datastr = JsonConvert.SerializeObject(obj);
            }
            catch (System.Exception ex)
            {
                ZLogger.Debug("ZSend 001:传输数据无法被序列化为Json:" + ex.ToString(), LogType.Error);
                ZLogger.Debug("停止发送本条消息!");
                return;
            }

            byte[] data = ZNetConfig.encoding.GetBytes(datastr);

            if (datastr == "\"\"")
            {
                ZLogger.Debug("ZSend 002:无法传递空消息:", LogType.Error);
                ZLogger.Debug("停止发送本条消息!");
                return;
            }
            else if (data.Length > ZNetConfig.MaxMessageSize)
            {
                ZLogger.Debug("ZSend 003: 单次发送消息过长", LogType.Error);
                ZLogger.Debug("停止发送本条消息!");
                return;
            }
            data = ZTool.Pack(data, ZNetConfig.isLittleEndian);

            try
            {
                conn.BeginSend(data, 0, data.Length, SocketFlags.None,
                               new AsyncCallback((IAsyncResult ar) =>
                {
                    conn.EndSend(ar);
                    OnEndSend?.Invoke(connectId);
                }), null);
            }
            catch (System.Exception ex)
            {
                OnDisConnected?.Invoke(connectId); //关闭连接
                conn?.Dispose();
                conn?.Close();
                conn = null;
                ZLogger.Debug("ZSend 003: 用户:" + connectId + " 连接已经断开", LogType.Warning);
            }
        }