private string AllDeviceStatusRequest(BaseProto bp) { try { StringBuilder sbd = new StringBuilder(); for (int i = 1; i < 31; i++) { if (i == 25 || i == 26) { continue; } if (i < 10) { bp = PMng.GetPObject("0" + i.ToString(), string.Empty) as P01; sbd.Append(PMng.MakeFrame(bp)); bp = null; } else { bp = PMng.GetPObject(i.ToString(), string.Empty) as P01; sbd.Append(PMng.MakeFrame(bp)); bp = null; } } return(sbd.ToString()); } catch (Exception ex) { return(ex.Message); } }
// 需要外部循环调用的 // 消息下发(主线程) public static void Update() { // 从消息列表中获取消息 if (NetManager.MessageQueue.Count > 0) { string[] item = null; lock (NetManager.MessageQueue) { //item = NetManager.MessageQueue.Peek(); item = NetManager.MessageQueue.Dequeue(); } string type = item[0]; string msg = item[1]; if (type == "Linked") { NetEvent.SendEvent(netEventEnum.Linked, msg); // 发送事件 //lock (NetManager.MessageQueue) { // NetManager.MessageQueue.Dequeue(); //} return; } UILog.log.Add("接收到的消息:" + msg); BaseProto date = JsonUtility.FromJson(msg, Type.GetType(type)) as BaseProto; NetEvent.SendEvent((netEventEnum)date.protoType, date.returnFun()); // 发送事件 //lock (NetManager.MessageQueue) { // NetManager.MessageQueue.Dequeue(); //} UILog.log.Add("接收到的消息类型:" + date.protoType); } }
private void SendBodyEnd(IAsyncResult _result) { socket.EndSend(_result); bool beginReceive = (bool)_result.AsyncState; if (beginReceive) { ReceiveHead(); } BaseProto sendData = null; lock (sendPool) { if (sendPool.Count > 0) { sendData = sendPool[0]; sendPool.RemoveAt(0); } else { isSendingData = false; } } if (sendData != null) { SendDataReal(sendData); } }
private void GetData(BaseProto _data) { Console.WriteLine("GetData:" + _data.GetType().ToString()); if (userService != null) { Action <SuperUserServiceBase> callBack = delegate(SuperUserServiceBase _service) { _service.GetData(_data); }; userService.Process(callBack); } else { LoginProto loginProto = _data as LoginProto; Action <UserManager> callBack = delegate(UserManager _service) { _service.Login(loginProto.userName, loginProto.password, this); }; UserManager.Instance.Process(callBack); } }
private void ReceiveBodyEnd(IAsyncResult _result) { int i = socket.EndReceive(_result); if (i == 0) { Console.WriteLine("disconnect!"); } else if (i < bodyLength) { bodyOffset = bodyOffset + i; bodyLength = bodyLength - i; ReceiveBody(); } else { receiveStream.Position = 0; receiveStream.Write(bodyBuffer, 0, bodyOffset + i); receiveStream.Position = 0; BaseProto data = reveiveFormatter.Deserialize(receiveStream) as BaseProto; GetData(data); } }
/// <summary> /// 编码 /// </summary> /// <returns></returns> public static byte[] Encode(BaseProto message) { string s = Js.Serialize(message); //Console.WriteLine("编码:" + s); return(Encoding.UTF8.GetBytes(s)); }
public void SendData <T>(BaseProto _data, Action <T> _callBack) where T : BaseProto { if (_data.type != PROTO_TYPE.C2S) { SuperDebug.LogError("sendError!"); return; } Action <BaseProto> callBack = delegate(BaseProto obj) { _callBack(obj as T); }; lock (sendPool) { if (isWaittingForResponse) { sendPool.Add(_data); callBackPool.Add(callBack); return; } else { isWaittingForResponse = true; nowCallBack = callBack; } } SendDataReal(_data); }
private void SendDataReal(BaseProto _data) { sendFormatter.Serialize(sendStream, _data); sendStream.Position = 0; socket.BeginSend(BitConverter.GetBytes(sendStream.Length), 0, 4, SocketFlags.None, SendHeadEnd, null); }
/// <summary> /// 解码 /// </summary> /// <returns></returns> public static BaseProto Decode(string protoName, string message) { //string message = Encoding.UTF8.GetString(bytes).Replace("\0", null).Trim(); //Console.WriteLine("准备解码:" + message); BaseProto date = Js.Deserialize(message, Type.GetType("Net.Proto." + protoName)) as BaseProto; return(date); }
private void SendDataReal(BaseProto _data) { sendFormatter.Serialize(sendStream, _data); sendStream.Position = 0; bool beginReceive = _data.type == PROTO_TYPE.S2C; Console.WriteLine("SendDataReal:" + _data.GetType().ToString()); socket.BeginSend(BitConverter.GetBytes(sendStream.Length), 0, HEAD_LENGTH, SocketFlags.None, SendHeadEnd, beginReceive); }
public Test() { //如果使用了List<T>类型,T类型为自定义的Protobuf类则需全局仅注册一次Member中List<T>中T的类型,这个步骤必须在序列化之前注册,metaIndex与ProtoMember中的MemberTypeIndex对应 ProtobufPropertyHelper.RegisterListMemberType(0, typeof(TestData)); BaseProto proto = new BaseProto(); proto.datas.Add(new TestData() { cid = 100 }); System.IO.MemoryStream stream = new System.IO.MemoryStream(); ProtoBuf.Serializer.Serialize(stream, typeof(BaseProto), proto); }
public void SendData(BaseProto _data) { lock (sendPool) { if (!isSendingData) { isSendingData = true; } else { sendPool.Add(_data); return; } } SendDataReal(_data); }
// 构造 public Client(Socket socket, int id) { Console.WriteLine("连接已经建立"); this.socket = socket; this.id = id; // 连接成功,下发用户信息(id) BaseProto bp = new BaseProto(); bp.protoType = netEventEnum.Enter; bp.id = id; Send(netEventEnum.Enter, Encoding.UTF8.GetString(Packet.Encode(bp))); // 开始接收 Console.WriteLine("开始接收..."); SendMsg(); socket.BeginReceive(readbuff.bytes, readbuff.writeIdx, readbuff.remain, 0, BeginReceiveCallback, socket); }
static void Main() { BaseProto obj = new DesiredProto { Address = "123 Somewhere", DestType = DestinationType.Foo, Name = "Marc", Owner = "Also Marc", VType = VObjectType.A }; BaseProto clone = Serializer.DeepClone(obj); DesiredProto typedClone = (DesiredProto)clone; Console.WriteLine(typedClone.Address); Console.WriteLine(typedClone.DestType); Console.WriteLine(typedClone.Name); Console.WriteLine(typedClone.Owner); Console.WriteLine(typedClone.VType); }
protected void SendData(BaseProto _data) { if (_data.type == PROTO_TYPE.S2C) { isWaittingForResponse = false; } if (serverUnit != null) { serverUnit.SendData(_data); } else { if (!isWaittingForResponse && replaceServerUnitCallBack != null) { replaceServerUnitCallBack(); replaceServerUnitCallBack = null; } } }
internal void GetData(BaseProto _data) { isWaittingForResponse = true; dataHandleDic[_data.GetType()](this, _data); }
private void ReceiveBodyEnd(IAsyncResult _result) { int length = socket.EndReceive(_result); Debug.Log("ReceiveBodyEnd!" + length + " " + bodyLength); if (length == 0) { SuperDebug.LogError("Disconnect!!!"); } else if (length < bodyLength) { bodyLength = bodyLength - length; bodyOffset = bodyOffset + length; ReceiveBody(); } else { Debug.Log("all!" + length + " " + bodyLength); receiveStream.Position = 0; receiveStream.Write(bodyBuffer, 0, bodyOffset + length); receiveStream.Position = 0; BaseProto data = null; try{ data = reveiveFormatter.Deserialize(receiveStream) as BaseProto; }catch (Exception e) { Debug.Log(e.ToString()); } Debug.Log("receive:" + data.GetType().ToString()); switch (data.type) { case PROTO_TYPE.C2S: SuperDebug.LogError("error1!"); return; case PROTO_TYPE.S2C: BaseProto sendData = null; Action <BaseProto> tmpCallBack = nowCallBack; nowCallBack = null; Action callBack = delegate() { tmpCallBack(data); }; lock (receivePool){ receivePool.Add(callBack); } lock (sendPool){ if (!isWaittingForResponse) { SuperDebug.LogError("error6!"); return; } if (sendPool.Count > 0) { sendData = sendPool[0]; sendPool.RemoveAt(0); nowCallBack = callBackPool[0]; callBackPool.RemoveAt(0); } else { isWaittingForResponse = false; } } if (sendData != null) { SendDataReal(sendData); } break; default: Type protoType = data.GetType(); if (pushDic.ContainsKey(protoType)) { callBack = delegate() { pushDic[protoType](data); }; lock (receivePool){ receivePool.Add(callBack); } } else { SuperDebug.LogError("error669!"); } break; } ReceiveHead(); } }