public void connectionHandling() { //Console.WriteLine("Tworzę wątek dla połączenia!"); NetworkStream stream = client.GetStream(); int max = 65565; Byte[] bytes = new Byte[max]; Byte[] respon = new Byte[max]; int i; RequestResolver resolver = new RequestResolver(client); while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { Communicate communicate = new Communicate(); try { if (communicate.fromByteArray(bytes)) { Communicate response = resolver.handle(communicate); // Inform server: Console.WriteLine(response.sessID + ": " + response.message != null ? response.message.content : ""); // Try to send response from server: respon = response.toByteArray(); stream.Write(respon, 0, respon.Length); stream.Flush(); } } catch (Exception e) { } } }
override protected int _execute(Communicate request) { // TODO: send message to user Session session = Session.getInstance(); User sender = new User { username = session.get(request.sessID, "username") }; User receiver = request.user; if (receiver != null) { string sid = session.getSessionIdByUsername(receiver.username); if (sid != null) { ClientsInfo clients = ClientsInfo.getInstance(); TcpClient client = clients.GetClient(sid); if (client != null) { NetworkStream stream = client.GetStream(); int max = 65565; Byte[] bytes = new Byte[max]; int i; Communicate user_message_response = new Communicate(); user_message_response.header = "received"; user_message_response.sessID = sid; user_message_response.user = sender; user_message_response.message.content = request.message != null ? request.message.content : ""; try { // Try to send response from server: bytes = user_message_response.toByteArray(); stream.Write(bytes, 0, bytes.Length); stream.Flush(); _response.header = "sent"; _response.message.content = "Message has been sent"; return(Command.OK); } catch (Exception e) {} } } } _response.header = "error"; _response.message.content = "Unknown receiver user"; return(Command.ERROR); }