/// <summary> /// 压缩流 /// </summary> /// <param name="source">源流</param> /// <param name="dest">目标流</param> private static void Compress(Stream source, Stream dest) { try { //把dest using (GZipStream gzipStream = new GZipStream(dest, CompressionMode.Compress, true)) { source.Position = 0L; int length = (int)source.Length; byte[] buffer = new byte[length]; int count; //直到字节数组全读取源流完后,写入压缩流 while ((count = source.Read(buffer, 0, length)) > 0) { //写入压缩流 gzipStream.Write(buffer, 0, count); } gzipStream.Close(); } } catch (Exception ex) { if (DebugMessage.Error == null) { return; } DebugMessage.Error((object)ex.ToString()); } }
/// <summary> /// 解压缩 /// </summary> /// <param name="source"></param> /// <param name="dest"></param> private static void Decompress(Stream source, Stream dest) { try { using (GZipStream gzipStream = new GZipStream(source, CompressionMode.Decompress, true)) { int length = (int)source.Length; //创建长度为源流的 数组 byte[] buffer = new byte[length]; int count; //如果流还未读到末尾,就继续读取,直到把解压缩后的源流全写到目标流为止 while ((count = gzipStream.Read(buffer, 0, length)) > 0) { //把解压缩后的字节数组写入目标流 dest.Write(buffer, 0, count); } dest.Position = 0L; gzipStream.Close(); } } catch (Exception ex) { if (DebugMessage.Error == null) { return; } DebugMessage.Error((object)ex.ToString()); } }
/// <summary> /// 用protocolbuff来编码/序列化 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static byte[] ProteBufEncode(object obj) { byte[] bytes = null; try { using (MemoryStream ms = new MemoryStream()) { if (obj != null) { RuntimeTypeModel.Default.Serialize(ms, obj); } ms.Position = 0; int lenth = (int)ms.Length; bytes = new byte[lenth]; ms.Read(bytes, 0, lenth); } } catch (Exception e) { if (DebugMessage.Error == null) { return(null); } DebugMessage.Error((object)e.Message); } return(bytes); }
/// <summary> /// 开始和指定端口连接 /// </summary> /// <param name="port"></param> public void Start(int port) { this.acceptClients = new System.Threading.Semaphore(this.maxClient, this.maxClient); try { this.ServerIPV4.Bind((EndPoint) new IPEndPoint(IPAddress.Any, port)); this.ServerIPV6.Bind((EndPoint) new IPEndPoint(IPAddress.IPv6Any, port)); //挂起的连接队列的最大长度为10 this.ServerIPV4.Listen(10); ////挂起的连接队列的最大长度为10 this.ServerIPV6.Listen(10); //ipv4 开始接受,其中waitone 即是一般的new Thread + thread.start(); StartAccept((SocketAsyncEventArgs)null); //ipv6 开始接受,传null StartAcceptV6((SocketAsyncEventArgs)null); } catch (Exception ex) { if (DebugMessage.Error == null) { return; } DebugMessage.Error((object)ex.Message); } }
/// <summary> /// 解码流,反序列化 /// </summary> /// <param name="value"></param> /// <returns></returns> public static object Decode(byte[] value) { try { object obj; using (MemoryStream ms1 = new MemoryStream(value, false)) using (MemoryStream ms2 = new MemoryStream()) { ////新建二进制格式 //BinaryFormatter binaryFormatter = new BinaryFormatter(); ms1.Position = 0L; //把流一解压缩到流二 SerializeUtil.Decompress((Stream)ms1, (Stream)ms2); //对内存流二反序列化到二进制格式 obj = new BinaryFormatter().Deserialize((Stream)ms2); } return(obj); } catch (FileLoadException ex) { if (DebugMessage.Error != null) { DebugMessage.Error((object)ex.ToString()); } return((object)null); } catch (Exception ex) { if (DebugMessage.Error != null) { DebugMessage.Error((object)ex.ToString()); } return((object)null); } }
/// <summary> /// 处理接受请求,无限处理 /// </summary> /// <param name="e"></param> public void ProcessAccept(SocketAsyncEventArgs e) { //创建一个新的用户模型,然后设置/从池子中取出 UserToken token = userTokenPool.DeQuene(); //设置socket token.conn = e.AcceptSocket; //连接处理中心连接socket this.center.ClientConnect(token); //开始接受数据 this.StartReceive(token); try { //确认ipv4 或ipv6,无限接收 switch (e.AcceptSocket.RemoteEndPoint.AddressFamily) { case AddressFamily.InterNetwork: this.StartAccept(e); break; case AddressFamily.InterNetworkV6: this.StartAcceptV6(e); break; } } catch (Exception ex) { if (DebugMessage.Error != null) { DebugMessage.Error((object)ex.Message); } this.StartAccept(e); } }
/// <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); } } } }
/// <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); }
/// <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); }
/// <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); } }
/// <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); } }
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); } }
/// <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); } }
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); }
/// <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); } }