/// <summary> /// B /// locking thread to receive and process data. /// </summary> public void ServerThread() { status = 1; while(status == 1) { var message = server.ReceiveMessage(TimeSpan.FromMilliseconds(500)); if (message.FrameCount == 0) continue; //Determine the identity of the host var identity = message[0]; var data = message[1]; //Initial connection will be a 15 byte identity. if(identity.BufferSize == 15) { //This person has no identity. Check for hello message if(data.Buffer[0] == (byte)MessageIdentifier.Init) { //Response message var response = new byte[28]; response[0] = (byte)MessageIdentifier.SetIdentity; log.Debug("Assigned new client a 27 byte identity."); //Setup a host object for this. var host = new Host(); HostCache.RegisterHost(host); host.Id.CopyTo(response, 1); server.SendMore(identity.Buffer); server.Send(response); }else { log.Debug("Client with 1 byte identity, message "+Enum.GetName(typeof(MessageIdentifier), data.Buffer[0])); } }else if(identity.BufferSize == 0) { log.Error("Empty identity message discarded."); } else { //Find the host object. var hostIdentity = HostCache.FindHost(identity.Buffer); if (hostIdentity == null) { log.Debug("Client contacted with invalid identity!"); lock (server) { server.SendMore(identity.Buffer); server.Send(new byte[] {(byte) MessageIdentifier.InvalidIdentity}); } }else { hostIdentity.ProcessMessage(this, data.Buffer); } } } server.Unbind(Settings.Default.Protocol+"://*:"+port); }
/// <summary> /// Register a new host in the database. /// </summary> /// <param name="host"></param> public static void RegisterHost(Host host) { hosts[host.Id] = host; }