public static void Log(string text, Exception e = null) { #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_ANDROID || UNITY_IOS if (e != null) { Debug.LogError($"{text} -> {e.Message}\n {e.StackTrace}"); } else if (verboseLog) { Debug.Log(text); } #else if (e != null) { ServerLog.Error($"{text} -> {e.Message}\n {e.StackTrace}"); } else { if (text.Contains(">")) { if (verboseLog) { ServerLog.Debug(text); } } else { ServerLog.Info(text); } } #endif }
public int Send(byte[][] data) { try { if (Link != null) { lock (Link) if (Link.Connected) { for (int i = 0; i < data.Length; i++) { Link.Send(data[i]); } return(1); } else { return(-1); } } } catch (Exception ex) { ServerLog.Error(ex.StackTrace); return(-1); } return(0); }
public int Send(byte[] data, byte type = EnvelopeType.Mate) { try { if (Link != null) { lock (Link) if (Link.Connected) { var ss = envelope.Pack(data, type); for (int i = 0; i < ss.Length; i++) { Link.Send(ss[i]); } return(1); } else { return(-1); } } } catch (Exception ex) { ServerLog.Error(ex.StackTrace); return(-1); } return(0); }
void Run() { byte[] buffer = new byte[65536]; IPEndPoint ip = new IPEndPoint(IPAddress.Any, 0); while (running) { try { EndPoint end = ip; int len = 0; try { len = soc.ReceiveFrom(buffer, ref end);//接收数据报 } catch { } if (len > 0) { byte[] dat = new byte[len]; for (int i = 0; i < len; i++) { dat[i] = buffer[i]; } Dispatch(dat, end as IPEndPoint); } } catch (Exception ex) { ServerLog.Error(ex.StackTrace); } } }
void AcceptClient() { while (true) { try { var client = soc.Accept(); CreateLink(client); } catch (Exception ex) { ServerLog.Error(ex.StackTrace); } } }
void Run() { while (running) { var now = DateTime.Now.Ticks; try { Recive(); int c = queue.Count; for (int i = 0; i < c; i++) { var mis = queue.Dequeue(); if (mis != null) { if (mis.action != null) { mis.action(mis.data); } } } } catch (Exception ex) { ServerLog.Error(ex.StackTrace); } try { if (soc != null) { SendAll(soc, now); } } catch { } long t = DateTime.Now.Ticks; t -= now; t /= 10000; if (t < 10) { Thread.Sleep(1); } } }
void Run() { byte[] buffer = new byte[65536]; IPEndPoint ip = new IPEndPoint(IPAddress.Any, 0); while (running) { try { EndPoint end = ip; int len = soc.ReceiveFrom(buffer, ref end);//接收数据报 if (len > 0) { byte[] dat = new byte[len]; for (int i = 0; i < len; i++) { dat[i] = buffer[i]; } var env = FindEnvelope(end as IPEndPoint); if (Packaging) { var data = env.envelope.Unpack(dat, len); for (int i = 0; i < data.Count; i++) { var item = data[i]; EnvelopeCallback(item.data, item.type, env); } } else { EnvelopeCallback(dat, 0, env); } } } catch (Exception ex) { ServerLog.Error(ex.StackTrace); } } }
public TcpServer(string ip, int port, PackType type = PackType.Part, int thread = 8) { tCount = thread; packType = type; soc = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); soc.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); //端点 endPoint = new IPEndPoint(IPAddress.Parse(ip), port); //绑定 try { soc.Bind(endPoint); } catch (Exception ex) { ServerLog.Error(ex.StackTrace); } soc.Listen(0); soc.ReceiveTimeout = 1000; Instance = this; if (tCount > 0) { linkBuff = new LinkThread <T> [tCount]; for (int i = 0; i < tCount; i++) { linkBuff[i] = new LinkThread <T>(SingleCount); linkBuff[i].soc = soc; } } else { tCount = 1; linkBuff = new LinkThread <T> [1]; linkBuff[0] = new LinkThread <T>(SingleCount); linkBuff[0].soc = soc; } }
public void Start() { if (server == null) { server = new Thread(AcceptClient); server.Start(); } if (threadTimer == null) { threadTimer = new ThreadTimer(); threadTimer.Interal = 1000; threadTimer.Tick = (o, e) => { try { Heartbeat(); } catch (Exception ex) { ServerLog.Error(ex.StackTrace); } }; } }