public CustomUdpClient(NetRespEvent respEvent, NetEnDecode enDecode) { onNetRespEvent = respEvent; netEnDecode = enDecode; encodeMs = new MemoryStream(); encodeBw = new BinaryWriter(encodeMs); }
public void DelNetRespEvent(NetRespEvent netRespEvent) { if (NetRespEvent != null) { NetRespEvent -= netRespEvent; } }
public CustomTcpClient(NetRespEvent respEvent, NetEnDecode enDecode) { _onNetRespEvent = respEvent; _netEnDecode = enDecode; _decodeMs = new MemoryStream(); _decodeBr = new BinaryReader(_decodeMs); _encodeMs = new MemoryStream(); _encodeBw = new BinaryWriter(_encodeMs); }
public void AddNetRespEvent(NetRespEvent netRespEvent) { if (NetRespEvent == null) { NetRespEvent = netRespEvent; } else { NetRespEvent += netRespEvent; } }
/// <summary> /// ws://127.0.0.1:8080 /// tcp://127.0.0.1:8080 /// udp://127.0.0.1:8080 /// </summary> /// <param name="url"></param> /// <param name="netEnDecode"></param> /// <param name="netRespEvent"></param> /// <returns></returns> public bool Connect(string url, NetEnDecode netEnDecode, NetRespEvent netRespEvent) { if (string.IsNullOrEmpty(url)) { //Odin.Log.Error("Url Is Null"); return(false); } if (netEnDecode == null) { //Odin.Log.Error("NetEnDecode Is Null"); return(false); } if (netRespEvent == null) { //Odin.Log.Error("NetRespEvent Is Null"); return(false); } if (NetRespEvent == null) { NetRespEvent += netRespEvent; } else { bool isAddEvent = false; foreach (var @delegate in NetRespEvent.GetInvocationList()) { if (@delegate.GetHashCode() == netRespEvent.GetHashCode()) { isAddEvent = true; break; } } if (!isAddEvent) { NetRespEvent += netRespEvent; } } if (string.IsNullOrEmpty(url)) { //Odin.Log.Error("url error"); return(false); } this._url = url; var idx = url.IndexOf("://"); string prefix = url.Substring(0, idx); switch (prefix.ToLower()) { case "tcp": _client = new CustomTcpClient(OnRespMsgEvent, netEnDecode); return(StartConnect(url)); case "udp": _client = new CustomUdpClient(OnRespMsgEvent, netEnDecode); return(StartConnect(url)); case "ws": _client = new WebSocketClient(OnRespMsgEvent, netEnDecode); _client.Connect(url, 0); return(true); default: //Odin.Log.Error("does not support protocol"); return(false); } }