//--------------------------------- Public methods ------------------------------------------------- public ICommand GetCommand(string pFullRequest) { var _commandAndVersion = UdpMessageParser.GetCommandNameAndVersion(pFullRequest); ICommand _cmdInstance; switch (_commandAndVersion) { case RbrApi.OnGetNextSequence: _cmdInstance = (ICommand) new OnGetNextSequence(dispatcher); break; case RbrApi.OnGetCallStats: _cmdInstance = (ICommand) new OnGetCallStats(dispatcher); break; case LoggerAPI.SetLogSeverity: _cmdInstance = new SetLogSeverity(TimokLogger.Instance); break; case LoggerAPI.GetLogSeverity: _cmdInstance = new GetLogSeverity(TimokLogger.Instance); break; default: throw (new ApplicationException("CommandFactory: Unknown command: " + pFullRequest)); } _cmdInstance.APIName = APIName; _cmdInstance.FullRequest = pFullRequest; _cmdInstance.NameAndVersion = _commandAndVersion; return(_cmdInstance); }
//-------------------------------------- Private -------------------------------------------------- private bool dispatcher(string pCmd) { try { // Create a command factory and call it to create correct CommandMessage instance: ICommand _cmd = commandFactory.GetInstance(UdpMessageParser.GetCommandNameAndVersion(pCmd)); _cmd.Sequence = UdpMessageParser.GetSequence(pCmd); _cmd.Parameters = UdpMessageParser.GetInParameters(pCmd); //Process command: _cmd.Execute(); return(_cmd.Result); } catch (Exception _ex) { T.LogCritical("Exception: " + Utils.GetFullExceptionInfo(_ex)); } return(false); }
//--------------------- Call Stats API ------------------------------------------- public CallStats GetCallStatistics(CallStatsType pCallStatsType, int pKey) { try { var _parameters = new[] { ((int)pCallStatsType).ToString(), pKey.ToString() }; //-- Send command string _strResult; var _success = udpClient.SendAndReceive(RbrApi.Name, RbrApi.OnGetCallStats, _parameters, out _strResult); if (_success) { var _outParams = UdpMessageParser.GetOutParameters(_strResult); return(new CallStats(_outParams)); } throw new Exception("Rbr communication error"); } catch (Exception _ex) { throw new Exception("Rbr communication exception", _ex); } }
public bool GetLogSeverity(out LogSeverity pLogSeverity) { pLogSeverity = LogSeverity.Debug; //default var _parameters = new string[] { }; //-- Send command string _strResult; var _result = udpClient.SendAndReceive(LoggerAPI.Name, LoggerAPI.GetLogSeverity, _parameters, out _strResult); if (_result) { var _params = UdpMessageParser.GetOutParameters(_strResult); if (_params.Length > 0 && _params[0] == "1") { pLogSeverity = (LogSeverity)Enum.Parse(typeof(LogSeverity), _params[1]); } } return(_result); }
//------------------- Rbr API ------------------------------------------------ public long GetNextSequence() { try { var _parameters = new string[] { }; //-- Send command string _strResult; var _success = udpClient.SendAndReceive(RbrApi.Name, RbrApi.OnGetNextSequence, _parameters, out _strResult); if (_success) { //T.LogRbr(LogSeverity.Debug, "RbrClient.GetNextSequence", string.Format("Returned={0}", _strResult)); var _outParams = UdpMessageParser.GetOutParameters(_strResult); if (_outParams.Length == 2 && _outParams[0] == "true") { return(long.Parse(_outParams[1])); } } throw new Exception("Rbr communication error"); } catch (Exception _ex) { throw new Exception("Rbr communication exception", _ex); } }
public ICommand GetCommand(string pFullRequest) { string _commandAndVersion = UdpMessageParser.GetCommandNameAndVersion(pFullRequest); ICommand _cmdInstance; switch (_commandAndVersion) { case LoggerAPI.SetLogSeverity: _cmdInstance = new SetLogSeverity(logger); break; case LoggerAPI.GetLogSeverity: _cmdInstance = new GetLogSeverity(logger); break; default: throw (new ApplicationException("Unknown command: " + _commandAndVersion)); } _cmdInstance.APIName = apiName; _cmdInstance.FullRequest = pFullRequest; _cmdInstance.NameAndVersion = _commandAndVersion; return(_cmdInstance); }
// TODO Move this to it's own class inside the game server. private void ProcessReceive() { var bytesReceived = Socket.ReceiveFrom(_receiveBuffer, SocketFlags.None, ref _receiveEndPoint); if (bytesReceived < 14) { return; } // TODO Exception handeling. using (var memoryStream = new MemoryStream(_receiveBuffer, 0, bytesReceived)) using (var reader = new BinaryReader(memoryStream)) { var messageHeader = UdpMessageParser.Parse <MessageHeader>(reader); if (messageHeader == null) { return; } if (Port == 5351 && messageHeader.Identity != UdpHeaderType.Connect) { return; // Ignore all packets except connect on the NAT socket. } // TODO Validate the IP-Address the message is sent from. // TODO Validate if the IP-Address is connected to the server. Console.WriteLine("UDP {0} >> {1}", Port, messageHeader); switch (messageHeader.Identity) { case UdpHeaderType.Connect: // Echo back the connect bytes. // Seed Code in this packet is the user id. reader.BaseStream.Seek(0, SeekOrigin.Begin); var outputMessage = reader.ReadBytes(bytesReceived); SendTo(outputMessage, _receiveEndPoint); break; case UdpHeaderType.Descript: if (bytesReceived < 28) { return; // Not enough bytes to parse this message. } var descriptHeader = UdpMessageParser.Parse <DescriptMessage>(reader); if (descriptHeader == null) { return; } if (bytesReceived != descriptHeader.Length) { Console.WriteLine("Error, Descript Length does not match."); return; } Console.WriteLine("Descript Header: {0}", descriptHeader); var decodedContent = ParseAndDecode(reader, descriptHeader.Length - 28); ProcessDescript(messageHeader, descriptHeader, decodedContent); break; default: Console.WriteLine("Received unknown UDP Header: {0}", messageHeader.Identity); break; } } }