using CommonLang; using CommonLang.Property; using SimpleJson; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Sockets; using System.Text; namespace Pomelo.DotNetClient { public class PomeloClientFactory { public static PomeloClientFactory Instance { get; private set; } static PomeloClientFactory() { new PomeloClientFactory(); } public PomeloClientFactory() { Instance = this; } public virtual PomeloClientAdapter CreateAdapter(IPomeloClientAdapterListener client) { return new PomeloTCP(client); } } public interface PomeloClientAdapter { Socket Session { get; } NetWorkState NetWorkState { get; } long TotalRecvBytes { get; } long TotalSentBytes { get; } void connect(string host, int port, int timeout, JsonObject user); void disconnect(); void send(SendMessage msg); void disposing(); void update(float deltime); bool resolve_route(RecvMessage msg, byte flag, out string route); void DoSendUpdate(); long GetPing(); } public interface IPomeloClientAdapterListener { void OnReceivedMessage(RecvMessage msg); void OnNetWorkStateChanged(NetWorkState st); void OnError(Exception err); void OnDisconnected(Socket session, string reason); void OnConnected(Socket session, JsonObject user); void ClearQueue(); } public interface ISerializer { void Serialize(Stream stream, object msg); object Deserialize(Stream source, object value, Type type); } #region _Types_ /// /// 网络状态 /// public enum NetWorkState { [Desc("主动断开")] CLOSED, [Desc("正在连接")] CONNECTING, [Desc("连接成功")] CONNECTED, [Desc("被动断开")] DISCONNECTED, [Desc("连接超时")] TIMEOUT, [Desc("网络错误")] ERROR, [Desc("服务器踢人")] KICK, [Desc("初始状态")] NONE } public class PomeloException : Exception { private readonly int code; public int Code { get { return code; } } public string Route { get; internal set; } public bool Timeout { get; internal set; } public PomeloException(int code, string message) : base(message) { this.code = code; } public PomeloException(int code, string message, Exception inner) : base(message, inner) { this.code = code; } } public delegate object MessageDecoder(string route, Stream input); public delegate void MessageEncoder(string route, object msg, Stream output); public delegate bool ProcessMessageImmediately(string route, Stream data); public delegate bool ResponseValidater(object response, out int s2c_code, out string s2c_msg); public class PushHandler { public string Route { get { return route; } } public bool IsBinary { get { return callback_bin != null; } } internal readonly PomeloClient client; internal readonly string route; private readonly Action callback; private readonly Action callback_bin; internal readonly MessageDecoder decoder; internal PushHandler(PomeloClient client, string route, Action cb, MessageDecoder dc) { this.client = client; this.route = route; this.decoder = dc; this.callback = cb; this.callback_bin = null; } internal PushHandler(PomeloClient client, string route, Action cb) { this.client = client; this.route = route; this.decoder = null; this.callback = null; this.callback_bin = cb; } internal void Invoke(object data) { try { callback(data); } catch (Exception err) { client.onError(err); } } internal void InvokeBin(byte[] data) { try { callback_bin(data); } catch (Exception err) { client.onError(err); } } public void Clear() { client.remove_push(this); } } public struct RequestHandler { public PomeloClient Client { get { return client; } } public string Route { get { return route; } } public MessageDecoder Decoder { get { return decoder; } } public bool IsBinary { get { return callback_bin != null; } } private PomeloClient client; private string route; private Action callback; private Action callback_bin; private MessageDecoder decoder; private long start_time_ms; internal RequestHandler(PomeloClient client, string route, Action callback, Action callback_bin, MessageDecoder decoder) { this.client = client; this.route = route; this.decoder = decoder; this.callback = callback; this.callback_bin = callback_bin; this.start_time_ms = CUtils.CurrentTimeMS; } internal void Invoke(object data) { try { callback(null, data); } catch (Exception err) { client.onError(err); } } internal void InvokeBin(byte[] data) { try { callback_bin(null, data); } catch (Exception err) { client.onError(err); } } internal void Invoke(PomeloException err) { try { if (callback != null) callback(err, null); else if (callback_bin != null) callback_bin(err, null); } catch (Exception err2) { client.onError(err2); } } internal bool CheckTimeout(int timeout_ms, long current_time_ms) { if (start_time_ms + timeout_ms < current_time_ms) { return true; } return false; } } #endregion }