Browse files

change tcp message content to JSON, making inqueue and outqueue queue…

…s of packets.
  • Loading branch information...
1 parent e2e299c commit cb4baa12d53ab027cefa306070ce9b338e4d322a @elevatorguy committed May 10, 2012
Showing with 135 additions and 122 deletions.
  1. +28 −100 TDSM_Android/AndroidPlugin.cs
  2. +65 −0 TDSM_Android/Packet.cs
  3. +40 −22 TDSM_Android/Server.cs
  4. +2 −0 TDSM_Android/TDSM_Android.csproj
View
128 TDSM_Android/AndroidPlugin.cs
@@ -21,7 +21,6 @@ public class AndroidPlugin : BasePlugin
public static bool isEnabled = false;
public static AndroidPlugin instance;
- public static String S = Char.ToString((char)0x1e); // SEPARATOR ▲
public static String LS = Char.ToString((char)0x1f); // LIST SEPARATOR ▼
public AndroidPlugin()
@@ -60,7 +59,7 @@ void OnServerStateChange(ref HookContext ctx, ref HookArgs.ServerStateChange arg
if (args.ServerChangeState == ServerState.LOADED)
{
ProgramLog.Plugin.Log("Starting Android server...");
- server = new Server(7778, S);
+ server = new Server(7778);
ProgramLog.Plugin.Log("Android server Started.");
Thread q2t = new Thread(queue2terraria);
@@ -87,7 +86,7 @@ void OnPlayerChat(ref HookContext ctx, ref HookArgs.PlayerChat args)
string message = args.Message;
if (playername != null && message != null)
- send(Packet.Chat, message, playername);
+ send(Packet.Type.CHAT, message, playername);
}
return;
@@ -118,7 +117,7 @@ void onPlayerPartyChange(ref HookContext ctx, ref HookArgs.PartySettingReceived
partycolor = "fdd017";
message = ctx.Player.Name + " has joined the yellow party.";
}
- send(Packet.PartyChange, message, "Terraria", partycolor);
+ send(Packet.Type.PARTYCHANGE, message, "Terraria", partycolor);
return;
}
@@ -127,7 +126,7 @@ void OnDeath(ref HookContext ctx, ref HookArgs.ObituaryReceived args)
{
string message = ctx.Player.Name + args.Obituary;
- send(Packet.Chat, message);
+ send(Packet.Type.CHAT, message);
return;
}
@@ -138,8 +137,8 @@ void OnLeave(ref HookContext ctx, ref HookArgs.PlayerLeftGame args)
var player = ctx.Player;
if (player.Name == null) return;
string message = ctx.Player.Name + " left Terraria.";
- send(Packet.Chat, message);
- send(Packet.PlayersOnline, List(), "Update");
+ send(Packet.Type.CHAT, message);
+ send(Packet.Type.PLAYERSONLINE, List(), "Update");
}
[Hook(HookOrder.TERMINAL)]
@@ -149,8 +148,8 @@ void OnJoin(ref HookContext ctx, ref HookArgs.PlayerEnteringGame args)
if (player.Name == null) return;
string message = ctx.Player.Name + " joined Terraria.";
- send(Packet.Chat, message);
- send(Packet.PlayersOnline, List(), "Update");
+ send(Packet.Type.CHAT, message);
+ send(Packet.Type.PLAYERSONLINE, List(), "Update");
}
#endregion
@@ -160,12 +159,10 @@ private static void queue2terraria()
{
if (server.inqueue.Count > 0)
{
- String data = null;
- server.inqueue.TryDequeue(out data);
+ Packet info = null;
+ server.inqueue.TryDequeue(out info);
- ReceivedDataInfo info = new ReceivedDataInfo(data);
-
- if(info.packet.Equals(Packet.Chat))
+ if(info.type.Equals(Packet.Type.CHAT))
{
ProgramLog.Chat.Log("[Android] <" + info.from + "> " + info.data1);
//send message to terraria players
@@ -174,31 +171,31 @@ private static void queue2terraria()
msg.PlayerChat(255, "<" + info.from + "> " + info.data1, color.R, color.G, color.B);
msg.Broadcast();
}
- else if (info.packet.Equals(Packet.Join) || info.packet.Equals(Packet.Leave))
+ else if (info.type.Equals(Packet.Type.JOIN) || info.type.Equals(Packet.Type.LEAVE))
{
ProgramLog.Chat.Log("[Android] " + info.from + info.data1);
var msg = NetMessage.PrepareThreadInstance();
msg.PlayerChat(255, info.from + info.data1, 255, 240, 20);
msg.Broadcast();
}
- else if (info.packet.Equals(Packet.PlayersOnline))
+ else if (info.type.Equals(Packet.Type.PLAYERSONLINE))
{
sendplayerlist(info.from);
}
- else if (info.packet.Equals(Packet.Kick))
+ else if (info.type.Equals(Packet.Type.KICK))
{
kick(info.data1, info.from);
}
- else if (info.packet.Equals(Packet.Ban))
+ else if (info.type.Equals(Packet.Type.BAN))
{
ban(info.data1, info.from);
}
- else if (info.packet.Equals(Packet.Op))
+ else if (info.type.Equals(Packet.Type.OP))
{
op(info.data1, info.from);
}
- if (info.packet.Equals(Packet.Join))
+ if (info.type.Equals(Packet.Type.JOIN))
server.setName(info.from);
}
Thread.Sleep(250);
@@ -281,109 +278,40 @@ public static string List()
return list;
}
- public class Packet
- {
- public const String Join = "1";
- public const String Leave = "2";
- public const String Confirm = "3";
- public const String PartyChange = "4";
- public const String Chat = "5";
- public const String PlayersOnline = "6";
- public const String Kick = "7";
- public const String Ban = "8";
- public const String Op = "9";
- public const String Give = "10";
- public const String Spawn = "11";
- }
-
public void send(String packettype, String message, String messageauthor = "Terraria", String data2 = null, String originator = "server")
{
if (data2 == null)
{
- server.outqueue.Enqueue(originator + S + packettype + S + message + S + messageauthor);
+ server.outqueue.Enqueue(new Packet(originator, packettype, messageauthor, message));
//ProgramLog.Debug.Log(originator + S + packettype + S + message + S + messageauthor);
}
else
{
- server.outqueue.Enqueue(originator + S + packettype + S + message + S + messageauthor + S + data2);
+ server.outqueue.Enqueue(new Packet(originator , packettype , messageauthor, message , data2));
//ProgramLog.Debug.Log(originator + S + packettype + S + message + S + messageauthor + S + data2);
}
}
public static void sendplayerlist(String target)
{
- server.sendToOneClient("server"+S+Packet.PlayersOnline+S+List()+S+target, target);
+ server.sendToOneClient(new Packet("server",Packet.Type.PLAYERSONLINE,target, List()), target);
}
- public void changeoriginator(String tochange, String originator = "server")
+ public void changeoriginator(Packet tochange, String originator = "server")
{
- ReceivedDataInfo info = new ReceivedDataInfo(tochange);
- if (info.packet != Packet.Leave && info.packet != Packet.Join && info.packet != Packet.PlayersOnline)
+ Packet info = new Packet(originator, tochange.type, tochange.from, tochange.data1, tochange.data2, tochange.data3);
+ if (info.type != Packet.Type.LEAVE && info.type != Packet.Type.JOIN && info.type != Packet.Type.PLAYERSONLINE)
{
- send(info.packet, info.data1, info.from, null, originator);
+ send(info.type, info.data1, info.from, null, originator);
}
- if (info.packet == Packet.Leave || info.packet == Packet.Join)
+ if (info.type == Packet.Type.LEAVE)
{
- send(Packet.Chat, info.data2, "Terraria", null, originator);
+ send(Packet.Type.CHAT, info.from + " has left chat.", "Terraria", null, originator);
}
- // the minus 1 is because if the newline that java sends in
- }
-
- public class ReceivedDataInfo
- {
- public String packet;
- public String from;
- public String data1;
- public String data2;
-
- public ReceivedDataInfo(String toparse)
+ else if (info.type == Packet.Type.JOIN)
{
- string[] dataarr = toparse.Split(S.ToCharArray());
-
- switch (dataarr[1])
- {
- case Packet.Chat:
- packet = Packet.Chat;
- from = dataarr[2];
- data1 = dataarr[3];
- break;
- case Packet.Join:
- packet = Packet.Join;
- from = dataarr[2];
- data1 = " has joined chat.";
- data2 = from + data1;
- break;
- case Packet.Leave:
- packet = Packet.Leave;
- from = dataarr[2];
- data1 = " has left chat.";
- data2 = from + data1;
- break;
- case Packet.PlayersOnline:
- packet = Packet.PlayersOnline;
- from = dataarr[2];
- data1 = "request";
- break;
- case Packet.Kick:
- packet = Packet.Kick;
- from = dataarr[2];
- data1 = dataarr[3];
- break;
- case Packet.Ban:
- packet = Packet.Ban;
- from = dataarr[2];
- data1 = dataarr[3];
- break;
- case Packet.Op:
- packet = Packet.Op;
- from = dataarr[2];
- data1 = dataarr[3];
- break;
- default:
- break;
- }
+ send(Packet.Type.CHAT, info.from + " has joined chat.", "Terraria", null, originator);
}
}
-
}
}
View
65 TDSM_Android/Packet.cs
@@ -0,0 +1,65 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace TDSM_Android
+{
+ public class Packet
+ {
+ public String originator;
+ public String type;
+ public String from;
+ public String data1;
+ public String data2;
+ public String data3;
+
+ public Packet()
+ : this(null, null, null, null, null, null) { }
+
+ public Packet(String originator, String type, String from)
+ : this(originator, type, from, null, null, null)
+ {
+ if (type.Equals(Packet.Type.LEAVE))
+ {
+ data1 = " has left chat.";
+ }
+ else if (type.Equals(Packet.Type.JOIN))
+ {
+ data1 = " has joined chat.";
+ }
+ }
+
+ public Packet(String originator, String type, String from, String data1)
+ : this(originator, type, from, data1, null, null) { }
+
+ public Packet(String originator, String type, String from, String data1, String data2)
+ : this(originator, type, from, data1, data2, null) { }
+
+ public Packet(String originator, String type, String from, String data1, String data2, String data3)
+ {
+ this.originator = originator;
+ this.type = type;
+ this.from = from;
+ this.data1 = data1;
+ this.data2 = data2;
+ this.data3 = data3;
+ }
+
+ public class Type
+ {
+ public const String JOIN = "1";
+ public const String LEAVE = "2";
+ public const String CONFIRM = "3";
+ public const String PARTYCHANGE = "4";
+ public const String CHAT = "5";
+ public const String PLAYERSONLINE = "6";
+ public const String KICK = "7";
+ public const String BAN = "8";
+ public const String OP = "9";
+ public const String GIVE = "10";
+ public const String SPAWN = "11";
+ }
+ }
+}
View
62 TDSM_Android/Server.cs
@@ -5,6 +5,7 @@
using System.Net.Sockets;
using System.Text;
using System.Threading;
+using System.Web.Script.Serialization;
using Terraria_Server;
using Terraria_Server.Logging;
@@ -16,18 +17,19 @@ public class Server
private Thread listenThread;
private Boolean enabled = true;
- public ConcurrentQueue<string> inqueue; // from socket
- public ConcurrentQueue<string> outqueue; // to socket
+ public ConcurrentQueue<Packet> inqueue; // from socket
+ public ConcurrentQueue<Packet> outqueue; // to socket
+
+ private static JavaScriptSerializer serial;
private int connectedClients;
public ConcurrentDictionary<NetworkStream, KeyValuePair<TcpClient, String>> clientlist;
private int port;
- private String S;
- public Server(int port, String Separator)
+ public Server(int port)
{
- S = Separator;
this.port = port;
+ serial = new JavaScriptSerializer();
tcpListener = new TcpListener(IPAddress.Any, port);
listenThread = new Thread(new ThreadStart(ListenForClients));
listenThread.Name = "Asrv";
@@ -36,8 +38,8 @@ public Server(int port, String Separator)
ProgramLog.Log("[Android] Starting Server...");
- inqueue = new ConcurrentQueue<string>();
- outqueue = new ConcurrentQueue<string>();
+ inqueue = new ConcurrentQueue<Packet>();
+ outqueue = new ConcurrentQueue<Packet>();
clientlist = new ConcurrentDictionary<NetworkStream, KeyValuePair<TcpClient, String>>();
connectedClients = 0;
@@ -113,7 +115,7 @@ private void HandleClientComm(object client)
//when the join packed with the username is sent, thats when its added to clientlist
clientlist.TryAdd(clientStream, new KeyValuePair<TcpClient, String>(tcpClient, "justjoined"));
- sendToOneClient("server"+S+"5"+S+"hello android application!"+S+"Server", clientStream);
+ sendToOneClient(new Packet("server",Packet.Type.CHAT,"Server","hello android application!"), clientStream);
connectedClients++;
ProgramLog.Log("There are now " + connectedClients + " connected clients");
}
@@ -169,20 +171,30 @@ private void socket2queue(Object clientinfoobject)
data = data.Substring(0, data.Length-1); // useable data. gets rid of the "/n" that java is putting into the stream at the end
- //the other android clients need to be able to chat with each other
- AndroidPlugin.instance.changeoriginator(data); // send it back to the outqueue, but with originator of "server"
- // we'll let the android app deal with filtering out chat from itself
+ Packet p = new Packet();
+ try
+ {
+ p = serial.Deserialize<Packet>(data);
- // for terraria
- inqueue.Enqueue(data);
+ //the other android clients need to be able to chat with each other
+ AndroidPlugin.instance.changeoriginator(p); // send it back to the outqueue, but with originator of "server"
+ // we'll let the android app deal with filtering out chat from itself
+
+ // for terraria
+ inqueue.Enqueue(p);
+ }
+ catch (ArgumentException e)
+ {
+ // ignore the packet
+ }
}
KeyValuePair<TcpClient, String> name;
clientlist.TryGetValue(stream, out name);
stream.Close();
tcpClient.Close();
ProgramLog.Log("[Android] Client disconnected...");
- inqueue.Enqueue("androidapp" +S+ AndroidPlugin.Packet.Leave + S + name.Value); // the \n because we are faking a message from the client
- AndroidPlugin.instance.changeoriginator("androidapp" + S+AndroidPlugin.Packet.Leave + S + name.Value + S+"Terraria"); // and this, because the other android apps would like to know aswell
+ inqueue.Enqueue(new Packet("androidapp" , Packet.Type.LEAVE , name.Value)); // the \n because we are faking a message from the client
+ AndroidPlugin.instance.changeoriginator(new Packet("androidapp" ,Packet.Type.LEAVE , name.Value, "Terraria")); // and this, because the other android apps would like to know aswell
KeyValuePair<TcpClient, String> temp;
clientlist.TryRemove(stream, out temp); // i really don't like this way of doing this, but i don't like locks either :/
connectedClients--;
@@ -197,12 +209,16 @@ private void queue2socket()
{
if (outqueue.Count > 0 && connectedClients > 0)
{
- String message = null;
- outqueue.TryDequeue(out message);
+ Packet p = null;
+ outqueue.TryDequeue(out p);
+ if (p != null)
+ {
+ String message = serial.Serialize(p);
- byte[] buffer = new ASCIIEncoding().GetBytes(message + "\n"); // java is doing readLine, so we need a newline here :/
+ byte[] buffer = new ASCIIEncoding().GetBytes(message + "\n"); // java is doing readLine, so we need a newline here :/
- sendToAllclients(buffer);
+ sendToAllclients(buffer);
+ }
}
Thread.Sleep(250);
}
@@ -243,19 +259,21 @@ private NetworkStream getstream(String name)
return null;
}
- public void sendToOneClient(String message, String name)
+ public void sendToOneClient(Packet packet, String name)
{
NetworkStream stream = getstream(name);
if (stream != null)
{
- sendToOneClient(message, stream);
+ sendToOneClient(packet, stream);
}
}
// this is for the greeting message when
// an android device initially connects
- private void sendToOneClient(String message, NetworkStream stream)
+ private void sendToOneClient(Packet packet, NetworkStream stream)
{
+ string message = serial.Serialize(packet);
+
byte[] buffer = new ASCIIEncoding().GetBytes(message + "\n");
try
{
View
2 TDSM_Android/TDSM_Android.csproj
@@ -33,6 +33,7 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
+ <Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
@@ -43,6 +44,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AndroidPlugin.cs" />
+ <Compile Include="Packet.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Server.cs" />
</ItemGroup>

0 comments on commit cb4baa1

Please sign in to comment.