本文整理汇总了C#中Lidgren.Network.NetPeer类的典型用法代码示例。如果您正苦于以下问题:C# NetPeer类的具体用法?C# NetPeer怎么用?C# NetPeer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NetPeer类属于Lidgren.Network命名空间,在下文中一共展示了NetPeer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
s_mainForm = new Form1();
WriteToConsole("Type 'connect <host> <port>' to connect to another peer, or 'connect <port>' to connect to another localhost peer");
NetConfiguration config = new NetConfiguration("p2pchat");
config.MaxConnections = 256;
s_peer = new NetPeer(config);
//s_peer.VerboseLog = true;
s_peer.SetMessageTypeEnabled(NetMessageType.ConnectionRejected | NetMessageType.BadMessageReceived | NetMessageType.VerboseDebugMessage, true);
// start listening for incoming connections
s_peer.Start();
// create a buffer to read data into
s_readBuffer = s_peer.CreateBuffer();
Application.Idle += new EventHandler(OnAppIdle);
Application.Run(s_mainForm);
s_peer.Shutdown("Application exiting");
}
示例2: ShellCallback
public static void ShellCallback(MessageBase msg, NetPeer peer, NetBuffer buff)
{
if(CallbackMapping.ContainsKey(msg.Id))
{
CallbackMapping[msg.Id](peer, buff, msg);
}
}
示例3: Connection
/// <summary>
/// Constructor
/// </summary>
/// <param name="manager">Local NetPeer</param>
/// <param name="con">NetConnection</param>
/// <param name="key">Encryption key</param>
/// <param name="nodeId">Node Id</param>
public Connection(NetPeer manager, NetConnection con, INetEncryption encryption, String nodeId)
{
// This keep alive timer is used to overcome BAD connections that drop ping/pong messages a lot.
// It simply sends an empty message every five seconds. As long as some messages are retained
// and received on the connection, all will be well
_keepAliveTimer = new System.Timers.Timer(5000);
_keepAliveTimer.Elapsed += new ElapsedEventHandler((Object state, ElapsedEventArgs args) =>
this.SendMessage(this.NetManager.CreateMessage(0), NetDeliveryMethod.Unreliable));
this.Username = (con.Tag as Handshake).Username;
this.NetManager = manager;
this.NetConnection = con;
this.NetConnection.Tag = this;
this.NodeId = nodeId;
if (encryption != null)
{
_netEncryption = encryption;
}
else
{
// You could write code that makes it possible to transfer users between server
// where you don't have any encryption key YET. The place to start that would
// be here.
this.IsTransfering = true;
_netEncryption = new NetXtea(new Byte[16]);
}
// Not connected until everything is done.
System.Threading.Thread.MemoryBarrier();
_connected = true;
_keepAliveTimer.Start();
}
示例4: NetXtea
/// <summary>
/// 16 byte key
/// </summary>
public NetXtea(NetPeer peer, byte[] key, int rounds)
: base(peer)
{
if (key.Length < c_keySize)
throw new NetException("Key too short!");
m_numRounds = rounds;
m_sum0 = new uint[m_numRounds];
m_sum1 = new uint[m_numRounds];
uint[] tmp = new uint[8];
int num2;
int index = num2 = 0;
while (index < 4)
{
tmp[index] = BitConverter.ToUInt32(key, num2);
index++;
num2 += 4;
}
for (index = num2 = 0; index < 32; index++)
{
m_sum0[index] = ((uint)num2) + tmp[num2 & 3];
num2 += -1640531527;
m_sum1[index] = ((uint)num2) + tmp[(num2 >> 11) & 3];
}
}
示例5: NetCryptoProviderBase
public NetCryptoProviderBase(NetPeer peer, SymmetricAlgorithm algo)
: base(peer)
{
m_algorithm = algo;
m_algorithm.GenerateKey();
m_algorithm.GenerateIV();
}
示例6: NetworkPeer
/// <summary>
/// Creates and starts a new NetworkPeer object, for servers.
/// </summary>
/// <param name="port">The port the server should listen on.</param>
/// <param name="protocols">An array of messaging protocols.</param>
public NetworkPeer(int port, params Protocol[] protocols)
{
Protocols = new List<Protocol>(protocols);
netPeer = new NetPeer(new NetPeerConfiguration("N/A").GetStandardConfiguration(port));
netPeer.RegisterReceivedCallback(OnMessageReceivedCallback);
Start();
}
示例7: Peer
public Peer(NetPeer peer)
{
this.peer = peer;
//start the client
peer.Start();
}
示例8: Program
public Program()
{
NetPeerConfiguration config = new NetPeerConfiguration("masterserver");
config.SetMessageTypeEnabled(NetIncomingMessageType.UnconnectedData, true);
config.Port = API.MasterServer.MasterServerPort;
peer = new NetPeer(config);
peer.Start();
}
示例9: ToOutgoing
internal NetOutgoingMessage ToOutgoing(NetPeer Sender)
{
NetOutgoingMessage msg = Sender.CreateMessage("__msg");
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
serializer.Serialize(writer, this);
msg.Write(sb.ToString());
return msg;
}
示例10: OnLoginMessage
public void OnLoginMessage(NetPeer server, NetBuffer buff, MessageBase msg)
{
var srv = server as NetServer;
var im = buff as NetIncomingMessage;
if (srv != null && im != null)
{
LoginMessageImpl(srv, im, msg);
}
}
示例11: OnLoginMessage
public void OnLoginMessage(NetPeer client, NetBuffer buff, MessageBase msg)
{
var srv = client as NetClient;
var im = buff as NetIncomingMessage;
if (srv != null && im != null)
{
LoginResultMessageImpl(srv, im, msg);
}
}
示例12: Main
static void Main(string[] args)
{
NetPeerConfiguration config = new NetPeerConfiguration("unittests");
config.EnableUPnP = true;
NetPeer peer = new NetPeer(config);
peer.Start(); // needed for initialization
Console.WriteLine("Unique identifier is " + NetUtility.ToHexString(peer.UniqueIdentifier));
ReadWriteTests.Run(peer);
NetQueueTests.Run();
MiscTests.Run(peer);
BitVectorTests.Run();
EncryptionTests.Run(peer);
var om = peer.CreateMessage();
peer.SendUnconnectedMessage(om, new IPEndPoint(IPAddress.Loopback, 14242));
try
{
peer.SendUnconnectedMessage(om, new IPEndPoint(IPAddress.Loopback, 14242));
}
catch (NetException nex)
{
if (nex.Message != "This message has already been sent! Use NetPeer.SendMessage() to send to multiple recipients efficiently")
throw nex;
}
peer.Shutdown("bye");
// read all message
NetIncomingMessage inc = peer.WaitMessage(5000);
while (inc != null)
{
switch (inc.MessageType)
{
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.VerboseDebugMessage:
case NetIncomingMessageType.WarningMessage:
case NetIncomingMessageType.ErrorMessage:
Console.WriteLine("Peer message: " + inc.ReadString());
break;
case NetIncomingMessageType.Error:
throw new Exception("Received error message!");
}
inc = peer.ReadMessage();
}
Console.WriteLine("Done");
Console.ReadKey();
}
示例13: PeerListener
public void PeerListener(NetPeer peer)
{
while (true)
{
NetIncomingMessage msg;
while ((msg=peer.ReadMessage())!=null)
{
NetConnection connection = msg.SenderConnection;
switch (msg.MessageType)
{
case NetIncomingMessageType.StatusChanged:
switch (msg.SenderConnection.Status)
{
case NetConnectionStatus.Connected:
logger.Info("{0}: connected",connection.RemoteEndPoint.Address.ToString());
ExecuteCommand(new CmdInitOk(connection));
break;
case NetConnectionStatus.Disconnected:
logger.Info("{0}: disconnected", connection.RemoteEndPoint.Address.ToString());
break;
case NetConnectionStatus.Disconnecting:
logger.Info("{0}: disconnecting", connection.RemoteEndPoint.Address.ToString());
break;
}
break;
case NetIncomingMessageType.Data:
msg.Decrypt(new NetRC2Encryption(peer, connection.CryptoKey));
string[] OpData = msg.ReadString().Split('&');
var OpCode = (LoginOperation)Enum.Parse(typeof(LoginOperation), OpData[0]);
switch (OpCode)
{
case LoginOperation.AuthLog:
string[] authData = OpData[1].Split('#');
string login = authData[0];
string pass = authData[1];
ExecuteCommand(new CmdLoginOkFail(connection, login,pass));
break;
}
break;
}
peer.Recycle(msg);
}
}
}
示例14: Host
public void Host()
{
// Create server and tell it to start listening for connections.
Config.Port = 25000;
NetServer server = new NetServer(Config);
server.Start();
// Assign it to NetPeer so it can be used interchangibly with client.
Peer = server;
Console.WriteLine("Server has started.");
}
示例15: Start
public void Start()
{
peer = new NetPeer(config);
peer.Start();
localIdentifier = peer.UniqueIdentifier;
listener = new peerListener(peer, localIdentifier);
moveQueue = new Queue<string>();
NetThread = new Thread(new ThreadStart(listener.processNetwork));
NetThread.Start();
}