public Root proccess(Root request) { Root answer = new Root(); if (request == null) { answer.ResponseParameters = new ResponseParameters(); answer.ResponseParameters.Exception = "request was null, network Error"; return answer; } try { if (request.RequestType == (int)ERequestType.Login) { answer = proccessLogin(request); } else if (request.RequestType == (int)ERequestType.Register) { answer = proccessRegister(request); } else { if (request.AuthInfo.Token == null || Int32.Parse(request.AuthInfo.Token) <= 0) { answer.ResponseParameters = new ResponseParameters(); answer.ResponseParameters.Exception = "Operation " + System.Enum.GetName(ERequestType.Get.GetType(), (ERequestType)(request.RequestType)) + " on ObjectType " + System.Enum.GetName(EObjectType.Login.GetType(), (EObjectType)(request.ObjectType)) + " not permitted without login"; } else if (processorMap.Keys.Contains(request.ObjectType)) { request.AuthInfo.ID_User = Int32.Parse(request.AuthInfo.Token); answer = processorMap[request.ObjectType].proccess(request); } } } catch (Exception ex) { if (answer.ResponseParameters == null) { answer.ResponseParameters = new ResponseParameters(); } answer.ResponseParameters.Exception = ex.ToString(); #if !DEBUG Logger.Log("Exception in RequestProcessor.proccessRequest" + ex.ToString(), 0); #else //System.Console.WriteLine("Exception in RequestProcessor.proccessRequest " + ex.ToString()); #endif } return answer; }
static void RunClient(object state) { Root root = new Root(); root.AuthInfo = new AuthenticationInfo(); root.AuthInfo.Username = "******"; root.RequestType = (int)ERequestType.Login; Console.WriteLine("CLIENT: Opening connection..."); using (TcpClient client = new TcpClient()) { client.Connect(new IPEndPoint(IPAddress.Loopback, PORT)); using (NetworkStream stream = client.GetStream()) { Console.WriteLine("CLIENT: Got connection; sending data..."); Serializer.SerializeWithLengthPrefix(stream, root, PrefixStyle.Base128); Console.WriteLine("CLIENT: Attempting to read data..."); Root rootAnswer = Serializer.DeserializeWithLengthPrefix<Root>(stream, PrefixStyle.Base128); Console.WriteLine("CLIENT: Got customer: "+ root.AuthInfo.Token); Console.WriteLine("CLIENT: Sending happy..."); stream.WriteByte(123); // just to show all bidirectional comms are OK Console.WriteLine("CLIENT: Closing..."); stream.Close(); } client.Close(); } }
private Root proccessRegister(Root request) { Root r = new Root(); r.ResponseParameters = new ResponseParameters(); AnrlDataContext db = new AnrlDataContext(); if (db.t_Users.Count(p => p.Name == request.AuthInfo.Username) == 0) { t_User user = new t_User(); user.Name = request.AuthInfo.Username; user.Password = request.AuthInfo.Password; user.ID_Role = 0; db.t_Users.InsertOnSubmit(user); db.SubmitChanges(); r.AuthInfo = new AuthenticationInfo(); } else { r.ResponseParameters.Exception = "Username already in use"; } return r; }
private Root proccessLogin(Root request) { Root r = new Root(); r.AuthInfo = new AuthenticationInfo(); r.ResponseParameters = new ResponseParameters(); AnrlDataContext db = new AnrlDataContext(); if (db.t_Users.Count(p => p.Name == request.AuthInfo.Username && p.Password == request.AuthInfo.Password) == 1) { r.AuthInfo.Token = db.t_Users.Single(p => p.Name == request.AuthInfo.Username && p.Password == request.AuthInfo.Password).ID.ToString(); } else { r.ResponseParameters.Exception = "Username / Password wrong"; } return r; }