Added in network functionality so that clients can send data to server and other clients.

This commit is contained in:
2018-05-08 10:45:26 -07:00
parent 20f52e2707
commit 9c1314480c
7 changed files with 106 additions and 35 deletions

View File

@ -73,7 +73,7 @@ namespace ModdedUtilitiesNetworking
if (e.KeyPressed==Microsoft.Xna.Framework.Input.Keys.K)
{
multiplayer.sendModInfoReturnVoid(Framework.Features.Stardew.MessageFeatures.FSTRING_SendHUDMessageWithIcon, MessagesExtentions.HUDMessageIconIdentifier, new HUDMessage("My love is like fire",1));
multiplayer.sendModInfoReturnVoid(Framework.Features.Stardew.MessageFeatures.FSTRING_SendHUDMessageWithIcon, MessagesExtentions.HUDMessageIconIdentifier, new HUDMessage("My love is like fire",1),Framework.Enums.MessageTypes.messageTypes.SendToAll);
}
}

View File

@ -30,6 +30,11 @@ namespace ModdedUtilitiesNetworking.Framework.Clients
return "";
}
public virtual NetClient getClient()
{
return this.client;
}
protected override string getHostUserName()
{
return this.client.ServerConnection.RemoteEndPoint.Address.ToString();
@ -144,19 +149,19 @@ namespace ModdedUtilitiesNetworking.Framework.Clients
{
using (BinaryWriter writer = new BinaryWriter((Stream)bufferWriteStream))
{
if (message.MessageType != 20)
if (message.MessageType <20)
{
message.Write(writer);
}
else
{
OutgoingMessageBase.WriteFromMessage(message, writer);
OutgoingMessageBase.WriteFromMessage(message, writer);
}
}
}
int num = (int)this.client.SendMessage(message1, NetDeliveryMethod.ReliableOrdered);
int num = (int)this.client.SendMessage(message1, NetDeliveryMethod.ReliableOrdered);
if (num == (int)NetSendResult.Sent)
{
ModCore.monitor.Log("DONE Writing message from client!");
@ -187,9 +192,8 @@ namespace ModdedUtilitiesNetworking.Framework.Clients
base.processIncomingMessage(message);
//Packet signiture for functions that return nothing.
if (message.MessageType == 20)
if (message.MessageType == Enums.MessageTypes.SendOneWay || message.MessageType == Enums.MessageTypes.SendToAll)
{
object[] obj = message.Reader.ReadModdedInfoPacket();
string functionName = (string)obj[0];
string classType = (string)obj[1];
@ -198,8 +202,6 @@ namespace ModdedUtilitiesNetworking.Framework.Clients
return;
}
//message.Reader.ReadChar();
//Write Binary ententions reader

View File

@ -36,26 +36,28 @@ namespace ModdedUtilitiesNetworking.Framework
/// <param name="message"></param>
public void sendMessage(OutgoingMessage message)
{
if (Game1.server != null)
{
foreach (long peerId in (IEnumerable<long>)Game1.otherFarmers.Keys)
if (Game1.server != null)
{
Game1.server.sendMessage(peerId, message);
foreach (long peerId in (IEnumerable<long>)Game1.otherFarmers.Keys)
{
Game1.server.sendMessage(peerId, message);
}
}
}
if (Game1.client != null)
{
if (Game1.client is CustomLidgrenClient) {
(Game1.client as CustomLidgrenClient).sendMessage(message);
return;
}
if (Game1.client is CustomGalaxyClient)
if (Game1.client != null)
{
(Game1.client as CustomGalaxyClient).sendMessage(message);
return;
}
ModCore.monitor.Log("Error sending server message!!!");
if (Game1.client is CustomLidgrenClient)
{
(Game1.client as CustomLidgrenClient).sendMessage(message);
return;
}
if (Game1.client is CustomGalaxyClient)
{
(Game1.client as CustomGalaxyClient).sendMessage(message);
return;
}
ModCore.monitor.Log("Error sending server message!!!");
}
}
@ -84,15 +86,21 @@ namespace ModdedUtilitiesNetworking.Framework
/// <param name="data"></param>
/// <param name="source"></param>
/// <returns></returns>
public OutgoingMessage sendOutGoingMessageReturnVoid(string functionName, string objectParametersType, object data, Farmer source)
public OutgoingMessage sendOutGoingMessageReturnVoid(string functionName, string objectParametersType, object data, Farmer source,Enums.MessageTypes.messageTypes sendingInfo)
{
OutgoingMessage message = new OutgoingMessage((byte)20, source, makeDataArray(functionName, objectParametersType, data));
byte bite = new byte();
if (sendingInfo == Enums.MessageTypes.messageTypes.SendOneWay) bite = Enums.MessageTypes.SendOneWay;
if (sendingInfo == Enums.MessageTypes.messageTypes.SendToAll) bite = Enums.MessageTypes.SendToAll;
OutgoingMessage message = new OutgoingMessage(bite, source, makeDataArray(functionName, objectParametersType, data));
return message;
}
public OutgoingMessage sendOutGoingMessageReturnVoid(string functionName, Type objectParametersType, object data, Farmer source)
public OutgoingMessage sendOutGoingMessageReturnVoid(string functionName, Type objectParametersType, object data, Farmer source, Enums.MessageTypes.messageTypes sendingInfo)
{
OutgoingMessage message = new OutgoingMessage((byte)20, source, makeDataArray(functionName, objectParametersType.ToString(), data));
byte bite=new byte();
if (sendingInfo == Enums.MessageTypes.messageTypes.SendOneWay) bite = Enums.MessageTypes.SendOneWay;
if (sendingInfo == Enums.MessageTypes.messageTypes.SendToAll) bite = Enums.MessageTypes.SendToAll;
OutgoingMessage message = new OutgoingMessage(bite, source, makeDataArray(functionName, objectParametersType.ToString(), data));
return message;
}
@ -116,11 +124,11 @@ namespace ModdedUtilitiesNetworking.Framework
/// <param name="uniqueID"></param>
/// <param name="classType"></param>
/// <param name="data"></param>
public void sendModInfoReturnVoid(string uniqueID,Type classType,object data)
public void sendModInfoReturnVoid(string uniqueID,Type classType,object data, Enums.MessageTypes.messageTypes sendingInfo)
{
Farmer f = Game1.player;
OutgoingMessage message =ModCore.multiplayer.sendOutGoingMessageReturnVoid(uniqueID, classType, data, f);
OutgoingMessage message =ModCore.multiplayer.sendOutGoingMessageReturnVoid(uniqueID, classType, data, f, sendingInfo);
ModCore.multiplayer.sendMessage(message);
}
@ -132,11 +140,11 @@ namespace ModdedUtilitiesNetworking.Framework
/// <param name="uniqueID"></param>
/// <param name="classType"></param>
/// <param name="data"></param>
public void sendModInfoReturnVoid(string uniqueID, string classType, object data)
public void sendModInfoReturnVoid(string uniqueID, string classType, object data, Enums.MessageTypes.messageTypes sendingInfo)
{
Farmer f = Game1.player;
OutgoingMessage message = ModCore.multiplayer.sendOutGoingMessageReturnVoid(uniqueID, classType, data, f);
OutgoingMessage message = ModCore.multiplayer.sendOutGoingMessageReturnVoid(uniqueID, classType, data, f,sendingInfo);
ModCore.multiplayer.sendMessage(message);
}

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModdedUtilitiesNetworking.Framework.Enums
{
public class MessageTypes
{
public enum messageTypes
{
SendOneWay,
SendToAll,
SendToSpecific
}
/// <summary>
/// Sends a one way message. If sent from server it sends to all clients.
/// If sent from a client it sends only to the server.
/// </summary>
public const int SendOneWay = 20;
/// <summary>
/// Sends the message to all clients and the server.
/// </summary>
public const int SendToAll = 21;
/// <summary>
/// Unused.
/// </summary>
public const int SendToSpecific = 22;
}
}

View File

@ -379,20 +379,44 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
case 5:
this.warpFarmer(message.SourceFarmer, message.Reader.ReadInt16(), message.Reader.ReadInt16(), message.Reader.ReadString(), (int)message.Reader.ReadByte() == 1);
break;
case 20:
case Enums.MessageTypes.SendOneWay:
object[] obj = message.Reader.ReadModdedInfoPacket();
string functionName = (string)obj[0];
string classType = (string)obj[1];
object actualObject = ModCore.processTypesToRead(message.Reader, classType);
ModCore.processVoidFunction(functionName, actualObject);
break;
case Enums.MessageTypes.SendToAll:
object[] obj2 = message.Reader.ReadModdedInfoPacket();
string functionName2 = (string)obj2[0];
string classType2 = (string)obj2[1];
object actualObject2 = ModCore.processTypesToRead(message.Reader, classType2);
ModCore.processVoidFunction(functionName2, actualObject2);
this.rebroadcastClientMessageToAllClients(message);
break;
default:
ModCore.multiplayer.processIncomingMessage(message);
break;
}
//this.rebroadcastClientMessage(message);
}
/// <summary>
/// Takes an incoming client message and sends it back to all clients to process.
/// </summary>
/// <param name="message"></param>
private void rebroadcastClientMessageToAllClients(IncomingMessage message)
{
byte messageType = Enums.MessageTypes.SendOneWay;
Farmer f = Game1.player;
object data = message.Data.Clone();
OutgoingMessage message1 = new OutgoingMessage(messageType, f, data);
foreach (long peerId in (IEnumerable<long>)Game1.otherFarmers.Keys)
{
ModCore.monitor.Log("RESEND MESSAGE TO CLIENT!!!", StardewModdingAPI.LogLevel.Alert);
this.sendMessage(peerId, message1);
}
this.rebroadcastClientMessage(message);
}
private void rebroadcastClientMessage(IncomingMessage message)

View File

@ -197,7 +197,7 @@ namespace ModdedUtilitiesNetworking.Framework.Servers
using (NetBufferWriteStream bufferWriteStream = new NetBufferWriteStream((NetBuffer)message1))
{
using (BinaryWriter writer = new BinaryWriter((Stream)bufferWriteStream)) {
if (message.MessageType != 20)
if (message.MessageType < 20)
{
message.Write(writer);
}

View File

@ -54,6 +54,7 @@
<Compile Include="Framework\Clients\CustomLidgrenClient.cs" />
<Compile Include="Framework\DataInfo.cs" />
<Compile Include="Framework\Delegates\DelegateInfo.cs" />
<Compile Include="Framework\Enums\MessageTypes.cs" />
<Compile Include="Framework\Extentions\GenericExtentions.cs" />
<Compile Include="Framework\Extentions\StrardewValleyExtentions\MessagesExtentions.cs" />
<Compile Include="Framework\Extentions\XNAExtentions.cs" />