public void Connect() { try { socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); AbsCoding.Ins = new PbCoding(); socket.Connect(V_IP, V_Port); socket.BeginReceive(readBuffer, 0, 1024, SocketFlags.None, ReceiveCallBack, null); TransModel model = new TransModel(); if (!string.IsNullOrEmpty(V_ConnectKey)) { model.SetMsg(Convert.ToBase64String(Encoding.UTF8.GetBytes(V_ConnectKey))); } ins.Send(model); //EventMgr.Ins.InvokeDeList(EM_LoginEvent.ConnectSuccess); } catch (Exception ex) { //EventMgr.Ins.InvokeDeList(EM_LoginEvent.ConnectFail); Debug.Log("无法连接服务器,请重启客户端或联系服务器管理员"); //AlertMgr.Ins.AddMsg("无法连接服务器,请重启客户端或联系服务器管理员"); } }
public void Send <T>(int pid, int area, T message) { TransModel model = new TransModel(pid, area); model.SetMsg(message); Send(model); }
public void Write() { if (write_queue.Count == 0) { isWrite = false; return; } //消息队列不为空,取出来并发送 TransModel m = write_queue.Dequeue(); byte[] queue = AbsCoding.Ins.ModelEncoding(m); if (queue == null) { isWrite = false; return; } try { socket.BeginSend(queue, 0, queue.Length, SocketFlags.None, SendCallBack, null); } catch (Exception ex) { Debug.Log(ex.ToString()); } }
public void Send(TransModel model) { write_queue.Enqueue(model); if (!isWrite) { isWrite = true; Write(); } }
/// <summary> /// 对缓冲区数据进行处理 /// </summary> void Read() { TransModel model = AbsCoding.Ins.ModelDecoding <TransModel>(ref cache); if (model == null) { isRead = false; return; } msg.Add(model); //todo 处理 model //example //Console.Write(model.GetMsg<string>()); //TransModel m = new TransModel(1, 2, 3); //m.SetMsg("i am yellow client"); //Send(m); Read(); }