123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- 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_
- /// <summary>
- /// 网络状态
- /// </summary>
- 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<object> callback;
- private readonly Action<byte[]> callback_bin;
- internal readonly MessageDecoder decoder;
- internal PushHandler(PomeloClient client, string route, Action<object> 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<byte[]> 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<PomeloException, object> callback;
- private Action<PomeloException, byte[]> callback_bin;
- private MessageDecoder decoder;
- private long start_time_ms;
- internal RequestHandler(PomeloClient client, string route, Action<PomeloException, object> callback, Action<PomeloException, byte[]> 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
- }
|