示例#1
0
 /// <summary>
 /// 客户端关闭
 /// </summary>
 /// <param name="token">断开的客户端对象</param>
 /// <param name="error">断开原因</param>
 public void ClientClose(UserToken token, string error)
 {
     if (token == null || token.conn == null)
     {
         if (DebugMessage.Debug == null)
         {
             return;
         }
         DebugMessage.Debug((object)("客户端异常退出:" + error));
     }
     //如果不为空,正常执行客户端断开连接
     else
     {
         lock (token)
         {
             if (DebugMessage.Debug != null)
             {
                 DebugMessage.Debug((object)("用户退出" + error));
             }
             //应用层断开连接业务处理,并处具体客户端以及原因
             this.center.ClientClose(token, error);
             //客户端关闭
             token.Close();
             //关闭客户端,回收回池子
             userTokenPool.EnterQuene(token);
             try
             {
                 //释放信号量
                 this.acceptClients.Release();
             }
             catch (Exception ex)
             {
                 if (DebugMessage.Error == null)
                 {
                     return;
                 }
                 DebugMessage.Error((object)ex.Message);
             }
         }
     }
 }
示例#2
0
        /// <summary>
        /// 用protocolbuff来解码/反序列化
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static object ProtoBufDecode(byte[] bytes, Type type)
        {
            object obj = null;

            try
            {
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    obj = RuntimeTypeModel.Default.Deserialize(ms, null, type);
                }
            }
            catch (Exception e)
            {
                if (DebugMessage.Error == null)
                {
                    return(null);
                }
                DebugMessage.Error((object)e.Message);
            }
            return(obj);
        }
示例#3
0
        /// <summary>
        /// 用protocolbuff来解码/反序列化
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static object ProtoBufDecode <T>(byte[] bytes)
        {
            object obj = null;

            try
            {
                using (MemoryStream ms = new MemoryStream(bytes))
                {
                    obj = Serializer.Deserialize <T>(ms);
                }
            }
            catch (Exception e)
            {
                if (DebugMessage.Error == null)
                {
                    return(null);
                }
                DebugMessage.Error((object)e.Message);
            }
            return((T)obj);
        }
示例#4
0
 /// <summary>
 /// 开始接收
 /// </summary>
 /// <param name="token"></param>
 public void StartReceive(UserToken token)
 {
     try
     {
         //如果目前socket处于挂起状态,返回
         if (token.conn.ReceiveAsync(token.receiveSAEA))
         {
             return;
         }
         //否则开始接收,传入异步事件事件
         ProcessReceive(token.receiveSAEA);
     }
     catch (Exception ex)
     {
         if (DebugMessage.Error == null)
         {
             return;
         }
         DebugMessage.Error((object)ex.Message);
     }
 }
示例#5
0
 /// <summary>
 /// 编码流,序列化
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public static byte[] Encode(object value)
 {
     try
     {
         using (MemoryStream ms = new MemoryStream())
         {
             BinaryFormatter bf = new BinaryFormatter();
             bf.Serialize(ms, value);
             byte[] array = new byte[ms.Length];
             Buffer.BlockCopy(ms.GetBuffer(), 0, array, 0, (int)ms.Length);
             return(array);
         };
     }
     catch (Exception ex)
     {
         if (DebugMessage.Error != null)
         {
             DebugMessage.Error((object)ex.ToString());
         }
         return((byte[])null);
     }
 }
示例#6
0
 public static byte[] EncodeObj(object value)
 {
     try
     {
         byte[] numArray;
         using (MemoryStream ms = new MemoryStream())
         {
             new BinaryFormatter().Serialize((Stream)ms, value);
             numArray = new byte[ms.Length];
             Buffer.BlockCopy((Array)ms.GetBuffer(), 0, (Array)numArray, 0, (int)ms.Length);
         }
         return(numArray);
     }
     catch (Exception ex)
     {
         if (DebugMessage.Error != null)
         {
             DebugMessage.Error((object)ex.ToString());
         }
         return((byte[])null);
     }
 }
示例#7
0
 /// <summary>
 /// 断开连接
 /// </summary>
 public void Close()
 {
     try
     {
         this.writeQueue.Clear();
         this.cache.Clear();
         this.isReading = false;
         this.isWriting = false;
         //socket关闭,断开:发送以及接收
         this.conn.Shutdown(SocketShutdown.Both);
         //关闭连接
         this.conn.Close();
         this.conn = (Socket)null;
     }
     catch (Exception ex)
     {
         if (DebugMessage.Error == null)
         {
             return;
         }
         DebugMessage.Error((object)ex.Message);
     }
 }
示例#8
0
        public static object DecodeObj(byte[] value)
        {
            object obj;

            try
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    ms.Position = 0L;
                    obj         = binaryFormatter.Deserialize((Stream)ms);
                    ms.Close();
                }
            }
            catch (Exception ex)
            {
                if (DebugMessage.Error != null)
                {
                    DebugMessage.Error((object)ex.ToString());
                }
                return((object)null);
            }
            return(obj);
        }
示例#9
0
 /// <summary>
 /// 正在发送的数据包
 /// </summary>
 public void OnWrite()
 {
     try
     {
         lock (this.sendSAEA)
         {
             if (this.writeQueue.Count == 0)
             {
                 //取消正在写标识
                 this.isWriting = false;
             }
             else
             {
                 //取出队列中最前面的消息
                 byte[] write = this.writeQueue[0];
                 this.writeQueue.RemoveAt(0);
                 //如果write队列是空
                 if (write == null)
                 {
                     if (DebugMessage.Notice != null)
                     {
                         DebugMessage.Notice((object)"解析出待发送的值为null");
                     }
                     this.isWriting = false;
                     //递归
                     this.OnWrite();
                 }
                 else
                 {
                     //设置数据缓冲
                     this.sendSAEA.SetBuffer(write, 0, write.Length);
                     //发送异步,先发送拆出来的一部分出去
                     bool flag = this.conn.SendAsync(this.sendSAEA);
                     //如果处于挂起状态直接返回
                     if (flag)
                     {
                         return;
                     }
                     //不处于挂起就发送进程
                     this.sendProcess(this.sendSAEA);
                     Console.Write("是否挂起:" + flag.ToString());
                 }
             }
         }
     }
     catch (InvalidOperationException ex)
     {
         if (DebugMessage.Error == null)
         {
             return;
         }
         DebugMessage.Error((object)ex.Message);
     }
     catch (Exception ex)
     {
         if (DebugMessage.Error == null)
         {
             return;
         }
         DebugMessage.Error((object)ex.Message);
     }
 }