You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-15 22:56:30 +09:00
RegisterReceivedCallback added to get a callback using SynchronizationContext when a message arrives
This commit is contained in:
@@ -55,6 +55,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Encryption\INetEncryption.cs" />
|
||||
<Compile Include="Encryption\NetBCrypt.cs" />
|
||||
<Compile Include="Encryption\NetBlockEncryptionBase.cs" />
|
||||
<Compile Include="Encryption\NetXorEncryption.cs" />
|
||||
<Compile Include="Encryption\NetXteaEncryption.cs" />
|
||||
|
||||
@@ -34,12 +34,20 @@ namespace Lidgren.Network
|
||||
internal long m_uniqueIdentifier;
|
||||
|
||||
private AutoResetEvent m_messageReceivedEvent = new AutoResetEvent(false);
|
||||
private List<NetTuple<SynchronizationContext, SendOrPostCallback>> m_receiveCallbacks;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the socket, if Start() has been called
|
||||
/// </summary>
|
||||
public Socket Socket { get { return m_socket; } }
|
||||
|
||||
public void RegisterReceivedCallback(SendOrPostCallback callback)
|
||||
{
|
||||
if (m_receiveCallbacks == null)
|
||||
m_receiveCallbacks = new List<NetTuple<SynchronizationContext, SendOrPostCallback>>();
|
||||
m_receiveCallbacks.Add(new NetTuple<SynchronizationContext, SendOrPostCallback>(SynchronizationContext.Current, callback));
|
||||
}
|
||||
|
||||
internal void ReleaseMessage(NetIncomingMessage msg)
|
||||
{
|
||||
NetException.Assert(msg.m_incomingMessageType != NetIncomingMessageType.Error);
|
||||
@@ -51,8 +59,15 @@ namespace Lidgren.Network
|
||||
}
|
||||
|
||||
m_releasedIncomingMessages.Enqueue(msg);
|
||||
|
||||
if (m_messageReceivedEvent != null)
|
||||
m_messageReceivedEvent.Set();
|
||||
|
||||
if (m_receiveCallbacks != null)
|
||||
{
|
||||
foreach (var tuple in m_receiveCallbacks)
|
||||
tuple.Item1.Post(tuple.Item2, this);
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeNetwork()
|
||||
|
||||
@@ -63,7 +63,7 @@ namespace Lidgren.Network
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Computer private key
|
||||
/// Computer private key (x)
|
||||
/// </summary>
|
||||
public static byte[] ComputePrivateKey(string username, string password, byte[] salt)
|
||||
{
|
||||
@@ -85,8 +85,6 @@ namespace Lidgren.Network
|
||||
/// </summary>
|
||||
public static byte[] ComputeServerVerifier(byte[] privateKey)
|
||||
{
|
||||
var sha = GetHashAlgorithm();
|
||||
|
||||
NetBigInteger x = new NetBigInteger(NetUtility.ToHexString(privateKey), 16);
|
||||
|
||||
// Verifier (v) = g^x (mod N)
|
||||
@@ -95,6 +93,15 @@ namespace Lidgren.Network
|
||||
return serverVerifier.ToByteArrayUnsigned();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SHA hash data
|
||||
/// </summary>
|
||||
public static byte[] Hash(byte[] data)
|
||||
{
|
||||
var sha = GetHashAlgorithm();
|
||||
return sha.ComputeHash(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute client public ephemeral value (A)
|
||||
/// </summary>
|
||||
@@ -124,7 +131,7 @@ namespace Lidgren.Network
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute intermediate value U
|
||||
/// Compute intermediate value (u)
|
||||
/// </summary>
|
||||
public static byte[] ComputeU(byte[] clientPublicEphemeral, byte[] serverPublicEphemeral)
|
||||
{
|
||||
|
||||
@@ -14,6 +14,9 @@ namespace Lidgren.Network
|
||||
private string m_serviceUrl;
|
||||
private NetPeer m_peer;
|
||||
|
||||
/// <summary>
|
||||
/// NetUPnP constructor
|
||||
/// </summary>
|
||||
public NetUPnP(NetPeer peer)
|
||||
{
|
||||
m_peer = peer;
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace ChatClient
|
||||
NetPeerConfiguration config = new NetPeerConfiguration("chat");
|
||||
s_client = new NetClient(config);
|
||||
|
||||
Application.Idle += new EventHandler(Application_Idle);
|
||||
s_client.RegisterReceivedCallback(new SendOrPostCallback(GotMessage));
|
||||
|
||||
Application.Run(s_form);
|
||||
|
||||
@@ -36,44 +36,40 @@ namespace ChatClient
|
||||
NativeMethods.AppendText(s_form.richTextBox1, text);
|
||||
}
|
||||
|
||||
private static void Application_Idle(object sender, EventArgs e)
|
||||
public static void GotMessage(object peer)
|
||||
{
|
||||
while (NativeMethods.AppStillIdle)
|
||||
NetIncomingMessage im;
|
||||
while ((im = s_client.ReadMessage()) != null)
|
||||
{
|
||||
NetIncomingMessage im;
|
||||
while ((im = s_client.ReadMessage()) != null)
|
||||
// handle incoming message
|
||||
switch (im.MessageType)
|
||||
{
|
||||
// handle incoming message
|
||||
switch (im.MessageType)
|
||||
{
|
||||
case NetIncomingMessageType.DebugMessage:
|
||||
case NetIncomingMessageType.ErrorMessage:
|
||||
case NetIncomingMessageType.WarningMessage:
|
||||
case NetIncomingMessageType.VerboseDebugMessage:
|
||||
string text = im.ReadString();
|
||||
Output(text);
|
||||
break;
|
||||
case NetIncomingMessageType.StatusChanged:
|
||||
NetConnectionStatus status = (NetConnectionStatus)im.ReadByte();
|
||||
case NetIncomingMessageType.DebugMessage:
|
||||
case NetIncomingMessageType.ErrorMessage:
|
||||
case NetIncomingMessageType.WarningMessage:
|
||||
case NetIncomingMessageType.VerboseDebugMessage:
|
||||
string text = im.ReadString();
|
||||
Output(text);
|
||||
break;
|
||||
case NetIncomingMessageType.StatusChanged:
|
||||
NetConnectionStatus status = (NetConnectionStatus)im.ReadByte();
|
||||
|
||||
if (status == NetConnectionStatus.Connected)
|
||||
s_form.EnableInput();
|
||||
else
|
||||
s_form.DisableInput();
|
||||
string reason = im.ReadString();
|
||||
Output(status.ToString() + ": " + reason);
|
||||
|
||||
break;
|
||||
case NetIncomingMessageType.Data:
|
||||
string chat = im.ReadString();
|
||||
Output(chat);
|
||||
break;
|
||||
default:
|
||||
Output("Unhandled type: " + im.MessageType + " " + im.LengthBytes + " bytes");
|
||||
break;
|
||||
}
|
||||
if (status == NetConnectionStatus.Connected)
|
||||
s_form.EnableInput();
|
||||
else
|
||||
s_form.DisableInput();
|
||||
string reason = im.ReadString();
|
||||
Output(status.ToString() + ": " + reason);
|
||||
|
||||
break;
|
||||
case NetIncomingMessageType.Data:
|
||||
string chat = im.ReadString();
|
||||
Output(chat);
|
||||
break;
|
||||
default:
|
||||
Output("Unhandled type: " + im.MessageType + " " + im.LengthBytes + " bytes");
|
||||
break;
|
||||
}
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,6 +89,7 @@ namespace ChatClient
|
||||
// s_client.Shutdown("Requested by user");
|
||||
}
|
||||
|
||||
// called by the UI
|
||||
public static void Send(string text)
|
||||
{
|
||||
NetOutgoingMessage om = s_client.CreateMessage(text);
|
||||
|
||||
Reference in New Issue
Block a user