/// <summary> /// Bind and listen /// </summary> public void Listen() { try { IPAddress addr = IPAddress.Parse(localIP); IPEndPoint ep = new IPEndPoint(addr, port); _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); _socket.Bind(ep); int count = config.GetInt("socket", "max_client_count"); _socket.Listen(count); _logger.LogInfo("listen success", "Server", true); } catch (Exception e) { _logger.LogError(e.Message, "Server", true); } }
/// <summary> /// **WITHIN THREAD** /// The entrance of protocol bytes /// </summary> /// <param name="data"></param> public void ProtocolEntry(byte[] data) { string json = FF.BytesToString(data); HoxisProtocol proto = FF.JsonToObject <HoxisProtocol>(json); switch (proto.type) { case ProtocolType.Synchronization: switch (proto.receiver.type) { case ReceiverType.Cluster: if (realtimeData.parentCluster == null) { return; } realtimeData.parentCluster.ProtocolBroadcast(proto); break; case ReceiverType.Team: if (realtimeData.parentTeam == null) { return; } realtimeData.parentTeam.ProtocolBroadcast(proto); break; case ReceiverType.User: HoxisUser user = HoxisServer.Ins.GetUser(proto.receiver.uid); // todo send break; } break; case ProtocolType.Request: // Request check Ret ret; CheckRequest(proto, out ret); if (ret.code != 0) { ResponseError(proto.handle, C.RESP_CHECK_FAILED, ret.desc); return; } // Check ok if (!respTable.ContainsKey(proto.action.method)) { if (DebugRecorder.LogEnable(_logger)) { _logger.LogError(FF.StringFormat("invalid request: {0}", proto.action.method), "", true); } ResponseError(proto.handle, C.RESP_CHECK_FAILED, FF.StringFormat("invalid request: {0}", proto.action.method)); return; } respTable[proto.action.method](proto.handle, proto.action.args); break; default: if (DebugRecorder.LogEnable(_logger)) { _logger.LogError(FF.StringFormat("invalid protocol type: {0}", proto.type), ""); } break; } }