private void ReceiveMsg() { byte[] buffer = new byte[1024]; int len = 0; while (true) { len = clientSocket.Receive(buffer); if (len <= 0) { continue; } var result = ProtobufEncoding.Decode(buffer) as Message; Debug.Log(result.MsgType + ", " + result.Agree + ", " + result.Msg); // TODO: 消息分发 } }
private void Receive() { if (!connected) { return; } Byte[] bytes = new Byte[1024]; int lengthl = 0; while (true) { try { if (!connected) { return; } NetworkStream stream = tcp.GetStream(); int length; while ((length = stream.Read(bytes, lengthl, bytes.Length - lengthl)) > 0) { KeyValuePair <byte[], List <Google.Protobuf.IMessage> > pair = ProtobufEncoding.Decode(bytes); if (pair.Key.Length > 0) { Array.Copy(pair.Key, 0, bytes, 0, pair.Key.Length); lengthl = pair.Key.Length; Array.Clear(bytes, lengthl, bytes.Length - lengthl); } else { Array.Clear(bytes, 0, bytes.Length); lengthl = 0; } foreach (Packet p in pair.Value.Cast <Packet>()) { IMessage msg = Utils.getPacket(p); if (msg != null) { msg.ReadSync(); } } } } catch (IOException ex) { Disconnect(); } catch (SocketException ex) { Disconnect(); } catch (InvalidOperationException ex) { Disconnect(); } } }