static void Main(string[] args) { WebSocket websocket = new WebSocket("ws://127.0.0.1:8080"); int packetSeq = 0; Protocol0Header rxheader; Protocol0Body rxbody; Guid ourUUID = Guid.Empty; Guid crassusUUID = Guid.Empty; Random random = new Random(); // Shortcut functions Protocol0 fastPacket = new Protocol0(); websocket.OnMessage += (sender, data) => { // Parse the inbound packet JArray dataBlockMaster = JArray.Parse(data.Data); // Convert into tokens IList <JToken> dataBlockChildren = dataBlockMaster.Children().ToList(); // PacketSeq packetSeq++; //Console.WriteLine("Json RX: {0}", data.Data); if (packetSeq == 1) { foreach (JToken clientPluginVersion in dataBlockChildren) { protocolVersion.Add(clientPluginVersion.ToObject <uint>()); Console.WriteLine("Server supports: {0}", clientPluginVersion); } if (protocolVersion.Count == 0) { Console.WriteLine("We do not support any mutual version the server does!"); websocket.Close(); } else { Console.WriteLine("Using protocol: {0}", protocolVersion[0]); } } else if (packetSeq == 2) { rxheader = dataBlockChildren[0].ToObject <Protocol0Header>(); rxbody = dataBlockChildren[1].ToObject <Protocol0Body>(); Guid[] decodedJson = JsonConvert.DeserializeObject <Guid[]>(rxbody.data); crassusUUID = decodedJson[0]; ourUUID = decodedJson[1]; Console.WriteLine( "Our UUID is: {0},\nThe crassus controller is on: {1}", ourUUID, crassusUUID ); // Lets send a subscribe! string channelQuery = fastPacket.CrassusCommand( crassusUUID, ourUUID, new string[] { "SUBSCRIPTION", "LIST" } ); websocket.Send(channelQuery); } else if (packetSeq == 3) { rxheader = dataBlockChildren[0].ToObject <Protocol0Header>(); rxbody = dataBlockChildren[1].ToObject <Protocol0Body>(); string[] channels = JsonConvert.DeserializeObject <string[]>(rxbody.data); foreach (string channel in channels) { Console.WriteLine("Server has a channel: {0}", channel); if (!channel.Equals("ECHO")) { continue; } websocket.Send( fastPacket.CrassusCommand( crassusUUID, ourUUID, new string[] { "SUBSCRIPTION", "ADD", channel } ) ); } } else { rxheader = dataBlockChildren[0].ToObject <Protocol0Header>(); rxbody = dataBlockChildren[1].ToObject <Protocol0Body>(); } // Elsewhere this is a normal packet! }; websocket.OnOpen += (sender, data) => { }; websocket.OnClose += (sender, data) => { }; websocket.Connect(); { websocket.Send(JsonConvert.SerializeObject(PacketVersions.ToArray())); } Console.ReadKey(); }
static void Consumer(Object MyID) { // Our ID int workerID = (int)MyID; // Pre create space for the header and body and internal guid Protocol rxheader = new Protocol(); Protocol rxbody = new Protocol(); // Register us some queues and whatnot Worker Workload = new Worker(); // A little debug Console.WriteLine("Worker: {0} started", workerID); // The initial negotiation // Wait for some work to do ( string jsonPacket, string websocketID ) = Workers[workerID].Queue.Take(); // Get a reference to our WebSocket WebSocketContainer webSocket = globalWebSockets[websocketID]; JArray dataBlockMaster; IList <JToken> dataBlockChildren = new JArray(); // Decrement the queue for this websocket crassus.queueSizes[workerID]--; try { // Parse the inbound packet dataBlockMaster = JArray.Parse(jsonPacket); // Convert into tokens dataBlockChildren = dataBlockMaster.Children().ToList(); } catch (Exception exception) { Console.WriteLine("Exception raised decoding json: {0}", exception.Message); globalWebSockets.Remove(websocketID); } List <uint> pluginVersions = new List <uint>(); foreach (JToken clientPluginVersion in dataBlockChildren) { pluginVersions.Add(clientPluginVersion.ToObject <uint>()); } // Do some logic to figure out what we want to use ... accept it webSocket.protocolVersionSupport = PacketStructures.Negotiate( pluginVersions, true ); webSocket.negotiatedVersion = true; // Send a response with our supported versions, highest first // Send out via the websocket webSocket.socket.Send( JsonConvert.SerializeObject( webSocket.protocolVersionSupport ) ); // If this was a blank array disconnect now if (webSocket.protocolVersionSupport.Length == 0) { globalWebSockets.Remove(websocketID); } // If we got here we have some virgin we agree on webSocket.protocolVersionLock = webSocket.protocolVersionSupport[0]; // Our shortcut functions should be availible here Protocol0 castPacket = new Protocol0(); // Send a welcome header string greetingPacket = castPacket.CrassusResponse( crassus.GetGuid(), webSocket.InternalGuid, new string[] { crassus.GetGuid().ToString(), webSocket.InternalGuid.ToString() } ); webSocket.socket.Send(greetingPacket); // We have a fully associated plugin lets deal with it while (true) { // Wait for some work to do ( jsonPacket, websocketID ) = Workers[workerID].Queue.Take(); // Decrement the queue for this websocket crassus.queueSizes[workerID]--; // Depending on version decrypt the packets correctly switch (webSocket.protocolVersionLock) { case 0: try { // Parse the inbound packet dataBlockMaster = JArray.Parse(jsonPacket); // Convert into tokens dataBlockChildren = dataBlockMaster.Children().ToList(); rxheader = dataBlockChildren[0].ToObject <Protocol0Header>(); rxbody = dataBlockChildren[1].ToObject <Protocol0Body>(); } catch (Exception exception) { Console.WriteLine("Exception raised: {0}", exception.Message); } Guid dst = ((Protocol0Body)rxbody).routing["destination"]; if (dst.Equals(crassus.GetGuid())) { // This is a message for US! string[] arguments; try { arguments = JsonConvert.DeserializeObject <string[]>(((Protocol0Body)rxbody).data); } catch (Exception exception) { Console.WriteLine( "Exception raised: {0}", exception.Message ); arguments = new string[] { "", "", "" }; } if (arguments[0].Equals("SUBSCRIPTION")) { if (arguments[1].Equals("LIST")) { string channelList = castPacket.CrassusResponse( crassus.GetGuid(), webSocket.InternalGuid, crassus.ListChannels() ); webSocket.socket.Send(channelList); } else if (arguments[1].Equals("ADD")) { Console.WriteLine("Plugin requested joining: {0}", arguments[2]); } } } break; default: Console.WriteLine( "No idea what to do with version: '{0}', " + "this client should not have sent this" ); globalWebSockets.Remove(websocketID); continue; } // Processing time! // If the destination is 00000-0000-000-00 etc then its for crassus its self //Console.WriteLine("Packet receieved!"); } }