/// <summary> /// 发送数据给服务器 /// </summary> public void Send(Google.Protobuf.IMessage msg) { if (IsConnected == false) { Debug.LogError("服务器未连接!"); return; } if (null == msg) { Debug.LogError("消息不能为空!"); return; } try { clientSocket.Send(ProtobufEncoding.Encode(msg)); } catch { IsConnected = false; clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); } }
public void Send(IMessage p) { if (!connected) { return; } try { NetworkStream stream = tcp.GetStream(); if (stream.CanWrite) { int?id = Utils.GetID(p.GetType()); if (id == null) { return; } byte[] b = ProtobufEncoding.Encode(new Packet() { Id = (int)id, Msg = p.ToString() }, false); stream.WriteAsync(b, 0, b.Length); } } catch (InvalidOperationException ex) { Disconnect(); } catch (SocketException ex) { Disconnect(); } }