internal void OnPkt(EasyAdapter adapter, PktType tp, byte[] buf, int offset, int length) { _bufhelper.Init(buf, offset, length); int ii = (int)tp; if ((ii >= 0) && (ii < _calls.Length)) { var f = _calls[ii]; if (f != null) { f(adapter, buf, offset, length); } } }
//PktType.RpcResponse void OnRpcResponse(EasyAdapter adapter, byte[] buf, int offset, int length) { OnRpcAck(PktType.RpcResponse, adapter, buf, offset, length); }
//PktType.SyncHandles void OnTopicHandles(EasyAdapter adapter, byte[] buf, int offset, int length) { var hs = _bufhelper.ReadHashSet(); if (HashChange(adapter.Subs, hs)) { SubsChange(); } }
//PktType.RpcErr void OnRpcErr(EasyAdapter adapter, byte[] buf, int offset, int length) { OnRpcAck(PktType.RpcErr, adapter, buf, offset, length); }
void OnRpcRequest(EasyAdapter adapter, byte[] buf, int offset, int length) { int msgid = _bufhelper.ReadInt(); int source = _bufhelper.ReadInt(); int destination = _bufhelper.ReadInt(); EasyAdapter dest = null; var k = _bufhelper.ReadString(); var lst = new List<EasyAdapter>(); _Rpcs.TryGetValue(k, out lst); if (lst != null) { if (lst.Count == 1) { dest = lst[0]; } else if (lst.Count > 1) { dest = lst[_random.Next(lst.Count)]; } } if (dest == null) { var ms = PktHelper.NewPkt(PktType.RpcErr); ms.WriteInt(msgid); ms.WriteInt(adapter.ID); ms.WriteInt(adapter.ID); ms.WriteString("no such method!"); //Console.WriteLine("no such method!"); PktHelper.ClosePkt(ms); adapter.WriteStream(ms); //Console.WriteLine("no such method!----"); } else { var ms = PktHelper.NewPkt(PktType.RpcRequest); ms.WriteInt(msgid); ms.WriteInt(adapter.ID); ms.WriteInt(dest.ID); //Console.WriteLine("api_" + dest.ID); ms.WriteString(k); _bufhelper.ReadBytes(out offset, out length); ms.WriteBytes(buf, offset, length); PktHelper.ClosePkt(ms); dest.WriteStream(ms); } }
void OnRpcAck(PktType t, EasyAdapter adapter, byte[] buf, int offset, int length) { int msgid = _bufhelper.ReadInt(); int source = _bufhelper.ReadInt(); int destination = _bufhelper.ReadInt(); EasyAdapter dest = null; _AllAdapters.TryGetValue(destination, out dest); if (adapter != null) { var ms = PktHelper.NewPkt(t); ms.Write(buf, offset, length); PktHelper.ClosePkt(ms); dest.WriteStream(ms); } }
//PktType.SyncHandles void OnPubData(EasyAdapter adapter, byte[] buf, int offset, int length) { EasyAdapter dest = null; bool rp = _bufhelper.ReadByte() != 0; //是否单播 string k = _bufhelper.ReadString(); List<EasyAdapter> lst; _Subs.TryGetValue(k, out lst); if ((lst == null) || (lst.Count == 0)) { return; } var ms = PktHelper.NewPkt(PktType.PubData); ms.Write(buf, offset, length); PktHelper.ClosePkt(ms); buf = ms.ToArray(); length = (int)ms.Length; if (lst.Count == 1) { lst[0].Write(buf, 0, length); } else if (!rp) { dest = lst[_random.Next(lst.Count)]; dest.Write(buf, 0, length); } else { foreach (var node in lst) { node.Write(buf, 0, length); } } }
void OnPing(EasyAdapter adapter, byte[] buf, int offset, int length) { var ms = PktHelper.NewPkt(PktType.Pong); PktHelper.ClosePkt(ms); adapter.WriteStream(ms); }
void OnNewSocket(Socket sc) { Excute(() => { if (_AllAdapters.Count > MaxConnectCount) { SocketHelper.FreeSocket(sc); return; } var a = new EasyAdapter(this, sc); _AllAdapters[a.ID] = a; a.OnClose = this.Poll; var ms = PktHelper.NewPkt(PktType.Open); PktHelper.ClosePkt(ms); a.WriteStream(ms); }); }
//ErrClose void OnErrClose(EasyAdapter adapter, byte[] buf, int offset, int length) { adapter.Free(); }