本文整理汇总了C#中Packet.Add方法的典型用法代码示例。如果您正苦于以下问题:C# Packet.Add方法的具体用法?C# Packet.Add怎么用?C# Packet.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Packet
的用法示例。
在下文中一共展示了Packet.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Raw
/// <summary>Packet factory that adds multiple arguments to the pakcet</summary>
public static Packet Raw(string command, params string[] args)
{
Packet p = new Packet(command);
foreach (string arg in args)
p.Add(arg);
return Parse(p.ToString());
}
示例2: GetCompletedWork
public Base GetCompletedWork(Guid jobGuid)
{
if (_ConnectionToManager == null || !_ConnectionToManager.IsConnected()) throw new Exception("Not connected to the manager");
while (true)
{
Packet p = new Packet(1100);
p.Add(jobGuid.ToByteArray(),true);
_ConnectionToManager.SendPacket(p);
Stopwatch sendTime = new Stopwatch();
sendTime.Start();
while (sendTime.ElapsedMilliseconds < _CommsTimeout)
{
if (_ConnectionToManager.GetPacketsToProcessCount() > 0)
{
foreach (Packet packet in _ConnectionToManager.GetPacketsToProcess())
{
switch (packet.Type)
{
case 1101:
return null;
case 1102:
Object[] packetObjects = packet.GetObjects();
BinaryFormatter binaryFormatter = new BinaryFormatter();
return (Base) binaryFormatter.Deserialize(new MemoryStream((Byte[]) packetObjects[0]));
}
}
Thread.Sleep(100);
}
}
if (_ConnectionToManager.IsConnected())_ConnectionToManager.Disconnect();
_ConnectionToManager.Connect(_IpAddress, _Port, 204800);
}
return null;
}
示例3: ClientConnection
/// <summary>
/// An instance of a client connection on the server
/// </summary>
/// <param name="newSocket"> </param>
/// <param name="bufferSizeInKB">The buffer size in kb for incoming packets</param>
protected ClientConnection(TcpClient newSocket, Int32 bufferSizeInKB = 1024)
{
_ByteBuffer = new Byte[bufferSizeInKB * 1024];
_ClientId = GetNewClientId();
_TimeOfConnection = DateTime.Now;
_AttachedSocket = newSocket;
_Connected = true;
Packet p = new Packet(CONNECT_PACKET);
p.Add(_ClientId);
_PacketsToSend.Add(p);
OnConnect();
_UpdateThread = new Thread(Update);
_UpdateThread.Start();
}
示例4: ClientUpdateLogic
/// <summary>
/// This is run on a seperate thread at a given interval, its useful for handeling incoming packets
/// </summary>
protected override void ClientUpdateLogic()
{
foreach (Packet packet in GetOutStandingProcessingPackets())
{
switch (packet.Type)
{
case 10:
Program.Write(((Int64)packet.GetObjects()[0]).ToString(CultureInfo.InvariantCulture));
Program.Write(((Single)packet.GetObjects()[1]).ToString(CultureInfo.InvariantCulture));
Byte[] data = ((Byte[]) packet.GetObjects()[2]);
StringBuilder sb = new StringBuilder();
foreach (Byte b in data)
{
sb.Append(b);
sb.Append(',');
}
sb.AppendLine();
foreach (Double d in (List<Double>)packet.GetObjects()[3])
{
sb.Append(d);
sb.Append(',');
}
sb.AppendLine();
foreach (Single d in (List<Single>)packet.GetObjects()[4])
{
sb.Append(d);
sb.Append(',');
}
Program.Write(sb.ToString());
Packet response = new Packet(45);
response.Add(data);
SendPacket(response);
break;
case 11:
Program.Write(((Boolean)packet.GetObjects()[0]).ToString(CultureInfo.InvariantCulture));
Program.Write(((String)packet.GetObjects()[1]).ToString(CultureInfo.InvariantCulture));
Program.Write(((Guid)packet.GetObjects()[2]).ToString());
break;
}
}
}
示例5: Main
static void Main()
{
BaseClient client = new BaseClient(); //Create an instance of the client used to connect to the server
client.Connect("127.0.0.1", 6789); //Connect to the server using the ip and port provided
while (client.IsConnected()) //While we are connected to the server
{
Packet p1 = new Packet(10); //Create an empty packet of type 10
p1.Add(DateTime.Now.Ticks); //Add to the packet a long, in this case the current time in Ticks
p1.Add(2.3f); //Add a float
p1.Add(new Byte[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}); //Add a float
p1.AddList(new List<Double>() { 10.1, 10.2, 10.3, 10.4 });
p1.AddList(new List<Single>() { 10.1f, 10.2f, 10.3f, 10.4f });
client.SendPacket(p1); //Send the packet over the connection (packet auto disposes when sent)
Packet p2 = new Packet(11); //Create an empty packet of type 10
p2.Add(true); //Add to the packet a bool
p2.Add("test cake"); //Add to the packet a string
p2.Add(Guid.NewGuid()); //Add to the packet a GUID
client.SendPacket(p2); //Send the packet over the connection (packet auto disposes when sent)
Thread.Sleep(20); //Wait for 20 ms before repeating
}
client.Disconnect();
}
示例6: Parse
/// <summary>Parse a string into a packet structure</summary>
public static Packet Parse(string packet)
{
if (packet[0] != DOLLAR)
throw new Exception("String is not a pakcet");
string[] args = packet.Split(DELIMIT, STAR);
Packet p = new Packet(args[0].Substring(1));
for (int i = 1; i < args.Length - 1; i++)
p.Add(args[i]);
byte crc = Convert.ToByte(args[args.Length - 1], 16);
if (crc != p.CRC)
throw new Exception("Packet has incorrect checksum");
return p;
}
示例7: HandelA1000
/// <summary>
/// Handels a packet of type 1000, this packet should be used to sent a work request
/// </summary>
/// <param name="p"></param>
protected void HandelA1000(Packet p)
{
Object[] packetObjects = p.GetObjects();
Guid jobGuid = Guid.NewGuid();
Byte[] jobData = (Byte[])packetObjects[0];
Base activity = null;
try
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
activity = (Base) binaryFormatter.Deserialize(new MemoryStream(jobData));
activity.SetGuid(jobGuid);
Packet returnPacket = new Packet(1001);
returnPacket.Add(jobGuid.ToByteArray(),true);
SendPacket(returnPacket);
Program.AddJob(activity);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
示例8: DemoAnimation
private static Packet DemoAnimation()
{
var text = new TextFile('A') {
new TextFileLine("<speed 5 /><picture 1/>", DisplayMode.Hold),
new TextFileLine("<speed 5 /><picture 2/>", DisplayMode.Hold),
new TextFileLine("<speed 5 /><picture 3/>", DisplayMode.Hold),
new TextFileLine("<speed 5 /><picture 4/>", DisplayMode.Hold),
new TextFileLine("<speed 5 /><picture 5/>", DisplayMode.Hold),
new TextFileLine("<speed 5 /><picture 6/>", DisplayMode.Hold),
new TextFileLine("<speed 5 /><picture 6/>", DisplayMode.Hold),
new TextFileLine("<speed 5 /><picture 5/> <picture 3/> <picture 1/> <picture 2/>", DisplayMode.Hold),
new TextFileLine("<speed 5 /><picture 4/> <picture 2/> <picture 1/> <picture 3/>", DisplayMode.Hold),
new TextFileLine("<speed 5 /><picture 3/> <picture 1/> <picture 2/> <picture 4/>", DisplayMode.Hold),
new TextFileLine("<speed 5 /><picture 2/> <picture 1/> <picture 3/> <picture 5/>", DisplayMode.Hold),
new TextFileLine("<speed 5 /><picture 1/> <picture 2/> <picture 4/> <picture 6/>", DisplayMode.Hold),
};
var pic1 = new PictureFile('1', "kirby/frame1.png", ColorFormat.Monochrome);
var pic2 = new PictureFile('2', "kirby/frame2.png", ColorFormat.Monochrome);
var pic3 = new PictureFile('3', "kirby/frame3.png", ColorFormat.Monochrome);
var pic4 = new PictureFile('4', "kirby/frame4.png", ColorFormat.Monochrome);
var pic5 = new PictureFile('5', "kirby/frame5.png", ColorFormat.Monochrome);
var pic6 = new PictureFile('6', "kirby/frame6.png", ColorFormat.Monochrome);
var packet = new Packet();
packet.SetMemory(new FileTable {
text,
pic1, pic2, pic3, pic4, pic5, pic6
});
packet.Add(new WriteTextCommand(text));
packet.Add(new WritePictureCommand(pic1));
packet.Add(new WritePictureCommand(pic2));
packet.Add(new WritePictureCommand(pic3));
packet.Add(new WritePictureCommand(pic4));
packet.Add(new WritePictureCommand(pic5));
packet.Add(new WritePictureCommand(pic6));
return packet;
}
示例9: HandelA1100
protected void HandelA1100(Packet p)
{
Object[] dataObjects = p.GetObjects();
Guid jobLookupID = new Guid((Byte[])dataObjects[0]);
Base activity = Program.GetCompletedJobByGuid(jobLookupID);
if (activity == null) SendPacket(new Packet(1101));
else
{
Packet returnPacket = new Packet(1102);
BinaryFormatter binaryFormatter = new BinaryFormatter();
MemoryStream datapackage = new MemoryStream();
binaryFormatter.Serialize(datapackage, activity);
returnPacket.Add(datapackage.ToArray(),true);
SendPacket(returnPacket);
}
}
示例10: OnPing
/// <summary>
/// Reponse au ping du client
/// </summary>
/// <param name="dp"></param>
public void OnPing(DataPacket dp)
{
Packet pak = new Packet((Int16)10);
pak.Add<UInt32>(dp.Read<UInt32>());
this.Send(pak);
}
示例11: DemoStrings
private static void DemoStrings(IConnection conn)
{
var packet = new Packet();
var text = new TextFile('A') {
{"<string C/>", DisplayMode.Scroll}
};
packet.SetMemory(new FileTable {
{'C', new StringFileInfo(10)},
text
});
packet.Add(new WriteTextCommand(text));
conn.Send(packet);
for(int i = 0; i < 60; i++)
{
packet = new Packet();
packet.Add(new WriteStringCommand(new StringFile('C', i.ToString("000"))));
conn.Send(packet);
Thread.Sleep(1000);
}
}
示例12: ToPacket
public virtual Packet ToPacket()
{
Packet p = new Packet(PACKETIDENTIFIER);
p.Add(_ActivityInstanceIdentifier.ToByteArray(),true);
return p;
}
示例13: HandelA200Packet
/// <summary>
/// Handels and incoming packet with ID 200, this is a hello packet from the lobe manager to which we repsond
/// with the number of worker threads we ahve running
/// </summary>
private void HandelA200Packet()
{
Packet responsePacket = new Packet(201);
responsePacket.Add(_ActiveWorkerServices.Count);
_ConnectionToLobeManager.SendPacket(responsePacket);
}
示例14: Start
public void Start(Settings settings)
{
_Settings = settings;
_CanRequestWork = true;
AnnounceStatus("Lobe Worker Launching");
_Running = true;
_TimeOfLastManagerComms = DateTime.Now;
_TargetThreads = _Settings.WorkerThreadCount;
//Prepare the worker threads
AnnounceStatus("Preparing Workers");
for (Int32 i = 0; i < _TargetThreads; i++)
{
lock (_ActiveWorkerServices)
{
_ActiveWorkerServices.Add(new WorkerThread(this));
}
}
AnnounceStatus("Connecting To Manager");
lock (_PacketsToBeProcessed)
{
if (!_ConnectionToLobeManager.Connect(_Settings.CommsManagerIp, _Settings.CommsManagerPort, 20480 * 1024)) AnnounceStatus("Unable to communicate with specified lobe manager, aborting!");
}
AnnounceStatus("Lobe Worker Online");
while (_Running || _ActiveWorkerServices.Count > 0)
{
lock (this)
{
_ActiveWorkerServices = _ActiveWorkerServices.Where(a => a.IsRunning()).ToList();
if (_CurrentThreads < _TargetThreads && _Running)
{
_ActiveWorkerServices.Add(new WorkerThread(this));
}
if (!_ConnectionToLobeManager.IsConnected())
{
String error = _ConnectionToLobeManager.GetError();
if (!string.IsNullOrEmpty(error)) Console.WriteLine("Net Error " + error);
AnnounceStatus("Unable to communicate with specified lobe manager, Attempting to reconnect");
if (_ConnectionToLobeManager.Connect(_Settings.CommsManagerIp, _Settings.CommsManagerPort, 20480 * 1024)) AnnounceStatus("Connection Re-established");
_CanRequestWork = true;
}
else
{
lock (_CompletedWork)
{
if (_CompletedWork.Count > 0)
{
using (Base job = _CompletedWork[0])
{
_CompletedWork.RemoveAt(0);
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream datapackage = new MemoryStream())
{
binaryFormatter.Serialize(datapackage, job);
_CompletedJobs++;
Packet responsePacket = new Packet(400);
responsePacket.Add(datapackage.ToArray(), true);
_ConnectionToLobeManager.SendPacket(responsePacket);
if (File.Exists(String.Format("{0}/{1}", _Settings.CompletedWorkDirectory, job.GetGuid())))
{
File.Delete(String.Format("{0}/{1}", _Settings.CompletedWorkDirectory, job.GetGuid()));
}
}
}
}
}
lock (_PendingWork)
{
if (_PendingWork.Count < _Settings.WorkBufferCount && _Running && _CanRequestWork)
{
Packet p = new Packet(300); //Generate a work request packet
_ConnectionToLobeManager.SendPacket(p);
_CanRequestWork = false;
}
}
lock (_ConnectionToLobeManager)
{
if (_ConnectionToLobeManager.GetPacketsToProcessCount() > 0)
{
//manager is communicating
_TimeOfLastManagerComms = DateTime.Now;
lock (_PacketsToBeProcessed)
{
_PacketsToBeProcessed.AddRange(_ConnectionToLobeManager.GetPacketsToProcess());
}
}
if (DateTime.Now - _TimeOfLastManagerComms > _TimeBeforeManagerConsideredLost)
{
//Disconnect as it seems the manager isnt there , this will cause an attempted reconnect above
_ConnectionToLobeManager.Disconnect();
}
}
lock (_PacketsToBeProcessed)
{
//.........这里部分代码省略.........
示例15: HandelA300
/// <summary>
/// Handels a packet of type 300, This is a work request packet from the lobe worker, these will be recieved
/// regulary if the worker has no work at this stage. Having the workers poll the manager better fits the
/// parent child model and reliance pathways.
/// </summary>
/// <param name="p"></param>
protected void HandelA300(Packet p)
{
Base work = Program.GetPendingJob();
if (work == null) SendPacket(new Packet(301)); //got no work
else
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
MemoryStream datapackage = new MemoryStream();
binaryFormatter.Serialize(datapackage, work);
Packet responsePacket = new Packet(302);
responsePacket.Add(datapackage.ToArray(),true);
SendPacket(responsePacket);
}
}