public virtual byte[] HandleRequest(byte[] buff, GXDLMSConnectionEventArgs connectionInfo)
{
if (buff == null || buff.Length == 0)
{
return null;
}
if (!Initialized)
{
throw new Exception("Server not Initialized.");
}
try
{
receivedData.Set(buff);
bool first = Settings.ServerAddress == 0 && Settings.ClientAddress == 0;
GXDLMS.GetData(Settings, receivedData, info);
//If all data is not received yet.
if (!info.IsComplete)
{
return null;
}
receivedData.Clear();
if (first)
{
// Check is data send to this server.
if (!IsTarget(Settings.ServerAddress,
Settings.ClientAddress))
{
info.Clear();
return null;
}
}
//If client want next frame.
if ((info.MoreData & RequestTypes.Frame) == RequestTypes.Frame)
{
return GXDLMS.GetHdlcFrame(Settings, Settings.ReceiverReady(), replyData);
}
//Update command if transaction and next frame is asked.
if (info.Command == Command.None)
{
if (transaction != null)
{
info.Command = transaction.command;
}
}
//Check inactivity time out.
if (hdlcSetup != null)
{
if (info.Command == Command.Snrm)
{
dataReceived = DateTime.Now;
}
else
{
int elapsed = (int)(DateTime.Now - dataReceived).TotalSeconds;
//If inactivity time out is elapsed.
if (elapsed >= hdlcSetup.InactivityTimeout)
{
Reset();
dataReceived = DateTime.MinValue;
return null;
}
dataReceived = DateTime.Now;
}
}
byte[] reply = HandleCommand(info.Command, info.Data, connectionInfo);
info.Clear();
return reply;
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
if (info.Command != Command.None)
{
return ReportError(info.Command, ErrorCode.HardwareFault);
}
else
{
Reset();
if (Settings.Connected)
{
Settings.Connected = false;
Disconnected(connectionInfo);
}
return null;
}
}
}