|
i'm new to programming n networking, i like to know more about how bittorrent working. so, recently i start building torrent retriever. i come across this site http://dotnet.org.za/weareu/archive/2007/04/12/decoding-bencoded-streams-in-c.aspx with the following code to decode torrent file. In the (public static object BDecode(BinaryReader reader)). there have four different type return. if i called this method, how to retrieve the four different variable. thanks.
public class BEncoding
{
public static object BDecode(string filePath)
{
byte[] data = File.ReadAllBytes(filePath);
return BDecode(data);
}
public static object BDecode(Stream stream)
{
if (!stream.CanSeek)
{
throw new Exception("Your stream needs to support seeking for this algorithm");
}
BinaryReader reader = new BinaryReader(stream, System.Text.Encoding.ASCII);
return BDecode(reader);
}
public static object BDecode(byte[] data)
{
return BDecode(new MemoryStream(data));
}
public static object BDecode(BinaryReader reader)
{
char ch = reader.ReadChar();
Int64 ires = 0;
if (ch == 'd')
{
ch = (char)reader.PeekChar();
Hashtable hash = new Hashtable();
while (ch != 'e')
{
object key = BDecode(reader);
hash[key] = BDecode(reader);
ch = (char)reader.PeekChar();
}
return hash;
}
else if (ch == 'l')
{
ch = (char)reader.PeekChar();
ArrayList list = new ArrayList();
while (ch != 'e')
{
list.Add(BDecode(reader));
ch = (char)reader.PeekChar();
}
return list;
}
else if (ch == 'i')
{
ch = reader.ReadChar();
string number = "";
while (ch != 'e')
{
number += ch;
ch = reader.ReadChar();
}
return Int64.Parse(number);
}
else if (Int64.TryParse(ch.ToString(), out ires))
{
string number = "";
while (Int64.TryParse(ch.ToString(), out ires))
{
number += ch;
ch = reader.ReadChar();
}
string line = "";
for (int i = 0; i < Int64.Parse(number); i++)
{
line += reader.ReadChar();
}
return line;
}
throw (new Exception("Torrent Error"));
}
}
|
|
|
|
|
This is really badly formed code. You should rewrite it as a more type-safe method. Your original question however: check the return type. Something like this:
object result = BDecode(...);
string resultAsAString = "";
HashTable resultAsHashTable = null;
ArrayList resultAsArrayList = null;
if(result != null)
{
if(result is string)
resultAsAString = result.ToString();
else if(result is HashTable)
resultAsHashTable = result as HashTable;
else if(result is ArrayList)
resultAsArrayList = result as ArrayList;
}
This is just a kludge however. Your original method is still badly in need of a refactor
|
|
|
|
|
thanks for your valuable answer. i get it working.
|
|
|
|
|
nay one can tell me how we use joining more than one table in asp.net by inner join and outer joi
please send website name to find out
rizvan sivally
|
|
|
|
|
sheemap wrote: please send website name to find out
google[^].
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
that site is nnot accessibe please send again
rizvan sivally
|
|
|
|
|
|
What, have you lost the internet?? How is google not accessible.
Here is a nice list of EVERY google site (that I was able to find, using google)
google.de
google.com
google.at
google.pl
google.fr
google.nl
google.it
google.com.tr
google.es
google.ch
google.be
google.gr
google.com.br
google.lu
google.fi
google.pt
google.hu
google.hr
google.bg
google.com.mx
google.si
google.sk
google.ro
google.ca
google.co.uk
google.cl
google.com.ar
google.se
google.cz
google.dk
google.co.th
google.com.co
google.lt
google.co.id
google.co.in
google.co.il
google.com.eg
google.cn
google.co.ve
google.ru
google.co.jp
google.com.pe
google.com.au
google.co.ma
google.co.za
google.com.ph
google.com.sa
google.ie
google.co.kr
google.no
google.com.ec
google.com.vn
google.lv
google.com.mt
google.com.uy
google.ae
google.ba
google.co.nz
google.com.ua
google.co.cr
google.ee
google.com.do
google.com.tw
google.com.hk
google.com.my
google.com.sv
google.com.pr
google.lk
google.com.gt
google.com.bd
google.com.pk
google.is
google.li
google.com.bh
google.com.ni
google.com.py
google.com.ng
google.com.bo
google.co.ke
google.hn
google.com.sg
google.mu
google.ci
google.jo
google.nu
google.com.jm
google.com.ly
google.co.yu
google.tt
google.com.kh
google.ge
google.com.na
google.com.et
google.sm
google.cd
google.gm
google.com.qa
google.dj
google.com.cu
google.com.pa
google.gp
google.az
google.as
google.pl
google.mn
google.ht
google.md
google.am
google.sn
google.je
googlee.com
google.com.bn
google.com.ai
google.co.zm
google.ma
google.rw
google.co.ug
google.com.vc
googlenews.de
google.com
google.at
google.com.gi
google.to
google.com.om
google.kz
google.co.uz
if every single one is inaccessible, then I will do your research for you. Not before.
|
|
|
|
|
You have way too much time on your hands
|
|
|
|
|
I thought you were a web developer? surely that kind of stuff is basic to someone of your job description? thou maybe your 'web' definition is not what ours is. your 'web' doesnt even have google!!!
Oh sorry... I thought you were asking a question... but you just want a website name... how about 'CodeProject'? Now what do i find out?
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
|
Hi,
I have a small problem.I have created a form using OpenGL controls.My task is to set an image file as background to the form.I am able to set the image when the form is loading but my image is flickering on the form.I dunnow what to do.Please have a look at my code below and do tel me what is the mistake there.
code :
public Form1()
{
sName = "C:\\Winter.jpg";
InitializeComponent();
simpleOpenGlControl1.InitializeContexts();
nFstFime = 0;
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
simpleOpenGlControl1.BackgroundImage = Image.FromFile(sName);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Glu.gluOrtho2D(0, simpleOpenGlControl1.Width, 0, simpleOpenGlControl1.Height);
}
private void simpleOpenGlControl1_Paint_1(object sender, PaintEventArgs e)
{
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
if (nFstFime == 0)
{
b = new Bitmap(simpleOpenGlControl1.Width, simpleOpenGlControl1.Height);
nFstFime++;
}
b = (Bitmap)Image.FromFile(sName);
simpleOpenGlControl1.BackgroundImage = b;
Graphics g = e.Graphics;
g.DrawImage(Image.FromFile(sName), new Rectangle(0, 0, simpleOpenGlControl1.Width, simpleOpenGlControl1.Height));
}
Thnx,
Raesa
|
|
|
|
|
can some body tell me how i can save the string in to PDF file using C# .Net???
WANTED wasim khan(Killed 50 Innocent Buggs, Distroyed 200 Exception, make 5 Project Hostage) any Compnay Hire him will pay 30,000. Best place where u can get him is Sorcim Technologies Murre Road RWP
|
|
|
|
|
|
thnx
WANTED wasim khan(Killed 50 Innocent Buggs, Distroyed 200 Exception, make 5 Project Hostage) any Compnay Hire him will pay 30,000. Best place where u can get him is Sorcim Technologies Murre Road RWP
|
|
|
|
|
You can also see iTextSharp who is an adaptation of a java library who's very effective
|
|
|
|
|
Hi there,
creating a project with multiple namespaces/ classes. Basically I have two threads that each have own windows (GUIs) -> don't ask why... know that this is no criteria to launch seperate thread, need it that way anyways. Both threads use a function library, that accesses hardware (driving some motors, reading out sensors, sending commands, receiving replies...). Communication to that hardware via network.
I need to make sure that my static function library is locked if one of the threads executes read or write operations. I need the locking to be handled within this function library. So my experience up to now is that a semaphore doesn't do the job because if the semaphore is part of the function library it always is owned (semaphore "ignores" that access to my funclib is done by different threads, state of my semaphore always "owned" (obviously by my funclib) if once "taken" -> the funclib takes its implemented semaphore, not the accessing thread).
So what I need is a different method to lock my funclib or a method to identify the accessing threads in my funclib and add that info to my semaphore handling... or any other ideas?
|
|
|
|
|
I don't know the details of your implementation, but I guess you're using Windows Forms.
Remember that having more than one open Form open doesn't mean they are running on different threads.
So I will give it for granted that you are running your communications code in two separate threads you created.
I guess your function library exposes functions to start the communications and callback events to signal the end of communications or errors (timeout, wrong response, etc.). You should use lock in your library's functions (or in calls to those functions, if you don't want to modify the library) in order to achieve a first level of thread-safety, but this won't solve the problem of not having the two threads trying to communicate to the same device at the same time if the communication is in progress between calls to the various library functions (i.e. if it is asynchronous). If the communication is synchrnonous, it will be enough.
If the communication is asynchronous, using thread locking to avoid concurrent access to the devices is usually not a good practice, since communications can take a long time (especially when checking timeouts), and you would keep you threads locked during all that time, thus losing all the benefits of async comms.
In that case, you should implement a (thread-safe) queue of requests coming from the two threads. Each request will be satisfied when the instrument is "free" and the result posted in another (thread-safe) queue or sent to the calling thread by means of events or similar. All this can be implemented in a "controller" class, which will stand between your calling threads and your library. The controller might also use a simple round-robin system (with priority management, if you need it) to manage access to the library.
If you want to be more OO, you can also create a class for each type of instrument and create instances for every physical instrument. These classes will expose properties and functions to read and write the desired values to/from the devices, and will stand between your calling threads and the controller class.
Hope this can be of some help.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
rpm8200 wrote: I need to make sure that my static function library is locked if one of the threads executes read or write operations
It's my understanding that static methods are always thread safe. That if thread 1 is executing Read then thread 2 will have to wait if it calls Read.
If you want thread 2 to wait if it calls Write while thread 1 is executing Read, then simply create an object to lock on:
private static readonly object lockme = new object() ;
Then in each of your methods:
lock ( lockme ) { ... }
I don't think I've ever had to do that in static methods, but I expect it will work.
|
|
|
|
|
First of all: Thank you both for your replies on that subject.
I think I am a bit closer to finally solving my problems with semaphores. I have to do more testing but am confident up to now. I did use a codesnippet with a quite sophisticated semaphore implemented and did not work as desired. Back on msdn I reread the hints to semaphores and implemented the easiest way you can do and now seems to work.
Moreno Airoldi: Thank you for your hints. I am no C# crack but sure I know that creating a new window (I only do this for testing purposes -> the second thread later on won't have own window) does not mean to have a new thread. I forked off a thread creating that window. After first semaphore approach did not work and I almost tried anything (I at least thought so) I doubted my own implementation (really different thread??? ) and additionally did read out the thread IDs ... and... they are different (after I knew this definitly I've been wondering why I really doubted that )) ). To make long story short: Definitly there are two different threads with two different thread IDs (that I can also identify in MSProcessExplorer what I also did)
The problem why I want to lock the whole function library is not because two threads are communicating to the same device (wouldn't be a problem at all), the point is that two threads might communicate to the same object on my hardware controller. Especially the command handling is critical. A command is written to a reserved data object in the shared memory of that controller. If values (= Commands) change, the controller will examine the new command, copy it to its command register and quit the command by writing something in another shared memory as a reply. So if one thread "sends" a command and is interrupted anytime before it is acknowledged, that that command has been accepted by the hardware controller, it might happen that the other thread starts to write to the shared memory and corrupts the command.
The application on PC is a "simple" visualisation of what is going on at my hardware controller. I believe it will be no problem to lock the whole funclib while a command is wtitten (there are also other shared memories that can also be read or written that I don't want to be corrupted, only half updated and then read out by other thread aso....). Integrity/correctness of Data is primary goal.
As you pointed out, this is no good practice, but untill we have to manage communication by shared memory objects (and using another function library by the manufacturer of the controller to access that memories) this seems most practical solution (don't really need more). We have implemented timeouts to avoid deadlocks.
We are planning to do communication by ourselves and creating a tcp protocol to do communication. We wont use shared memory then and I will definitly do more work to accomplish this goal in a more OO way
PIEBALDconsult: I did already use lock to lock the single functions. Unfortunatly this is not enough as I also wanted to implement a handshake to the communication. Goal is to encapsulate "sending a command" (and other read/write operations) in a function library so that any software engineer working on that project can easily use it without dealing with eventhandling. So what I try to do is to force my funclib to do asynchronous communication synchronously (just for the goal to achieve usability and stability). Locking the single functions is not enough (unfortunatly). I really have to lock the whole static object (that might be kind of "thread-safe" itself but not in conjunction with the hardware it communicates with and the communication procedure that it has to follow).
Thank you both once again for writing me a reply! Its always interesting and helpful to know how other software engineers approach such a problem. Keep on coding!
PS: Moreno Airoldi: 2+2=5 ... can't google the solution -> whats it about?
|
|
|
|
|
Hi Guys,
I have a table that contains 100 disticnt record.(Table1)
I have a second table that contain 40 records(Table2).
when i run a script to get me a records that are in Table1 not in Table2.
Select * from Table1 where field1 not in (select field1 from Table2)
I should have at least 60 records.However ,it doesnot return anything?
Can you please advice?
Thank you very much.
|
|
|
|
|
Could we have the descrition of the 2 tables
|
|
|
|
|
Many thanks for your reply.Its Much appreciated
Table1
id int
Cli Nvarchar(200)
Timestamp datetime
100 records
Table2
id int
Cli nvarchar(200)
40 records
Select distinct(cli) from Table1 where Cli not in (select Cli from table2)
This should at least return 60 records.
Regards
Select
|
|
|
|
|
|
<blockquote class="FQ"><div class="FQA">kibromg wrote:</div>Select distinct(cli) from Table1 where Cli not in (select Cli from table2)</blockquote>
Select distinct(cli) from Table1 where Cli not in (select Cli from table2 where Cli is not null)
FTFY, SQL server has some floopy processing when it does 'not in' queries.
The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.
|
|
|
|