Author |
|
waqas Intermediate
Joined: April 07 2015 Location: United Kingdom Posts: 3
|
Posted: April 27 2015 at 10:08am | IP Logged
|
|
|
I am trying to understand this code but its very
confusing. Its from LanScape Sample projects that came
when we bought the API. I understand Monitor and
WaitHandle mechanics in .NET framework but cannot
understand this code.I would be grateful if anyone can
explain it to me.
Thanks
//////////////////////////////////////////////////////
////////////////////
//
// Copyright (c) 1987-2009 LanScape Corporation.
// All Rights Reserved.
//
// Permission is hereby granted to use and develop
additional software
// applications using the software contained within
this source code module.
// Redistribution of this source code in whole or in
part is strictly
// prohibited.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS"
WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING
BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
FITNESS FOR A
// PARTICULAR PURPOSE.
//
// IN NO EVENT SHALL LANSCAPE BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT,
// SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
INCLUDING LOST PROFITS,
// ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
DOCUMENTATION, EVEN IF
// LANSCAPE HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
//
// LANSCAPE SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
DOCUMENTATION, IF
// ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
LANSCAPE HAS NO OBLIGATION
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
ENHANCEMENTS, OR MODIFICATIONS.
//
//////////////////////////////////////////////////////
////////////////////
using System;
using System.Collections.Generic;
using System.Threading;
// A generic fifo template class.
//
public class Fifo<T>
{
public bool Initialized = false;
public Object FifoLock = null;
public Queue<T> FifoQueue = null;
public EventWaitHandle Event = null;
public Fifo()
{
// create the fifo.
FifoQueue = new Queue<T>();
if (FifoQueue != null)
{
// create the fifo lock for
thread safety.
FifoLock = new Object();
if(FifoLock != null)
{
// create the
signaling event.
Event = new
EventWaitHandle(false, EventResetMode.AutoReset);
if (Event != null)
{
Initialized =
true;
}
}
}
}
~Fifo()
{
FifoLock = null;
FifoQueue = null;
Event = null;
}
public bool FifoWrite(T FifoData)
{
bool ret = false;
bool Err = false;
lock (FifoLock)
try
{
FifoQueue.Enqueue(FifoData);
}
catch
{
Err = true;
}
if (!Err)
{
// signal other threads to
run.
Event.Set();
// success.
ret = true;
}
return (ret);
}
// read a fifo element- wait forever.
//
public bool FifoRead(ref T FifoData)
{
return(FifoReadTimeout(ref FifoData,
Timeout.Infinite));
}
// read a fifo element- wait until the
specified Ms timeout.
//
public bool FifoReadTimeout(ref T FifoData,
int TimeOutMs)
{
bool ret = false;
bool Err = false;
// wait for an element to be placed in
// the FifoData.
if (Event.WaitOne(TimeOutMs, false))
{
lock (FifoLock)
try
{
FifoData =
FifoQueue.Dequeue();
}
catch
{
Err = true;
}
if (FifoQueue.Count > 0)
{
// reset the event.
additional data is in the fifo.
Event.Set();
}
if (!Err)
{
ret = true;
}
}
else
{
// we timed out.
}
return(ret);
}
}
|
Back to Top |
|
|
support Administrator
Joined: January 26 2005 Location: United States Posts: 1666
|
Posted: April 27 2015 at 10:21am | IP Logged
|
|
|
Hi,
I will be back in the office tomorrow. I can explain then.
RJ
|
Back to Top |
|
|
support Administrator
Joined: January 26 2005 Location: United States Posts: 1666
|
Posted: April 28 2015 at 6:00am | IP Logged
|
|
|
The code you posted is for a simple fifo object implemented as a C# generic class. You can create an instance of the fifo object for a specific type of element as you see fit.
After you create an object of the fifo class type, you can then write objects to the fifo from some thread and then read objects from the same thread or some other thread. The fifo class is thread safe.
Use Google to search for “C# generics”.
RJ
|
Back to Top |
|
|
waqas Intermediate
Joined: April 07 2015 Location: United Kingdom Posts: 3
|
Posted: April 28 2015 at 7:30am | IP Logged
|
|
|
Thank you for your reply.
Could you please kindly explain non generic stuff
inside the constructor and within FifoReadTimeout and
FifoWrite functions.
This is my sample code
class CallFactory
{
Queue<InboundCall> _calls = new
Queue<InboundCall>();
public CallFactory()
{
}
public InboundCall GetCall()
{
lock (_calls)
{
while (_calls.Count == 0)
{
Monitor.Wait(_calls);
}
return _calls.Dequeue();
}
}
public void AddCall(InboundCall[]
callCollection)
{
lock (_calls)
{
foreach (InboundCall call in
callCollection)
{
_calls.Enqueue(call);
}
Monitor.PulseAll(_calls);
}
}
}
and this is simple read and write what you have
mentioned in the reply"You can create an instance of
the fifo object for a specific type of element as you
see fit.
After you create an object of the fifo class type, you
can then write objects to the fifo from some thread
and then read objects from the same thread or some
other thread" and make sense but your code does not
give clear picture what its doing. I would be very
grateful if you could clarify the requested part of
the code
Many Thanks
Waqas
|
Back to Top |
|
|
waqas Intermediate
Joined: April 07 2015 Location: United Kingdom Posts: 3
|
Posted: April 29 2015 at 8:48am | IP Logged
|
|
|
It looks like lanscape are not happy with my question
|
Back to Top |
|
|
support Administrator
Joined: January 26 2005 Location: United States Posts: 1666
|
Posted: May 05 2015 at 11:14am | IP Logged
|
|
|
Hi,
I am not sure what you are asking. Free support is limited to question regarding the functionality of the VOIP SDK and its configuration.
If you need help understanding software examples or if you would like us to assist you in developing your software application, please let us know. We offer a paid support option. See the LanScape home page for "enhanced customer support".
Thanks,
RJ
|
Back to Top |
|
|